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 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 9 | #include "aos/logging/implementations.h" |
| 10 | #include "aos/mutex/mutex.h" |
John Park | b5e4730 | 2020-01-08 19:58:18 -0800 | [diff] [blame^] | 11 | #include "absl/base/call_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 | |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 23 | // Sets the current thread's time to be monotonic_now for logging. |
| 24 | void MockTime(::aos::monotonic_clock::time_point monotonic_now) { |
| 25 | mock_time_ = true; |
| 26 | monotonic_now_ = monotonic_now; |
| 27 | } |
| 28 | |
| 29 | // Clears any mock time for the current thread. |
| 30 | void UnMockTime() { mock_time_ = false; } |
| 31 | |
| 32 | ::aos::monotonic_clock::time_point monotonic_now() const override { |
| 33 | if (mock_time_) { |
| 34 | return monotonic_now_; |
| 35 | } |
| 36 | return ::aos::monotonic_clock::now(); |
| 37 | } |
| 38 | |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 39 | // This class has to be a singleton so that everybody can get access to the |
| 40 | // same instance to read out the messages etc. |
| 41 | static TestLogImplementation *GetInstance() { |
John Park | b5e4730 | 2020-01-08 19:58:18 -0800 | [diff] [blame^] | 42 | static absl::once_flag once; |
| 43 | absl::call_once(once, CreateInstance); |
| 44 | return instance; |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | // Clears out all of the messages already recorded. |
| 48 | void ClearMessages() { |
Brian Silverman | 459d37a | 2015-03-29 18:00:30 -0400 | [diff] [blame] | 49 | ::aos::MutexLocker locker(&messages_mutex_); |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 50 | messages_.clear(); |
| 51 | } |
| 52 | |
| 53 | // Prints out all of the messages (like when a test fails). |
| 54 | void PrintAllMessages() { |
Brian Silverman | 459d37a | 2015-03-29 18:00:30 -0400 | [diff] [blame] | 55 | ::aos::MutexLocker locker(&messages_mutex_); |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 56 | for (auto it = messages_.begin(); it != messages_.end(); ++it) { |
| 57 | logging::internal::PrintMessage(stdout, *it); |
| 58 | } |
| 59 | } |
| 60 | |
Philipp Schrader | e41ed9d | 2015-03-15 22:57:13 +0000 | [diff] [blame] | 61 | void SetOutputFile(const char *filename) { |
| 62 | if (strcmp("-", filename) != 0) { |
| 63 | FILE *newfile = fopen(filename, "w"); |
| 64 | |
| 65 | if (newfile) { |
| 66 | output_file_ = newfile; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 71 | bool fill_type_cache() override { return false; } |
| 72 | |
Philipp Schrader | e41ed9d | 2015-03-15 22:57:13 +0000 | [diff] [blame] | 73 | void PrintMessagesAsTheyComeIn() { print_as_messages_come_in_ = true; } |
| 74 | |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 75 | private: |
John Park | b5e4730 | 2020-01-08 19:58:18 -0800 | [diff] [blame^] | 76 | static TestLogImplementation *instance; |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 77 | TestLogImplementation() {} |
Philipp Schrader | e41ed9d | 2015-03-15 22:57:13 +0000 | [diff] [blame] | 78 | ~TestLogImplementation() { |
| 79 | if (output_file_ != stdout) { |
| 80 | fclose(output_file_); |
| 81 | } |
| 82 | } |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 83 | |
John Park | b5e4730 | 2020-01-08 19:58:18 -0800 | [diff] [blame^] | 84 | static void CreateInstance() { |
| 85 | instance = new TestLogImplementation(); |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Brian Silverman | be858a1 | 2014-04-30 17:37:28 -0700 | [diff] [blame] | 88 | virtual void HandleMessage(const LogMessage &message) override { |
Brian Silverman | 459d37a | 2015-03-29 18:00:30 -0400 | [diff] [blame] | 89 | ::aos::MutexLocker locker(&messages_mutex_); |
Philipp Schrader | e41ed9d | 2015-03-15 22:57:13 +0000 | [diff] [blame] | 90 | if (message.level == FATAL || print_as_messages_come_in_) { |
| 91 | logging::internal::PrintMessage(output_file_, message); |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | messages_.push_back(message); |
| 95 | } |
| 96 | |
| 97 | ::std::vector<LogMessage> messages_; |
Philipp Schrader | e41ed9d | 2015-03-15 22:57:13 +0000 | [diff] [blame] | 98 | bool print_as_messages_come_in_ = false; |
| 99 | FILE *output_file_ = stdout; |
Brian Silverman | 459d37a | 2015-03-29 18:00:30 -0400 | [diff] [blame] | 100 | ::aos::Mutex messages_mutex_; |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 101 | |
| 102 | // Thread local storage for mock time. This is thread local because if |
| 103 | // someone spawns a thread and goes to town in parallel with a simulated event |
| 104 | // loop, we want to just print the actual monotonic clock out. |
| 105 | static thread_local bool mock_time_; |
| 106 | static thread_local ::aos::monotonic_clock::time_point monotonic_now_; |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 107 | }; |
| 108 | |
John Park | b5e4730 | 2020-01-08 19:58:18 -0800 | [diff] [blame^] | 109 | TestLogImplementation *TestLogImplementation::instance; |
| 110 | |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 111 | thread_local bool TestLogImplementation::mock_time_ = false; |
| 112 | thread_local ::aos::monotonic_clock::time_point |
| 113 | TestLogImplementation::monotonic_now_ = ::aos::monotonic_clock::min_time; |
| 114 | |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 115 | class MyTestEventListener : public ::testing::EmptyTestEventListener { |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 116 | virtual void OnTestStart(const ::testing::TestInfo & /*test_info*/) { |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 117 | TestLogImplementation::GetInstance()->ClearMessages(); |
| 118 | } |
| 119 | virtual void OnTestEnd(const ::testing::TestInfo &test_info) { |
| 120 | if (test_info.result()->Failed()) { |
Philipp Schrader | e41ed9d | 2015-03-15 22:57:13 +0000 | [diff] [blame] | 121 | 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] | 122 | test_info.name()); |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 123 | } |
| 124 | } |
Brian Silverman | b91a37d | 2013-03-09 17:54:30 -0800 | [diff] [blame] | 125 | |
| 126 | virtual void OnTestPartResult( const ::testing::TestPartResult &result) { |
| 127 | if (result.failed()) { |
| 128 | const char *failure_type = "unknown"; |
| 129 | switch (result.type()) { |
| 130 | case ::testing::TestPartResult::Type::kNonFatalFailure: |
| 131 | failure_type = "EXPECT"; |
| 132 | break; |
| 133 | case ::testing::TestPartResult::Type::kFatalFailure: |
| 134 | failure_type = "ASSERT"; |
| 135 | break; |
| 136 | case ::testing::TestPartResult::Type::kSuccess: |
| 137 | break; |
| 138 | } |
| 139 | log_do(ERROR, "%s: %d: gtest %s failure\n%s\n", |
| 140 | result.file_name(), |
| 141 | result.line_number(), |
| 142 | failure_type, |
| 143 | result.message()); |
| 144 | } |
| 145 | } |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | void *DoEnableTestLogging() { |
| 149 | logging::Init(); |
| 150 | logging::AddImplementation(TestLogImplementation::GetInstance()); |
| 151 | |
| 152 | ::testing::UnitTest::GetInstance()->listeners().Append( |
| 153 | new MyTestEventListener()); |
| 154 | |
Brian Silverman | f5f8d8e | 2015-12-06 18:39:12 -0500 | [diff] [blame] | 155 | return nullptr; |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 156 | } |
| 157 | |
John Park | b5e4730 | 2020-01-08 19:58:18 -0800 | [diff] [blame^] | 158 | static absl::once_flag enable_test_logging_once; |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 159 | |
| 160 | } // namespace |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 161 | |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 162 | void EnableTestLogging() { |
John Park | b5e4730 | 2020-01-08 19:58:18 -0800 | [diff] [blame^] | 163 | absl::call_once(enable_test_logging_once, DoEnableTestLogging); |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Philipp Schrader | e41ed9d | 2015-03-15 22:57:13 +0000 | [diff] [blame] | 166 | void SetLogFileName(const char* filename) { |
| 167 | TestLogImplementation::GetInstance()->SetOutputFile(filename); |
| 168 | } |
| 169 | |
| 170 | void ForcePrintLogsDuringTests() { |
| 171 | TestLogImplementation::GetInstance()->PrintMessagesAsTheyComeIn(); |
| 172 | } |
| 173 | |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 174 | void MockTime(::aos::monotonic_clock::time_point monotonic_now) { |
| 175 | TestLogImplementation::GetInstance()->MockTime(monotonic_now); |
| 176 | } |
| 177 | void UnMockTime() { |
| 178 | TestLogImplementation::GetInstance()->UnMockTime(); |
| 179 | } |
| 180 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 181 | } // namespace testing |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 182 | } // namespace aos |