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