blob: 9663fd22d96ee048143514ec70d3f4cbcef242b8 [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
Brian Silvermancb5da1f2015-12-05 22:19:58 -05008#include "aos/common/logging/implementations.h"
Brian Silvermanf665d692013-02-17 22:11:39 -08009#include "aos/common/time.h"
Brian Silvermancb5da1f2015-12-05 22:19:58 -050010#include "aos/common/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
Brian Silvermancb5da1f2015-12-05 22:19:58 -050022class TestLogImplementation : public SimpleLogImplementation {
Brian Silvermanf7986142014-04-21 17:42:35 -070023 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 0)))
Brian Silvermancb5da1f2015-12-05 22:19:58 -050024 void DoLog(log_level level, const char *format, va_list ap) override {
Brian Silvermanf665d692013-02-17 22:11:39 -080025 internal::FillInMessage(level, format, ap, &message_);
26
Brian Silvermanfe457de2014-05-26 22:04:08 -070027 if (level == FATAL) {
28 internal::PrintMessage(stderr, message_);
29 abort();
30 }
31
Brian Silvermanf665d692013-02-17 22:11:39 -080032 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};
46class 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 Silvermanf0bb8372014-04-30 15:58:55 -070066 LOG(FATAL, "got a message from %" PRIu32 ", but we're %" PRIu32 "\n",
Brian Silvermanf665d692013-02-17 22:11:39 -080067 static_cast<uint32_t>(log_implementation->message().source),
68 static_cast<uint32_t>(context->source));
69 }
Austin Schuhaebbc342015-01-25 02:25:13 -080070 if (log_implementation->message().name_length != context->name_size ||
71 memcmp(log_implementation->message().name, context->name,
72 context->name_size) !=
Brian Silvermanf0bb8372014-04-30 15:58:55 -070073 0) {
74 LOG(FATAL, "got a message from %.*s, but we're %s\n",
75 static_cast<int>(log_implementation->message().name_length),
Austin Schuhaebbc342015-01-25 02:25:13 -080076 log_implementation->message().name, context->name);
Brian Silvermanf665d692013-02-17 22:11:39 -080077 }
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 Silvermancb5da1f2015-12-05 22:19:58 -050089 void SetUp() override {
Brian Silvermanf665d692013-02-17 22:11:39 -080090 static bool first = true;
91 if (first) {
92 first = false;
93
94 Init();
Brian Silvermanf665d692013-02-17 22:11:39 -080095 AddImplementation(log_implementation = new TestLogImplementation());
96 }
97
98 log_implementation->reset_used();
99 }
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500100 void TearDown() override {
Brian Silvermanf665d692013-02-17 22:11:39 -0800101 Cleanup();
102 }
103
104 static TestLogImplementation *log_implementation;
105};
106TestLogImplementation *LoggingTest::log_implementation(NULL);
107typedef LoggingTest LoggingDeathTest;
108
109// Tests both basic logging functionality and that the test setup works
110// correctly.
111TEST_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}
125TEST_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 Silvermancb5da1f2015-12-05 22:19:58 -0500134 expected << "implementations_test.cc: ";
Brian Silvermanf665d692013-02-17 22:11:39 -0800135 expected << (begin_line + 1);
136 expected << "-";
137 expected << (end_line + 1);
138 expected << ": ";
Brian Silvermanc0a0d112013-09-19 21:08:49 -0700139 expected << __func__;
Brian Silvermanf665d692013-02-17 22:11:39 -0800140 expected << ": first part second part (=19) third part last part 5\n";
141 EXPECT_TRUE(WasLogged(WARNING, expected.str()));
142}
143
Brian Silvermanf665d692013-02-17 22:11:39 -0800144TEST_F(LoggingDeathTest, Fatal) {
145 ASSERT_EXIT(LOG(FATAL, "this should crash it\n"),
146 ::testing::KilledBySignal(SIGABRT),
147 "this should crash it");
148}
Brian Silvermanfe457de2014-05-26 22:04:08 -0700149
150TEST_F(LoggingDeathTest, PCHECK) {
151 EXPECT_DEATH(PCHECK(fprintf(stdin, "nope")),
152 ".*fprintf\\(stdin, \"nope\"\\).*failed.*");
153}
154
155TEST_F(LoggingTest, PCHECK) {
156 EXPECT_EQ(7, PCHECK(printf("abc123\n")));
157}
Brian Silvermanf665d692013-02-17 22:11:39 -0800158
159TEST_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
169TEST_F(LoggingTest, Timing) {
170 // For writing only.
171 //static const long kTimingCycles = 5000000;
172 static const long kTimingCycles = 5000;
173
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800174 monotonic_clock::time_point start = monotonic_clock::now();
Brian Silvermanf665d692013-02-17 22:11:39 -0800175 for (long i = 0; i < kTimingCycles; ++i) {
176 LOG(INFO, "a\n");
177 }
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800178 monotonic_clock::time_point end = monotonic_clock::now();
179 auto diff = end - start;
Brian Silverman41abe012014-02-08 18:25:02 -0800180 printf("short message took %" PRId64 " nsec for %ld\n",
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800181 chrono::duration_cast<chrono::nanoseconds>(diff).count(),
182 kTimingCycles);
Brian Silvermanf665d692013-02-17 22:11:39 -0800183
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800184 start = monotonic_clock::now();
Brian Silvermanf665d692013-02-17 22:11:39 -0800185 for (long i = 0; i < kTimingCycles; ++i) {
186 LOG(INFO, "something longer than just \"a\" to log to test timing\n");
187 }
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800188 end = monotonic_clock::now();
Brian Silvermanf665d692013-02-17 22:11:39 -0800189 diff = end - start;
Brian Silverman41abe012014-02-08 18:25:02 -0800190 printf("long message took %" PRId64 " nsec for %ld\n",
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800191 chrono::duration_cast<chrono::nanoseconds>(diff).count(),
192 kTimingCycles);
Brian Silvermanf665d692013-02-17 22:11:39 -0800193}
194
Brian Silverman54a368e2015-02-14 20:05:33 -0500195TEST(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 Schuh648b3612017-11-20 01:02:24 -0800214 EXPECT_EQ("0000000002.000000s", ::std::string(buffer));
Brian Silverman54a368e2015-02-14 20:05:33 -0500215 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
216 AOS_TIME_ARGS(2, 500)));
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, 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 Schuh648b3612017-11-20 01:02:24 -0800226
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 Silverman54a368e2015-02-14 20:05:33 -0500231}
232
233TEST(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 Silvermanf665d692013-02-17 22:11:39 -0800247} // namespace testing
248} // namespace logging
249} // namespace aos