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