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