Brian Silverman | fe457de | 2014-05-26 22:04:08 -0700 | [diff] [blame] | 1 | #include <inttypes.h> |
| 2 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 3 | #include <chrono> |
| 4 | #include <string> |
| 5 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 6 | #include "gtest/gtest.h" |
| 7 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 8 | #include "aos/logging/implementations.h" |
| 9 | #include "aos/time/time.h" |
| 10 | #include "aos/logging/printf_formats.h" |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 11 | |
| 12 | using ::testing::AssertionResult; |
| 13 | using ::testing::AssertionSuccess; |
| 14 | using ::testing::AssertionFailure; |
| 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 | |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 22 | class TestLogImplementation : public SimpleLogImplementation { |
Brian Silverman | f798614 | 2014-04-21 17:42:35 -0700 | [diff] [blame] | 23 | __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 0))) |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 24 | void DoLog(log_level level, const char *format, va_list ap) override { |
Austin Schuh | 82c0c82 | 2019-05-27 19:55:20 -0700 | [diff] [blame^] | 25 | internal::FillInMessage(level, monotonic_now(), format, ap, &message_); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 26 | |
Brian Silverman | fe457de | 2014-05-26 22:04:08 -0700 | [diff] [blame] | 27 | if (level == FATAL) { |
| 28 | internal::PrintMessage(stderr, message_); |
| 29 | abort(); |
| 30 | } |
| 31 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 32 | used_ = true; |
| 33 | } |
| 34 | |
| 35 | LogMessage message_; |
| 36 | |
| 37 | public: |
| 38 | const LogMessage &message() { return message_; } |
| 39 | bool used() { return used_; } |
| 40 | void reset_used() { used_ = false; } |
| 41 | |
| 42 | TestLogImplementation() : used_(false) {} |
| 43 | |
| 44 | bool used_; |
| 45 | }; |
| 46 | class LoggingTest : public ::testing::Test { |
| 47 | protected: |
| 48 | AssertionResult WasAnythingLogged() { |
| 49 | if (log_implementation->used()) { |
| 50 | return AssertionSuccess() << "read message '" << |
| 51 | log_implementation->message().message << "'"; |
| 52 | } |
| 53 | return AssertionFailure(); |
| 54 | } |
| 55 | AssertionResult WasLogged(log_level level, const std::string message) { |
| 56 | if (!log_implementation->used()) { |
| 57 | return AssertionFailure() << "nothing was logged"; |
| 58 | } |
| 59 | if (log_implementation->message().level != level) { |
| 60 | return AssertionFailure() << "a message with level " << |
| 61 | log_str(log_implementation->message().level) << |
| 62 | " was logged instead of " << log_str(level); |
| 63 | } |
| 64 | internal::Context *context = internal::Context::Get(); |
| 65 | if (log_implementation->message().source != context->source) { |
Brian Silverman | f0bb837 | 2014-04-30 15:58:55 -0700 | [diff] [blame] | 66 | LOG(FATAL, "got a message from %" PRIu32 ", but we're %" PRIu32 "\n", |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 67 | static_cast<uint32_t>(log_implementation->message().source), |
| 68 | static_cast<uint32_t>(context->source)); |
| 69 | } |
Austin Schuh | aebbc34 | 2015-01-25 02:25:13 -0800 | [diff] [blame] | 70 | if (log_implementation->message().name_length != context->name_size || |
| 71 | memcmp(log_implementation->message().name, context->name, |
| 72 | context->name_size) != |
Brian Silverman | f0bb837 | 2014-04-30 15:58:55 -0700 | [diff] [blame] | 73 | 0) { |
| 74 | LOG(FATAL, "got a message from %.*s, but we're %s\n", |
| 75 | static_cast<int>(log_implementation->message().name_length), |
Austin Schuh | aebbc34 | 2015-01-25 02:25:13 -0800 | [diff] [blame] | 76 | log_implementation->message().name, context->name); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 77 | } |
| 78 | if (strstr(log_implementation->message().message, message.c_str()) |
| 79 | == NULL) { |
| 80 | return AssertionFailure() << "got a message of '" << |
| 81 | log_implementation->message().message << |
| 82 | "' but expected it to contain '" << message << "'"; |
| 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 { |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 90 | static bool first = true; |
| 91 | if (first) { |
| 92 | first = false; |
| 93 | |
| 94 | Init(); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 95 | AddImplementation(log_implementation = new TestLogImplementation()); |
| 96 | } |
| 97 | |
| 98 | log_implementation->reset_used(); |
| 99 | } |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 100 | void TearDown() override { |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 101 | Cleanup(); |
| 102 | } |
| 103 | |
| 104 | static TestLogImplementation *log_implementation; |
| 105 | }; |
| 106 | TestLogImplementation *LoggingTest::log_implementation(NULL); |
| 107 | typedef LoggingTest LoggingDeathTest; |
| 108 | |
| 109 | // Tests both basic logging functionality and that the test setup works |
| 110 | // correctly. |
| 111 | TEST_F(LoggingTest, Basic) { |
| 112 | EXPECT_FALSE(WasAnythingLogged()); |
| 113 | LOG(INFO, "test log 1\n"); |
| 114 | EXPECT_TRUE(WasLogged(INFO, "test log 1\n")); |
| 115 | LOG(INFO, "test log 1.5\n"); |
| 116 | // there's a subtle typo on purpose... |
| 117 | EXPECT_FALSE(WasLogged(INFO, "test log 15\n")); |
| 118 | LOG(ERROR, "test log 2=%d\n", 55); |
| 119 | EXPECT_TRUE(WasLogged(ERROR, "test log 2=55\n")); |
| 120 | LOG(ERROR, "test log 3\n"); |
| 121 | EXPECT_FALSE(WasLogged(WARNING, "test log 3\n")); |
| 122 | LOG(WARNING, "test log 4\n"); |
| 123 | EXPECT_TRUE(WasAnythingLogged()); |
| 124 | } |
| 125 | TEST_F(LoggingTest, Cork) { |
| 126 | static const int begin_line = __LINE__; |
| 127 | LOG_CORK("first part "); |
| 128 | LOG_CORK("second part (=%d) ", 19); |
| 129 | EXPECT_FALSE(WasAnythingLogged()); |
| 130 | LOG_CORK("third part "); |
| 131 | static const int end_line = __LINE__; |
| 132 | LOG_UNCORK(WARNING, "last part %d\n", 5); |
| 133 | std::stringstream expected; |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 134 | expected << "implementations_test.cc: "; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 135 | expected << (begin_line + 1); |
| 136 | expected << "-"; |
| 137 | expected << (end_line + 1); |
| 138 | expected << ": "; |
Brian Silverman | c0a0d11 | 2013-09-19 21:08:49 -0700 | [diff] [blame] | 139 | expected << __func__; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 140 | expected << ": first part second part (=19) third part last part 5\n"; |
| 141 | EXPECT_TRUE(WasLogged(WARNING, expected.str())); |
| 142 | } |
| 143 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 144 | TEST_F(LoggingDeathTest, Fatal) { |
| 145 | ASSERT_EXIT(LOG(FATAL, "this should crash it\n"), |
| 146 | ::testing::KilledBySignal(SIGABRT), |
| 147 | "this should crash it"); |
| 148 | } |
Brian Silverman | fe457de | 2014-05-26 22:04:08 -0700 | [diff] [blame] | 149 | |
| 150 | TEST_F(LoggingDeathTest, PCHECK) { |
| 151 | EXPECT_DEATH(PCHECK(fprintf(stdin, "nope")), |
| 152 | ".*fprintf\\(stdin, \"nope\"\\).*failed.*"); |
| 153 | } |
| 154 | |
| 155 | TEST_F(LoggingTest, PCHECK) { |
| 156 | EXPECT_EQ(7, PCHECK(printf("abc123\n"))); |
| 157 | } |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 158 | |
| 159 | TEST_F(LoggingTest, PrintfDirectives) { |
| 160 | LOG(INFO, "test log %%1 %%d\n"); |
| 161 | EXPECT_TRUE(WasLogged(INFO, "test log %1 %d\n")); |
| 162 | LOG_DYNAMIC(WARNING, "test log %%2 %%f\n"); |
| 163 | EXPECT_TRUE(WasLogged(WARNING, "test log %2 %f\n")); |
| 164 | LOG_CORK("log 3 part %%1 %%d "); |
| 165 | LOG_UNCORK(DEBUG, "log 3 part %%2 %%f\n"); |
| 166 | EXPECT_TRUE(WasLogged(DEBUG, "log 3 part %1 %d log 3 part %2 %f\n")); |
| 167 | } |
| 168 | |
| 169 | TEST_F(LoggingTest, Timing) { |
| 170 | // For writing only. |
| 171 | //static const long kTimingCycles = 5000000; |
| 172 | static const long kTimingCycles = 5000; |
| 173 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 174 | monotonic_clock::time_point start = monotonic_clock::now(); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 175 | for (long i = 0; i < kTimingCycles; ++i) { |
| 176 | LOG(INFO, "a\n"); |
| 177 | } |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 178 | monotonic_clock::time_point end = monotonic_clock::now(); |
| 179 | auto diff = end - start; |
Brian Silverman | 41abe01 | 2014-02-08 18:25:02 -0800 | [diff] [blame] | 180 | printf("short message took %" PRId64 " nsec for %ld\n", |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 181 | chrono::duration_cast<chrono::nanoseconds>(diff).count(), |
| 182 | kTimingCycles); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 183 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 184 | start = monotonic_clock::now(); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 185 | for (long i = 0; i < kTimingCycles; ++i) { |
| 186 | LOG(INFO, "something longer than just \"a\" to log to test timing\n"); |
| 187 | } |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 188 | end = monotonic_clock::now(); |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 189 | diff = end - start; |
Brian Silverman | 41abe01 | 2014-02-08 18:25:02 -0800 | [diff] [blame] | 190 | printf("long message took %" PRId64 " nsec for %ld\n", |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 191 | chrono::duration_cast<chrono::nanoseconds>(diff).count(), |
| 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 |