blob: d97e1651717167843ad5fbfacabbab7b0c00b93d [file] [log] [blame]
Brian Silvermanfe457de2014-05-26 22:04:08 -07001#include <inttypes.h>
2
Austin Schuhf2a50ba2016-12-24 16:16:26 -08003#include <chrono>
4#include <string>
5
Brian Silvermanf665d692013-02-17 22:11:39 -08006#include "gtest/gtest.h"
7
John Park33858a32018-09-28 23:05:48 -07008#include "aos/logging/implementations.h"
9#include "aos/time/time.h"
10#include "aos/logging/printf_formats.h"
Brian Silvermanf665d692013-02-17 22:11:39 -080011
12using ::testing::AssertionResult;
13using ::testing::AssertionSuccess;
14using ::testing::AssertionFailure;
15
16namespace aos {
17namespace logging {
18namespace testing {
19
Austin Schuhf2a50ba2016-12-24 16:16:26 -080020namespace chrono = ::std::chrono;
21
Alex Perrycb7da4b2019-08-28 19:35:56 -070022class 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 Schuh82c0c822019-05-27 19:55:20 -070029 internal::FillInMessage(level, monotonic_now(), format, ap, &message_);
Brian Silvermanf665d692013-02-17 22:11:39 -080030
Brian Silvermanfe457de2014-05-26 22:04:08 -070031 if (level == FATAL) {
32 internal::PrintMessage(stderr, message_);
33 abort();
34 }
35
Brian Silvermanf665d692013-02-17 22:11:39 -080036 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};
50class LoggingTest : public ::testing::Test {
51 protected:
52 AssertionResult WasAnythingLogged() {
53 if (log_implementation->used()) {
54 return AssertionSuccess() << "read message '" <<
55 log_implementation->message().message << "'";
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) {
64 return AssertionFailure() << "a message with level " <<
65 log_str(log_implementation->message().level) <<
66 " was logged instead of " << log_str(level);
67 }
68 internal::Context *context = internal::Context::Get();
69 if (log_implementation->message().source != context->source) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070070 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 Silvermanf665d692013-02-17 22:11:39 -080073 }
Austin Schuhaebbc342015-01-25 02:25:13 -080074 if (log_implementation->message().name_length != context->name_size ||
75 memcmp(log_implementation->message().name, context->name,
76 context->name_size) !=
Brian Silvermanf0bb8372014-04-30 15:58:55 -070077 0) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070078 AOS_LOG(FATAL, "got a message from %.*s, but we're %s\n",
79 static_cast<int>(log_implementation->message().name_length),
80 log_implementation->message().name, context->name);
Brian Silvermanf665d692013-02-17 22:11:39 -080081 }
82 if (strstr(log_implementation->message().message, message.c_str())
83 == NULL) {
84 return AssertionFailure() << "got a message of '" <<
85 log_implementation->message().message <<
86 "' but expected it to contain '" << message << "'";
87 }
88
89 return AssertionSuccess() << log_implementation->message().message;
90 }
91
92 private:
Brian Silvermancb5da1f2015-12-05 22:19:58 -050093 void SetUp() override {
Brian Silvermanf665d692013-02-17 22:11:39 -080094 static bool first = true;
95 if (first) {
96 first = false;
97
98 Init();
Brian Silvermanf665d692013-02-17 22:11:39 -080099 AddImplementation(log_implementation = new TestLogImplementation());
100 }
101
102 log_implementation->reset_used();
103 }
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500104 void TearDown() override {
Brian Silvermanf665d692013-02-17 22:11:39 -0800105 Cleanup();
106 }
107
108 static TestLogImplementation *log_implementation;
109};
110TestLogImplementation *LoggingTest::log_implementation(NULL);
111typedef LoggingTest LoggingDeathTest;
112
113// Tests both basic logging functionality and that the test setup works
114// correctly.
115TEST_F(LoggingTest, Basic) {
116 EXPECT_FALSE(WasAnythingLogged());
Austin Schuhf257f3c2019-10-27 21:00:43 -0700117 AOS_LOG(INFO, "test log 1\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800118 EXPECT_TRUE(WasLogged(INFO, "test log 1\n"));
Austin Schuhf257f3c2019-10-27 21:00:43 -0700119 AOS_LOG(INFO, "test log 1.5\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800120 // there's a subtle typo on purpose...
121 EXPECT_FALSE(WasLogged(INFO, "test log 15\n"));
Austin Schuhf257f3c2019-10-27 21:00:43 -0700122 AOS_LOG(ERROR, "test log 2=%d\n", 55);
Brian Silvermanf665d692013-02-17 22:11:39 -0800123 EXPECT_TRUE(WasLogged(ERROR, "test log 2=55\n"));
Austin Schuhf257f3c2019-10-27 21:00:43 -0700124 AOS_LOG(ERROR, "test log 3\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800125 EXPECT_FALSE(WasLogged(WARNING, "test log 3\n"));
Austin Schuhf257f3c2019-10-27 21:00:43 -0700126 AOS_LOG(WARNING, "test log 4\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800127 EXPECT_TRUE(WasAnythingLogged());
128}
129TEST_F(LoggingTest, Cork) {
130 static const int begin_line = __LINE__;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700131 AOS_LOG_CORK("first part ");
132 AOS_LOG_CORK("second part (=%d) ", 19);
Brian Silvermanf665d692013-02-17 22:11:39 -0800133 EXPECT_FALSE(WasAnythingLogged());
Austin Schuhf257f3c2019-10-27 21:00:43 -0700134 AOS_LOG_CORK("third part ");
Brian Silvermanf665d692013-02-17 22:11:39 -0800135 static const int end_line = __LINE__;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700136 AOS_LOG_UNCORK(WARNING, "last part %d\n", 5);
Brian Silvermanf665d692013-02-17 22:11:39 -0800137 std::stringstream expected;
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500138 expected << "implementations_test.cc: ";
Brian Silvermanf665d692013-02-17 22:11:39 -0800139 expected << (begin_line + 1);
140 expected << "-";
141 expected << (end_line + 1);
142 expected << ": ";
Brian Silvermanc0a0d112013-09-19 21:08:49 -0700143 expected << __func__;
Brian Silvermanf665d692013-02-17 22:11:39 -0800144 expected << ": first part second part (=19) third part last part 5\n";
145 EXPECT_TRUE(WasLogged(WARNING, expected.str()));
146}
147
Brian Silvermanf665d692013-02-17 22:11:39 -0800148TEST_F(LoggingDeathTest, Fatal) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700149 ASSERT_EXIT(AOS_LOG(FATAL, "this should crash it\n"),
150 ::testing::KilledBySignal(SIGABRT), "this should crash it");
Brian Silvermanf665d692013-02-17 22:11:39 -0800151}
Brian Silvermanfe457de2014-05-26 22:04:08 -0700152
153TEST_F(LoggingDeathTest, PCHECK) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700154 EXPECT_DEATH(AOS_PCHECK(fprintf(stdin, "nope")),
Brian Silvermanfe457de2014-05-26 22:04:08 -0700155 ".*fprintf\\(stdin, \"nope\"\\).*failed.*");
156}
157
Austin Schuhf257f3c2019-10-27 21:00:43 -0700158TEST_F(LoggingTest, PCHECK) { EXPECT_EQ(7, AOS_PCHECK(printf("abc123\n"))); }
Brian Silvermanf665d692013-02-17 22:11:39 -0800159
160TEST_F(LoggingTest, PrintfDirectives) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700161 AOS_LOG(INFO, "test log %%1 %%d\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800162 EXPECT_TRUE(WasLogged(INFO, "test log %1 %d\n"));
Austin Schuhf257f3c2019-10-27 21:00:43 -0700163 AOS_LOG_DYNAMIC(WARNING, "test log %%2 %%f\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800164 EXPECT_TRUE(WasLogged(WARNING, "test log %2 %f\n"));
Austin Schuhf257f3c2019-10-27 21:00:43 -0700165 AOS_LOG_CORK("log 3 part %%1 %%d ");
166 AOS_LOG_UNCORK(DEBUG, "log 3 part %%2 %%f\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800167 EXPECT_TRUE(WasLogged(DEBUG, "log 3 part %1 %d log 3 part %2 %f\n"));
168}
169
170TEST_F(LoggingTest, Timing) {
171 // For writing only.
172 //static const long kTimingCycles = 5000000;
173 static const long kTimingCycles = 5000;
174
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800175 monotonic_clock::time_point start = monotonic_clock::now();
Brian Silvermanf665d692013-02-17 22:11:39 -0800176 for (long i = 0; i < kTimingCycles; ++i) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700177 AOS_LOG(INFO, "a\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800178 }
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800179 monotonic_clock::time_point end = monotonic_clock::now();
180 auto diff = end - start;
Brian Silverman41abe012014-02-08 18:25:02 -0800181 printf("short message took %" PRId64 " nsec for %ld\n",
Alex Perrycb7da4b2019-08-28 19:35:56 -0700182 static_cast<int64_t>(
183 chrono::duration_cast<chrono::nanoseconds>(diff).count()),
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800184 kTimingCycles);
Brian Silvermanf665d692013-02-17 22:11:39 -0800185
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800186 start = monotonic_clock::now();
Brian Silvermanf665d692013-02-17 22:11:39 -0800187 for (long i = 0; i < kTimingCycles; ++i) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700188 AOS_LOG(INFO, "something longer than just \"a\" to log to test timing\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800189 }
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800190 end = monotonic_clock::now();
Brian Silvermanf665d692013-02-17 22:11:39 -0800191 diff = end - start;
Brian Silverman41abe012014-02-08 18:25:02 -0800192 printf("long message took %" PRId64 " nsec for %ld\n",
Alex Perrycb7da4b2019-08-28 19:35:56 -0700193 static_cast<int64_t>(
194 chrono::duration_cast<chrono::nanoseconds>(diff).count()),
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800195 kTimingCycles);
Brian Silvermanf665d692013-02-17 22:11:39 -0800196}
197
Brian Silverman54a368e2015-02-14 20:05:33 -0500198TEST(LoggingPrintFormatTest, Time) {
199 char buffer[1024];
200
201 // Easy ones.
202 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
203 AOS_TIME_ARGS(2, 0)));
204 EXPECT_EQ("0000000002.000000s", ::std::string(buffer));
205 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
206 AOS_TIME_ARGS(2, 1)));
207 EXPECT_EQ("0000000002.000000s", ::std::string(buffer));
208
209 // This one should be exact.
210 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
211 AOS_TIME_ARGS(2, 1000)));
212 EXPECT_EQ("0000000002.000001s", ::std::string(buffer));
213
214 // Make sure rounding works correctly.
215 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
216 AOS_TIME_ARGS(2, 999)));
Austin Schuh648b3612017-11-20 01:02:24 -0800217 EXPECT_EQ("0000000002.000000s", ::std::string(buffer));
Brian Silverman54a368e2015-02-14 20:05:33 -0500218 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
219 AOS_TIME_ARGS(2, 500)));
Austin Schuh648b3612017-11-20 01:02:24 -0800220 EXPECT_EQ("0000000002.000000s", ::std::string(buffer));
Brian Silverman54a368e2015-02-14 20:05:33 -0500221 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
222 AOS_TIME_ARGS(2, 499)));
223 EXPECT_EQ("0000000002.000000s", ::std::string(buffer));
224
225 // This used to result in "0000000001.099500s".
226 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
227 AOS_TIME_ARGS(1, 995000000)));
228 EXPECT_EQ("0000000001.995000s", ::std::string(buffer));
Austin Schuh648b3612017-11-20 01:02:24 -0800229
230 // This used to result in "0000000001.099500s".
231 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
232 AOS_TIME_ARGS(1, 999999999)));
233 EXPECT_EQ("0000000001.999999s", ::std::string(buffer));
Brian Silverman54a368e2015-02-14 20:05:33 -0500234}
235
236TEST(LoggingPrintFormatTest, Base) {
237 char buffer[1024];
238
239 static const ::std::string kExpected1 =
240 "name(971)(01678): ERROR at 0000000001.995000s: ";
241 ASSERT_GT(sizeof(buffer), kExpected1.size());
242 ASSERT_EQ(
243 kExpected1.size(),
244 static_cast<size_t>(snprintf(
245 buffer, sizeof(buffer), AOS_LOGGING_BASE_FORMAT,
246 AOS_LOGGING_BASE_ARGS(4, "name", 971, 1678, ERROR, 1, 995000000))));
247 EXPECT_EQ(kExpected1, ::std::string(buffer));
248}
249
Brian Silvermanf665d692013-02-17 22:11:39 -0800250} // namespace testing
251} // namespace logging
252} // namespace aos