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 | |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 9 | #include "absl/base/call_once.h" |
Brian Silverman | b47f555 | 2020-10-01 15:08:14 -0700 | [diff] [blame] | 10 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 11 | #include "aos/logging/implementations.h" |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 12 | #include "aos/stl_mutex/stl_mutex.h" |
Brian Silverman | b47f555 | 2020-10-01 15:08:14 -0700 | [diff] [blame] | 13 | #include "aos/thread_local.h" |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 14 | |
| 15 | using ::aos::logging::LogMessage; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 16 | |
| 17 | namespace aos { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 18 | namespace testing { |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 19 | namespace { |
| 20 | |
Brian Silverman | be858a1 | 2014-04-30 17:37:28 -0700 | [diff] [blame] | 21 | class TestLogImplementation : public logging::HandleMessageLogImplementation { |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 22 | public: |
| 23 | const ::std::vector<LogMessage> &messages() { return messages_; } |
| 24 | |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 25 | ::aos::monotonic_clock::time_point monotonic_now() const override { |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 26 | return ::aos::monotonic_clock::now(); |
| 27 | } |
| 28 | |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 29 | // This class has to be a singleton so that everybody can get access to the |
| 30 | // same instance to read out the messages etc. |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 31 | static std::shared_ptr<TestLogImplementation> GetInstance() { |
| 32 | static std::shared_ptr<TestLogImplementation> instance = |
| 33 | std::make_unique<TestLogImplementation>(); |
John Park | b5e4730 | 2020-01-08 19:58:18 -0800 | [diff] [blame] | 34 | return instance; |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | // Clears out all of the messages already recorded. |
| 38 | void ClearMessages() { |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 39 | std::unique_lock<aos::stl_mutex> locker(messages_mutex_); |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 40 | messages_.clear(); |
| 41 | } |
| 42 | |
| 43 | // Prints out all of the messages (like when a test fails). |
| 44 | void PrintAllMessages() { |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 45 | std::unique_lock<aos::stl_mutex> locker(messages_mutex_); |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 46 | for (auto it = messages_.begin(); it != messages_.end(); ++it) { |
| 47 | logging::internal::PrintMessage(stdout, *it); |
| 48 | } |
| 49 | } |
| 50 | |
Philipp Schrader | e41ed9d | 2015-03-15 22:57:13 +0000 | [diff] [blame] | 51 | void SetOutputFile(const char *filename) { |
| 52 | if (strcmp("-", filename) != 0) { |
| 53 | FILE *newfile = fopen(filename, "w"); |
| 54 | |
| 55 | if (newfile) { |
| 56 | output_file_ = newfile; |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | void PrintMessagesAsTheyComeIn() { print_as_messages_come_in_ = true; } |
| 62 | |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 63 | // Don't call these from outside this class. |
Philipp Schrader | e41ed9d | 2015-03-15 22:57:13 +0000 | [diff] [blame] | 64 | ~TestLogImplementation() { |
| 65 | if (output_file_ != stdout) { |
| 66 | fclose(output_file_); |
| 67 | } |
| 68 | } |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 69 | |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 70 | private: |
Brian Silverman | be858a1 | 2014-04-30 17:37:28 -0700 | [diff] [blame] | 71 | virtual void HandleMessage(const LogMessage &message) override { |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 72 | std::unique_lock<aos::stl_mutex> locker(messages_mutex_); |
Philipp Schrader | e41ed9d | 2015-03-15 22:57:13 +0000 | [diff] [blame] | 73 | if (message.level == FATAL || print_as_messages_come_in_) { |
| 74 | logging::internal::PrintMessage(output_file_, message); |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | messages_.push_back(message); |
| 78 | } |
| 79 | |
| 80 | ::std::vector<LogMessage> messages_; |
Philipp Schrader | e41ed9d | 2015-03-15 22:57:13 +0000 | [diff] [blame] | 81 | bool print_as_messages_come_in_ = false; |
| 82 | FILE *output_file_ = stdout; |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 83 | aos::stl_mutex messages_mutex_; |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | class MyTestEventListener : public ::testing::EmptyTestEventListener { |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame] | 87 | virtual void OnTestStart(const ::testing::TestInfo & /*test_info*/) { |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 88 | TestLogImplementation::GetInstance()->ClearMessages(); |
| 89 | } |
| 90 | virtual void OnTestEnd(const ::testing::TestInfo &test_info) { |
| 91 | if (test_info.result()->Failed()) { |
Philipp Schrader | e41ed9d | 2015-03-15 22:57:13 +0000 | [diff] [blame] | 92 | 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] | 93 | test_info.name()); |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 94 | } |
| 95 | } |
Brian Silverman | b91a37d | 2013-03-09 17:54:30 -0800 | [diff] [blame] | 96 | |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 97 | virtual void OnTestPartResult(const ::testing::TestPartResult &result) { |
Brian Silverman | b91a37d | 2013-03-09 17:54:30 -0800 | [diff] [blame] | 98 | if (result.failed()) { |
| 99 | const char *failure_type = "unknown"; |
| 100 | switch (result.type()) { |
| 101 | case ::testing::TestPartResult::Type::kNonFatalFailure: |
| 102 | failure_type = "EXPECT"; |
| 103 | break; |
| 104 | case ::testing::TestPartResult::Type::kFatalFailure: |
| 105 | failure_type = "ASSERT"; |
| 106 | break; |
| 107 | case ::testing::TestPartResult::Type::kSuccess: |
James Kuszmaul | f4bf9fe | 2021-05-10 22:58:24 -0700 | [diff] [blame^] | 108 | case ::testing::TestPartResult::Type::kSkip: |
Brian Silverman | b91a37d | 2013-03-09 17:54:30 -0800 | [diff] [blame] | 109 | break; |
| 110 | } |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 111 | log_do(ERROR, "%s: %d: gtest %s failure\n%s\n", result.file_name(), |
| 112 | result.line_number(), failure_type, result.message()); |
Brian Silverman | b91a37d | 2013-03-09 17:54:30 -0800 | [diff] [blame] | 113 | } |
| 114 | } |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | void *DoEnableTestLogging() { |
Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame] | 118 | logging::SetImplementation(TestLogImplementation::GetInstance()); |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 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 | |
John Park | b5e4730 | 2020-01-08 19:58:18 -0800 | [diff] [blame] | 126 | static absl::once_flag enable_test_logging_once; |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 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() { |
John Park | b5e4730 | 2020-01-08 19:58:18 -0800 | [diff] [blame] | 131 | absl::call_once(enable_test_logging_once, DoEnableTestLogging); |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 134 | void SetLogFileName(const char *filename) { |
Philipp Schrader | e41ed9d | 2015-03-15 22:57:13 +0000 | [diff] [blame] | 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 |