Add log-related command-line options to unit tests.
You can now specify the following when running a unit test:
-p, --print-logs
Print the log messages as they are being generated.
-o, --log-file=FILE
Print all log messages to FILE instead of standard output
Also, when a test fails the messages no longer get dumped to standard
output. They only get dumped when specifying --print-logs.
Change-Id: Icc265734f302c27c24829409ca6f7c8fce2979d4
diff --git a/aos/common/common.gyp b/aos/common/common.gyp
index 5264dab..a0750fe 100644
--- a/aos/common/common.gyp
+++ b/aos/common/common.gyp
@@ -20,7 +20,6 @@
'dependencies': [
'<(AOS)/build/aos.gyp:logging',
'once',
- '<(EXTERNALS):gtest',
'<(AOS)/linux_code/ipc_lib/ipc_lib.gyp:shared_mem',
],
'export_dependent_settings': [
diff --git a/aos/common/queue_testutils.cc b/aos/common/queue_testutils.cc
index 824cb89..d71d023 100644
--- a/aos/common/queue_testutils.cc
+++ b/aos/common/queue_testutils.cc
@@ -41,22 +41,41 @@
}
}
+ void SetOutputFile(const char *filename) {
+ if (strcmp("-", filename) != 0) {
+ FILE *newfile = fopen(filename, "w");
+
+ if (newfile) {
+ output_file_ = newfile;
+ }
+ }
+ }
+
+ void PrintMessagesAsTheyComeIn() { print_as_messages_come_in_ = true; }
+
private:
TestLogImplementation() {}
+ ~TestLogImplementation() {
+ if (output_file_ != stdout) {
+ fclose(output_file_);
+ }
+ }
static TestLogImplementation *CreateInstance() {
return new TestLogImplementation();
}
virtual void HandleMessage(const LogMessage &message) override {
- if (message.level == FATAL) {
- logging::internal::PrintMessage(stdout, message);
+ if (message.level == FATAL || print_as_messages_come_in_) {
+ logging::internal::PrintMessage(output_file_, message);
}
messages_.push_back(message);
}
::std::vector<LogMessage> messages_;
+ bool print_as_messages_come_in_ = false;
+ FILE *output_file_ = stdout;
};
class MyTestEventListener : public ::testing::EmptyTestEventListener {
@@ -65,13 +84,8 @@
}
virtual void OnTestEnd(const ::testing::TestInfo &test_info) {
if (test_info.result()->Failed()) {
- printf("Test %s failed. Printing out all log messages.\n",
+ printf("Test %s failed. Use '--print-logs' to see all log messages.\n",
test_info.name());
- fputs("\tThis will include already printed WARNING and up messages.\n",
- stdout);
- fputs("\tIt will also include duplicates of all gtest failures.\n",
- stdout);
- TestLogImplementation::GetInstance()->PrintAllMessages();
}
}
@@ -145,6 +159,14 @@
CHECK_EQ(atexit(TerminateExitHandler), 0);
}
+void SetLogFileName(const char* filename) {
+ TestLogImplementation::GetInstance()->SetOutputFile(filename);
+}
+
+void ForcePrintLogsDuringTests() {
+ TestLogImplementation::GetInstance()->PrintMessagesAsTheyComeIn();
+}
+
} // namespace testing
} // namespace common
} // namespace aos
diff --git a/aos/common/queue_testutils.h b/aos/common/queue_testutils.h
index 3c7a0a4..fb0ffcd 100644
--- a/aos/common/queue_testutils.h
+++ b/aos/common/queue_testutils.h
@@ -35,6 +35,16 @@
// being run.
void PreventExit();
+// Redirect the messages enabled by EnableTestLogging() function to a file.
+// By default the messages are printed to standard output.
+void SetLogFileName(const char* filename);
+
+// Force the messages to be printed as they are handled by the logging
+// framework. This can be useful for tests that hang where no messages would
+// otherwise be printed. This is also useful for tests that do pass, but where
+// we want to use graphing tools to verify what's happening.
+void ForcePrintLogsDuringTests();
+
} // namespace testing
} // namespace common
} // namespace aos