Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame] | 1 | #include "aos/logging/implementations.h" |
| 2 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 3 | #include <chrono> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 4 | #include <cinttypes> |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 5 | #include <string> |
| 6 | |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 7 | #include "glog/logging.h" |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 8 | #include "gtest/gtest.h" |
| 9 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 10 | #include "aos/logging/printf_formats.h" |
| 11 | #include "aos/time/time.h" |
| 12 | |
Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame] | 13 | using ::testing::AssertionFailure; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 14 | using ::testing::AssertionResult; |
| 15 | using ::testing::AssertionSuccess; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 16 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame^] | 17 | namespace aos::logging::testing { |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 18 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 19 | namespace chrono = ::std::chrono; |
| 20 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 21 | class TestLogImplementation : public LogImplementation { |
| 22 | virtual ::aos::monotonic_clock::time_point monotonic_now() const { |
| 23 | return ::aos::monotonic_clock::now(); |
| 24 | } |
| 25 | |
Austin Schuh | ad9e5eb | 2021-11-19 20:33:55 -0800 | [diff] [blame] | 26 | std::string_view MyName() override { |
| 27 | internal::Context *context = internal::Context::Get(); |
| 28 | return context->MyName(); |
| 29 | } |
| 30 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 31 | __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 0))) void DoLog( |
| 32 | log_level level, const char *format, va_list ap) override { |
Austin Schuh | ad9e5eb | 2021-11-19 20:33:55 -0800 | [diff] [blame] | 33 | internal::FillInMessage(level, MyName(), monotonic_now(), format, ap, |
| 34 | &message_); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 35 | |
Brian Silverman | fe457de | 2014-05-26 22:04:08 -0700 | [diff] [blame] | 36 | if (level == FATAL) { |
| 37 | internal::PrintMessage(stderr, message_); |
| 38 | abort(); |
| 39 | } |
| 40 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 41 | used_ = true; |
| 42 | } |
| 43 | |
| 44 | LogMessage message_; |
| 45 | |
| 46 | public: |
| 47 | const LogMessage &message() { return message_; } |
| 48 | bool used() { return used_; } |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 49 | |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 50 | bool used_ = false; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 51 | }; |
| 52 | class LoggingTest : public ::testing::Test { |
| 53 | protected: |
| 54 | AssertionResult WasAnythingLogged() { |
| 55 | if (log_implementation->used()) { |
Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame] | 56 | return AssertionSuccess() << "read message '" |
| 57 | << log_implementation->message().message << "'"; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 58 | } |
| 59 | return AssertionFailure(); |
| 60 | } |
| 61 | AssertionResult WasLogged(log_level level, const std::string message) { |
| 62 | if (!log_implementation->used()) { |
| 63 | return AssertionFailure() << "nothing was logged"; |
| 64 | } |
| 65 | if (log_implementation->message().level != level) { |
Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame] | 66 | return AssertionFailure() << "a message with level " |
| 67 | << log_str(log_implementation->message().level) |
| 68 | << " was logged instead of " << log_str(level); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 69 | } |
| 70 | internal::Context *context = internal::Context::Get(); |
| 71 | if (log_implementation->message().source != context->source) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 72 | AOS_LOG(FATAL, "got a message from %" PRIu32 ", but we're %" PRIu32 "\n", |
| 73 | static_cast<uint32_t>(log_implementation->message().source), |
| 74 | static_cast<uint32_t>(context->source)); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 75 | } |
Austin Schuh | ad9e5eb | 2021-11-19 20:33:55 -0800 | [diff] [blame] | 76 | if (log_implementation->message().name_length != context->MyName().size() || |
| 77 | memcmp(log_implementation->message().name, context->MyName().data(), |
| 78 | context->MyName().size()) != 0) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 79 | AOS_LOG(FATAL, "got a message from %.*s, but we're %s\n", |
| 80 | static_cast<int>(log_implementation->message().name_length), |
Austin Schuh | ad9e5eb | 2021-11-19 20:33:55 -0800 | [diff] [blame] | 81 | log_implementation->message().name, |
| 82 | std::string(context->MyName()).c_str()); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 83 | } |
Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame] | 84 | if (strstr(log_implementation->message().message, message.c_str()) == |
| 85 | NULL) { |
| 86 | return AssertionFailure() |
| 87 | << "got a message of '" << log_implementation->message().message |
| 88 | << "' but expected it to contain '" << message << "'"; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | return AssertionSuccess() << log_implementation->message().message; |
| 92 | } |
| 93 | |
| 94 | private: |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 95 | void SetUp() override { |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 96 | log_implementation = std::make_shared<TestLogImplementation>(); |
| 97 | SetImplementation(log_implementation); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 98 | } |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 99 | void TearDown() override { |
| 100 | SetImplementation(nullptr); |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 101 | internal::Context::DeleteNow(); |
| 102 | CHECK_EQ(log_implementation.use_count(), 1); |
| 103 | log_implementation.reset(); |
| 104 | } |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 105 | |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 106 | std::shared_ptr<TestLogImplementation> log_implementation; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 107 | }; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 108 | typedef LoggingTest LoggingDeathTest; |
| 109 | |
| 110 | // Tests both basic logging functionality and that the test setup works |
| 111 | // correctly. |
| 112 | TEST_F(LoggingTest, Basic) { |
| 113 | EXPECT_FALSE(WasAnythingLogged()); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 114 | AOS_LOG(INFO, "test log 1\n"); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 115 | EXPECT_TRUE(WasLogged(INFO, "test log 1\n")); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 116 | AOS_LOG(INFO, "test log 1.5\n"); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 117 | // there's a subtle typo on purpose... |
| 118 | EXPECT_FALSE(WasLogged(INFO, "test log 15\n")); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 119 | AOS_LOG(ERROR, "test log 2=%d\n", 55); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 120 | EXPECT_TRUE(WasLogged(ERROR, "test log 2=55\n")); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 121 | AOS_LOG(ERROR, "test log 3\n"); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 122 | EXPECT_FALSE(WasLogged(WARNING, "test log 3\n")); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 123 | AOS_LOG(WARNING, "test log 4\n"); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 124 | EXPECT_TRUE(WasAnythingLogged()); |
| 125 | } |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 126 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 127 | TEST_F(LoggingDeathTest, Fatal) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 128 | ASSERT_EXIT(AOS_LOG(FATAL, "this should crash it\n"), |
| 129 | ::testing::KilledBySignal(SIGABRT), "this should crash it"); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 130 | } |
Brian Silverman | fe457de | 2014-05-26 22:04:08 -0700 | [diff] [blame] | 131 | |
| 132 | TEST_F(LoggingDeathTest, PCHECK) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 133 | EXPECT_DEATH(AOS_PCHECK(fprintf(stdin, "nope")), |
Brian Silverman | fe457de | 2014-05-26 22:04:08 -0700 | [diff] [blame] | 134 | ".*fprintf\\(stdin, \"nope\"\\).*failed.*"); |
| 135 | } |
| 136 | |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 137 | TEST_F(LoggingTest, PCHECK) { EXPECT_EQ(7, AOS_PCHECK(printf("abc123\n"))); } |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 138 | |
| 139 | TEST_F(LoggingTest, PrintfDirectives) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 140 | AOS_LOG(INFO, "test log %%1 %%d\n"); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 141 | EXPECT_TRUE(WasLogged(INFO, "test log %1 %d\n")); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 142 | AOS_LOG_DYNAMIC(WARNING, "test log %%2 %%f\n"); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 143 | EXPECT_TRUE(WasLogged(WARNING, "test log %2 %f\n")); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | TEST_F(LoggingTest, Timing) { |
| 147 | // For writing only. |
Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame] | 148 | // static const long kTimingCycles = 5000000; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 149 | static const long kTimingCycles = 5000; |
| 150 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 151 | monotonic_clock::time_point start = monotonic_clock::now(); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 152 | for (long i = 0; i < kTimingCycles; ++i) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 153 | AOS_LOG(INFO, "a\n"); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 154 | } |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 155 | monotonic_clock::time_point end = monotonic_clock::now(); |
| 156 | auto diff = end - start; |
Brian Silverman | 41abe01 | 2014-02-08 18:25:02 -0800 | [diff] [blame] | 157 | printf("short message took %" PRId64 " nsec for %ld\n", |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 158 | static_cast<int64_t>( |
| 159 | chrono::duration_cast<chrono::nanoseconds>(diff).count()), |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 160 | kTimingCycles); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 161 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 162 | start = monotonic_clock::now(); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 163 | for (long i = 0; i < kTimingCycles; ++i) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 164 | AOS_LOG(INFO, "something longer than just \"a\" to log to test timing\n"); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 165 | } |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 166 | end = monotonic_clock::now(); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 167 | diff = end - start; |
Brian Silverman | 41abe01 | 2014-02-08 18:25:02 -0800 | [diff] [blame] | 168 | printf("long message took %" PRId64 " nsec for %ld\n", |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 169 | static_cast<int64_t>( |
| 170 | chrono::duration_cast<chrono::nanoseconds>(diff).count()), |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 171 | kTimingCycles); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Brian Silverman | 54a368e | 2015-02-14 20:05:33 -0500 | [diff] [blame] | 174 | TEST(LoggingPrintFormatTest, Time) { |
| 175 | char buffer[1024]; |
| 176 | |
| 177 | // Easy ones. |
| 178 | ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT, |
| 179 | AOS_TIME_ARGS(2, 0))); |
| 180 | EXPECT_EQ("0000000002.000000s", ::std::string(buffer)); |
| 181 | ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT, |
| 182 | AOS_TIME_ARGS(2, 1))); |
| 183 | EXPECT_EQ("0000000002.000000s", ::std::string(buffer)); |
| 184 | |
| 185 | // This one should be exact. |
| 186 | ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT, |
| 187 | AOS_TIME_ARGS(2, 1000))); |
| 188 | EXPECT_EQ("0000000002.000001s", ::std::string(buffer)); |
| 189 | |
| 190 | // Make sure rounding works correctly. |
| 191 | ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT, |
| 192 | AOS_TIME_ARGS(2, 999))); |
Austin Schuh | 648b361 | 2017-11-20 01:02:24 -0800 | [diff] [blame] | 193 | EXPECT_EQ("0000000002.000000s", ::std::string(buffer)); |
Brian Silverman | 54a368e | 2015-02-14 20:05:33 -0500 | [diff] [blame] | 194 | ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT, |
| 195 | AOS_TIME_ARGS(2, 500))); |
Austin Schuh | 648b361 | 2017-11-20 01:02:24 -0800 | [diff] [blame] | 196 | EXPECT_EQ("0000000002.000000s", ::std::string(buffer)); |
Brian Silverman | 54a368e | 2015-02-14 20:05:33 -0500 | [diff] [blame] | 197 | ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT, |
| 198 | AOS_TIME_ARGS(2, 499))); |
| 199 | EXPECT_EQ("0000000002.000000s", ::std::string(buffer)); |
| 200 | |
| 201 | // This used to result in "0000000001.099500s". |
| 202 | ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT, |
| 203 | AOS_TIME_ARGS(1, 995000000))); |
| 204 | EXPECT_EQ("0000000001.995000s", ::std::string(buffer)); |
Austin Schuh | 648b361 | 2017-11-20 01:02:24 -0800 | [diff] [blame] | 205 | |
| 206 | // This used to result in "0000000001.099500s". |
| 207 | ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT, |
| 208 | AOS_TIME_ARGS(1, 999999999))); |
| 209 | EXPECT_EQ("0000000001.999999s", ::std::string(buffer)); |
Brian Silverman | 54a368e | 2015-02-14 20:05:33 -0500 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | TEST(LoggingPrintFormatTest, Base) { |
| 213 | char buffer[1024]; |
| 214 | |
| 215 | static const ::std::string kExpected1 = |
| 216 | "name(971)(01678): ERROR at 0000000001.995000s: "; |
| 217 | ASSERT_GT(sizeof(buffer), kExpected1.size()); |
| 218 | ASSERT_EQ( |
| 219 | kExpected1.size(), |
| 220 | static_cast<size_t>(snprintf( |
| 221 | buffer, sizeof(buffer), AOS_LOGGING_BASE_FORMAT, |
| 222 | AOS_LOGGING_BASE_ARGS(4, "name", 971, 1678, ERROR, 1, 995000000)))); |
| 223 | EXPECT_EQ(kExpected1, ::std::string(buffer)); |
| 224 | } |
| 225 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 226 | TEST(ScopedLogRestorerTest, RestoreTest) { |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 227 | SetImplementation(std::make_shared<StreamLogImplementation>(stdout)); |
| 228 | LogImplementation *curr_impl = GetImplementation().get(); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 229 | |
Austin Schuh | ad9e5eb | 2021-11-19 20:33:55 -0800 | [diff] [blame] | 230 | std::string name = "name"; |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 231 | { |
| 232 | ScopedLogRestorer log_restorer; |
| 233 | |
Austin Schuh | ad9e5eb | 2021-11-19 20:33:55 -0800 | [diff] [blame] | 234 | log_restorer.Swap(std::make_shared<CallbackLogImplementation>( |
| 235 | [](const LogMessage &) {}, &name)); |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 236 | ASSERT_NE(curr_impl, GetImplementation().get()); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 239 | ASSERT_EQ(curr_impl, GetImplementation().get()); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 240 | } |
| 241 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame^] | 242 | } // namespace aos::logging::testing |