Brian Silverman | f5f8d8e | 2015-12-06 18:39:12 -0500 | [diff] [blame] | 1 | #include "aos/testing/test_logging.h" |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 2 | |
Brian Silverman | f5f8d8e | 2015-12-06 18:39:12 -0500 | [diff] [blame] | 3 | #include <stdio.h> |
| 4 | |
| 5 | #include <vector> |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 6 | |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 7 | #include "gtest/gtest.h" |
| 8 | |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 9 | #include "aos/common/logging/implementations.h" |
Brian Silverman | 459d37a | 2015-03-29 18:00:30 -0400 | [diff] [blame] | 10 | #include "aos/common/mutex.h" |
Sabina Davis | 2ed5ea2 | 2017-09-26 22:27:42 -0700 | [diff] [blame^] | 11 | #include "aos/once.h" |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 12 | |
| 13 | using ::aos::logging::LogMessage; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 14 | |
| 15 | namespace aos { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 16 | namespace testing { |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 17 | namespace { |
| 18 | |
Brian Silverman | be858a1 | 2014-04-30 17:37:28 -0700 | [diff] [blame] | 19 | class TestLogImplementation : public logging::HandleMessageLogImplementation { |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 20 | public: |
| 21 | const ::std::vector<LogMessage> &messages() { return messages_; } |
| 22 | |
| 23 | // This class has to be a singleton so that everybody can get access to the |
| 24 | // same instance to read out the messages etc. |
| 25 | static TestLogImplementation *GetInstance() { |
| 26 | static Once<TestLogImplementation> once(CreateInstance); |
| 27 | return once.Get(); |
| 28 | } |
| 29 | |
| 30 | // Clears out all of the messages already recorded. |
| 31 | void ClearMessages() { |
Brian Silverman | 459d37a | 2015-03-29 18:00:30 -0400 | [diff] [blame] | 32 | ::aos::MutexLocker locker(&messages_mutex_); |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 33 | messages_.clear(); |
| 34 | } |
| 35 | |
| 36 | // Prints out all of the messages (like when a test fails). |
| 37 | void PrintAllMessages() { |
Brian Silverman | 459d37a | 2015-03-29 18:00:30 -0400 | [diff] [blame] | 38 | ::aos::MutexLocker locker(&messages_mutex_); |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 39 | for (auto it = messages_.begin(); it != messages_.end(); ++it) { |
| 40 | logging::internal::PrintMessage(stdout, *it); |
| 41 | } |
| 42 | } |
| 43 | |
Philipp Schrader | e41ed9d | 2015-03-15 22:57:13 +0000 | [diff] [blame] | 44 | void SetOutputFile(const char *filename) { |
| 45 | if (strcmp("-", filename) != 0) { |
| 46 | FILE *newfile = fopen(filename, "w"); |
| 47 | |
| 48 | if (newfile) { |
| 49 | output_file_ = newfile; |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | void PrintMessagesAsTheyComeIn() { print_as_messages_come_in_ = true; } |
| 55 | |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 56 | private: |
| 57 | TestLogImplementation() {} |
Philipp Schrader | e41ed9d | 2015-03-15 22:57:13 +0000 | [diff] [blame] | 58 | ~TestLogImplementation() { |
| 59 | if (output_file_ != stdout) { |
| 60 | fclose(output_file_); |
| 61 | } |
| 62 | } |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 63 | |
| 64 | static TestLogImplementation *CreateInstance() { |
| 65 | return new TestLogImplementation(); |
| 66 | } |
| 67 | |
Brian Silverman | be858a1 | 2014-04-30 17:37:28 -0700 | [diff] [blame] | 68 | virtual void HandleMessage(const LogMessage &message) override { |
Brian Silverman | 459d37a | 2015-03-29 18:00:30 -0400 | [diff] [blame] | 69 | ::aos::MutexLocker locker(&messages_mutex_); |
Philipp Schrader | e41ed9d | 2015-03-15 22:57:13 +0000 | [diff] [blame] | 70 | if (message.level == FATAL || print_as_messages_come_in_) { |
| 71 | logging::internal::PrintMessage(output_file_, message); |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | messages_.push_back(message); |
| 75 | } |
| 76 | |
| 77 | ::std::vector<LogMessage> messages_; |
Philipp Schrader | e41ed9d | 2015-03-15 22:57:13 +0000 | [diff] [blame] | 78 | bool print_as_messages_come_in_ = false; |
| 79 | FILE *output_file_ = stdout; |
Brian Silverman | 459d37a | 2015-03-29 18:00:30 -0400 | [diff] [blame] | 80 | ::aos::Mutex messages_mutex_; |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | class MyTestEventListener : public ::testing::EmptyTestEventListener { |
| 84 | virtual void OnTestStart(const ::testing::TestInfo &/*test_info*/) { |
| 85 | TestLogImplementation::GetInstance()->ClearMessages(); |
| 86 | } |
| 87 | virtual void OnTestEnd(const ::testing::TestInfo &test_info) { |
| 88 | if (test_info.result()->Failed()) { |
Philipp Schrader | e41ed9d | 2015-03-15 22:57:13 +0000 | [diff] [blame] | 89 | printf("Test %s failed. Use '--print-logs' to see all log messages.\n", |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 90 | test_info.name()); |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 91 | } |
| 92 | } |
Brian Silverman | b91a37d | 2013-03-09 17:54:30 -0800 | [diff] [blame] | 93 | |
| 94 | virtual void OnTestPartResult( const ::testing::TestPartResult &result) { |
| 95 | if (result.failed()) { |
| 96 | const char *failure_type = "unknown"; |
| 97 | switch (result.type()) { |
| 98 | case ::testing::TestPartResult::Type::kNonFatalFailure: |
| 99 | failure_type = "EXPECT"; |
| 100 | break; |
| 101 | case ::testing::TestPartResult::Type::kFatalFailure: |
| 102 | failure_type = "ASSERT"; |
| 103 | break; |
| 104 | case ::testing::TestPartResult::Type::kSuccess: |
| 105 | break; |
| 106 | } |
| 107 | log_do(ERROR, "%s: %d: gtest %s failure\n%s\n", |
| 108 | result.file_name(), |
| 109 | result.line_number(), |
| 110 | failure_type, |
| 111 | result.message()); |
| 112 | } |
| 113 | } |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | void *DoEnableTestLogging() { |
| 117 | logging::Init(); |
| 118 | logging::AddImplementation(TestLogImplementation::GetInstance()); |
| 119 | |
| 120 | ::testing::UnitTest::GetInstance()->listeners().Append( |
| 121 | new MyTestEventListener()); |
| 122 | |
Brian Silverman | f5f8d8e | 2015-12-06 18:39:12 -0500 | [diff] [blame] | 123 | return nullptr; |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | Once<void> enable_test_logging_once(DoEnableTestLogging); |
| 127 | |
| 128 | } // namespace |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 129 | |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 130 | void EnableTestLogging() { |
| 131 | enable_test_logging_once.Get(); |
| 132 | } |
| 133 | |
Philipp Schrader | e41ed9d | 2015-03-15 22:57:13 +0000 | [diff] [blame] | 134 | void SetLogFileName(const char* filename) { |
| 135 | TestLogImplementation::GetInstance()->SetOutputFile(filename); |
| 136 | } |
| 137 | |
| 138 | void ForcePrintLogsDuringTests() { |
| 139 | TestLogImplementation::GetInstance()->PrintMessagesAsTheyComeIn(); |
| 140 | } |
| 141 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 142 | } // namespace testing |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 143 | } // namespace aos |