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