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