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_; } |
| 44 | void reset_used() { used_ = false; } |
| 45 | |
| 46 | TestLogImplementation() : used_(false) {} |
| 47 | |
| 48 | bool used_; |
| 49 | }; |
| 50 | class LoggingTest : public ::testing::Test { |
| 51 | protected: |
| 52 | AssertionResult WasAnythingLogged() { |
| 53 | if (log_implementation->used()) { |
Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame] | 54 | return AssertionSuccess() << "read message '" |
| 55 | << log_implementation->message().message << "'"; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 56 | } |
| 57 | return AssertionFailure(); |
| 58 | } |
| 59 | AssertionResult WasLogged(log_level level, const std::string message) { |
| 60 | if (!log_implementation->used()) { |
| 61 | return AssertionFailure() << "nothing was logged"; |
| 62 | } |
| 63 | if (log_implementation->message().level != level) { |
Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame] | 64 | return AssertionFailure() << "a message with level " |
| 65 | << log_str(log_implementation->message().level) |
| 66 | << " was logged instead of " << log_str(level); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 67 | } |
| 68 | internal::Context *context = internal::Context::Get(); |
| 69 | if (log_implementation->message().source != context->source) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 70 | AOS_LOG(FATAL, "got a message from %" PRIu32 ", but we're %" PRIu32 "\n", |
| 71 | static_cast<uint32_t>(log_implementation->message().source), |
| 72 | static_cast<uint32_t>(context->source)); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 73 | } |
Austin Schuh | aebbc34 | 2015-01-25 02:25:13 -0800 | [diff] [blame] | 74 | if (log_implementation->message().name_length != context->name_size || |
| 75 | memcmp(log_implementation->message().name, context->name, |
Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame] | 76 | context->name_size) != 0) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 77 | AOS_LOG(FATAL, "got a message from %.*s, but we're %s\n", |
| 78 | static_cast<int>(log_implementation->message().name_length), |
| 79 | log_implementation->message().name, context->name); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 80 | } |
Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame] | 81 | if (strstr(log_implementation->message().message, message.c_str()) == |
| 82 | NULL) { |
| 83 | return AssertionFailure() |
| 84 | << "got a message of '" << log_implementation->message().message |
| 85 | << "' but expected it to contain '" << message << "'"; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | return AssertionSuccess() << log_implementation->message().message; |
| 89 | } |
| 90 | |
| 91 | private: |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 92 | void SetUp() override { |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 93 | static bool first = true; |
| 94 | if (first) { |
| 95 | first = false; |
| 96 | |
| 97 | Init(); |
Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame] | 98 | SetImplementation(log_implementation = new TestLogImplementation()); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | log_implementation->reset_used(); |
| 102 | } |
Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame] | 103 | void TearDown() override { Cleanup(); } |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 104 | |
| 105 | static TestLogImplementation *log_implementation; |
| 106 | }; |
| 107 | TestLogImplementation *LoggingTest::log_implementation(NULL); |
| 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 | } |
| 126 | TEST_F(LoggingTest, Cork) { |
| 127 | static const int begin_line = __LINE__; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 128 | AOS_LOG_CORK("first part "); |
| 129 | AOS_LOG_CORK("second part (=%d) ", 19); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 130 | EXPECT_FALSE(WasAnythingLogged()); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 131 | AOS_LOG_CORK("third part "); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 132 | static const int end_line = __LINE__; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 133 | AOS_LOG_UNCORK(WARNING, "last part %d\n", 5); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 134 | std::stringstream expected; |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 135 | expected << "implementations_test.cc: "; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 136 | expected << (begin_line + 1); |
| 137 | expected << "-"; |
| 138 | expected << (end_line + 1); |
| 139 | expected << ": "; |
Brian Silverman | c0a0d11 | 2013-09-19 21:08:49 -0700 | [diff] [blame] | 140 | expected << __func__; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 141 | expected << ": first part second part (=19) third part last part 5\n"; |
| 142 | EXPECT_TRUE(WasLogged(WARNING, expected.str())); |
| 143 | } |
| 144 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 145 | TEST_F(LoggingDeathTest, Fatal) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 146 | ASSERT_EXIT(AOS_LOG(FATAL, "this should crash it\n"), |
| 147 | ::testing::KilledBySignal(SIGABRT), "this should crash it"); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 148 | } |
Brian Silverman | fe457de | 2014-05-26 22:04:08 -0700 | [diff] [blame] | 149 | |
| 150 | TEST_F(LoggingDeathTest, PCHECK) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 151 | EXPECT_DEATH(AOS_PCHECK(fprintf(stdin, "nope")), |
Brian Silverman | fe457de | 2014-05-26 22:04:08 -0700 | [diff] [blame] | 152 | ".*fprintf\\(stdin, \"nope\"\\).*failed.*"); |
| 153 | } |
| 154 | |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 155 | TEST_F(LoggingTest, PCHECK) { EXPECT_EQ(7, AOS_PCHECK(printf("abc123\n"))); } |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 156 | |
| 157 | TEST_F(LoggingTest, PrintfDirectives) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 158 | AOS_LOG(INFO, "test log %%1 %%d\n"); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 159 | EXPECT_TRUE(WasLogged(INFO, "test log %1 %d\n")); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 160 | AOS_LOG_DYNAMIC(WARNING, "test log %%2 %%f\n"); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 161 | EXPECT_TRUE(WasLogged(WARNING, "test log %2 %f\n")); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 162 | AOS_LOG_CORK("log 3 part %%1 %%d "); |
| 163 | AOS_LOG_UNCORK(DEBUG, "log 3 part %%2 %%f\n"); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 164 | EXPECT_TRUE(WasLogged(DEBUG, "log 3 part %1 %d log 3 part %2 %f\n")); |
| 165 | } |
| 166 | |
| 167 | TEST_F(LoggingTest, Timing) { |
| 168 | // For writing only. |
Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame] | 169 | // static const long kTimingCycles = 5000000; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 170 | static const long kTimingCycles = 5000; |
| 171 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 172 | monotonic_clock::time_point start = monotonic_clock::now(); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 173 | for (long i = 0; i < kTimingCycles; ++i) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 174 | AOS_LOG(INFO, "a\n"); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 175 | } |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 176 | monotonic_clock::time_point end = monotonic_clock::now(); |
| 177 | auto diff = end - start; |
Brian Silverman | 41abe01 | 2014-02-08 18:25:02 -0800 | [diff] [blame] | 178 | printf("short message took %" PRId64 " nsec for %ld\n", |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 179 | static_cast<int64_t>( |
| 180 | chrono::duration_cast<chrono::nanoseconds>(diff).count()), |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 181 | kTimingCycles); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 182 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 183 | start = monotonic_clock::now(); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 184 | for (long i = 0; i < kTimingCycles; ++i) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 185 | 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] | 186 | } |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 187 | end = monotonic_clock::now(); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 188 | diff = end - start; |
Brian Silverman | 41abe01 | 2014-02-08 18:25:02 -0800 | [diff] [blame] | 189 | printf("long message took %" PRId64 " nsec for %ld\n", |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 190 | static_cast<int64_t>( |
| 191 | chrono::duration_cast<chrono::nanoseconds>(diff).count()), |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 192 | kTimingCycles); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Brian Silverman | 54a368e | 2015-02-14 20:05:33 -0500 | [diff] [blame] | 195 | TEST(LoggingPrintFormatTest, Time) { |
| 196 | char buffer[1024]; |
| 197 | |
| 198 | // Easy ones. |
| 199 | ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT, |
| 200 | AOS_TIME_ARGS(2, 0))); |
| 201 | EXPECT_EQ("0000000002.000000s", ::std::string(buffer)); |
| 202 | ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT, |
| 203 | AOS_TIME_ARGS(2, 1))); |
| 204 | EXPECT_EQ("0000000002.000000s", ::std::string(buffer)); |
| 205 | |
| 206 | // This one should be exact. |
| 207 | ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT, |
| 208 | AOS_TIME_ARGS(2, 1000))); |
| 209 | EXPECT_EQ("0000000002.000001s", ::std::string(buffer)); |
| 210 | |
| 211 | // Make sure rounding works correctly. |
| 212 | ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT, |
| 213 | AOS_TIME_ARGS(2, 999))); |
Austin Schuh | 648b361 | 2017-11-20 01:02:24 -0800 | [diff] [blame] | 214 | EXPECT_EQ("0000000002.000000s", ::std::string(buffer)); |
Brian Silverman | 54a368e | 2015-02-14 20:05:33 -0500 | [diff] [blame] | 215 | ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT, |
| 216 | AOS_TIME_ARGS(2, 500))); |
Austin Schuh | 648b361 | 2017-11-20 01:02:24 -0800 | [diff] [blame] | 217 | EXPECT_EQ("0000000002.000000s", ::std::string(buffer)); |
Brian Silverman | 54a368e | 2015-02-14 20:05:33 -0500 | [diff] [blame] | 218 | ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT, |
| 219 | AOS_TIME_ARGS(2, 499))); |
| 220 | EXPECT_EQ("0000000002.000000s", ::std::string(buffer)); |
| 221 | |
| 222 | // This used to result in "0000000001.099500s". |
| 223 | ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT, |
| 224 | AOS_TIME_ARGS(1, 995000000))); |
| 225 | EXPECT_EQ("0000000001.995000s", ::std::string(buffer)); |
Austin Schuh | 648b361 | 2017-11-20 01:02:24 -0800 | [diff] [blame] | 226 | |
| 227 | // This used to result in "0000000001.099500s". |
| 228 | ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT, |
| 229 | AOS_TIME_ARGS(1, 999999999))); |
| 230 | EXPECT_EQ("0000000001.999999s", ::std::string(buffer)); |
Brian Silverman | 54a368e | 2015-02-14 20:05:33 -0500 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | TEST(LoggingPrintFormatTest, Base) { |
| 234 | char buffer[1024]; |
| 235 | |
| 236 | static const ::std::string kExpected1 = |
| 237 | "name(971)(01678): ERROR at 0000000001.995000s: "; |
| 238 | ASSERT_GT(sizeof(buffer), kExpected1.size()); |
| 239 | ASSERT_EQ( |
| 240 | kExpected1.size(), |
| 241 | static_cast<size_t>(snprintf( |
| 242 | buffer, sizeof(buffer), AOS_LOGGING_BASE_FORMAT, |
| 243 | AOS_LOGGING_BASE_ARGS(4, "name", 971, 1678, ERROR, 1, 995000000)))); |
| 244 | EXPECT_EQ(kExpected1, ::std::string(buffer)); |
| 245 | } |
| 246 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 247 | } // namespace testing |
| 248 | } // namespace logging |
| 249 | } // namespace aos |