blob: 1bbfacfdf31a61f81f23b7bba219e97913f14bf4 [file] [log] [blame]
Tyler Chatow4b471e12020-01-05 20:19:36 -08001#include "aos/logging/implementations.h"
2
Austin Schuhf2a50ba2016-12-24 16:16:26 -08003#include <chrono>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07004#include <cinttypes>
Austin Schuhf2a50ba2016-12-24 16:16:26 -08005#include <string>
6
Tyler Chatow4b471e12020-01-05 20:19:36 -08007#include "aos/logging/printf_formats.h"
8#include "aos/time/time.h"
Brian Silverman1463c092020-10-30 17:28:24 -07009#include "glog/logging.h"
Brian Silvermanf665d692013-02-17 22:11:39 -080010#include "gtest/gtest.h"
11
Tyler Chatow4b471e12020-01-05 20:19:36 -080012using ::testing::AssertionFailure;
Brian Silvermanf665d692013-02-17 22:11:39 -080013using ::testing::AssertionResult;
14using ::testing::AssertionSuccess;
Brian Silvermanf665d692013-02-17 22:11:39 -080015
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_; }
Brian Silvermanf665d692013-02-17 22:11:39 -080044
Austin Schuha0c41ba2020-09-10 22:59:14 -070045 bool used_ = false;
Brian Silvermanf665d692013-02-17 22:11:39 -080046};
47class LoggingTest : public ::testing::Test {
48 protected:
49 AssertionResult WasAnythingLogged() {
50 if (log_implementation->used()) {
Tyler Chatow4b471e12020-01-05 20:19:36 -080051 return AssertionSuccess() << "read message '"
52 << log_implementation->message().message << "'";
Brian Silvermanf665d692013-02-17 22:11:39 -080053 }
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 Chatow4b471e12020-01-05 20:19:36 -080061 return AssertionFailure() << "a message with level "
62 << log_str(log_implementation->message().level)
63 << " was logged instead of " << log_str(level);
Brian Silvermanf665d692013-02-17 22:11:39 -080064 }
65 internal::Context *context = internal::Context::Get();
66 if (log_implementation->message().source != context->source) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070067 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 Silvermanf665d692013-02-17 22:11:39 -080070 }
Austin Schuhaebbc342015-01-25 02:25:13 -080071 if (log_implementation->message().name_length != context->name_size ||
72 memcmp(log_implementation->message().name, context->name,
Tyler Chatow4b471e12020-01-05 20:19:36 -080073 context->name_size) != 0) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070074 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 Silvermanf665d692013-02-17 22:11:39 -080077 }
Tyler Chatow4b471e12020-01-05 20:19:36 -080078 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 Silvermanf665d692013-02-17 22:11:39 -080083 }
84
85 return AssertionSuccess() << log_implementation->message().message;
86 }
87
88 private:
Brian Silvermancb5da1f2015-12-05 22:19:58 -050089 void SetUp() override {
Austin Schuha0c41ba2020-09-10 22:59:14 -070090 log_implementation = std::make_shared<TestLogImplementation>();
91 SetImplementation(log_implementation);
Brian Silvermanf665d692013-02-17 22:11:39 -080092 }
Austin Schuha0c41ba2020-09-10 22:59:14 -070093 void TearDown() override {
94 SetImplementation(nullptr);
Austin Schuha0c41ba2020-09-10 22:59:14 -070095 internal::Context::DeleteNow();
96 CHECK_EQ(log_implementation.use_count(), 1);
97 log_implementation.reset();
98 }
Brian Silvermanf665d692013-02-17 22:11:39 -080099
Austin Schuha0c41ba2020-09-10 22:59:14 -0700100 std::shared_ptr<TestLogImplementation> log_implementation;
Brian Silvermanf665d692013-02-17 22:11:39 -0800101};
Brian Silvermanf665d692013-02-17 22:11:39 -0800102typedef LoggingTest LoggingDeathTest;
103
104// Tests both basic logging functionality and that the test setup works
105// correctly.
106TEST_F(LoggingTest, Basic) {
107 EXPECT_FALSE(WasAnythingLogged());
Austin Schuhf257f3c2019-10-27 21:00:43 -0700108 AOS_LOG(INFO, "test log 1\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800109 EXPECT_TRUE(WasLogged(INFO, "test log 1\n"));
Austin Schuhf257f3c2019-10-27 21:00:43 -0700110 AOS_LOG(INFO, "test log 1.5\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800111 // there's a subtle typo on purpose...
112 EXPECT_FALSE(WasLogged(INFO, "test log 15\n"));
Austin Schuhf257f3c2019-10-27 21:00:43 -0700113 AOS_LOG(ERROR, "test log 2=%d\n", 55);
Brian Silvermanf665d692013-02-17 22:11:39 -0800114 EXPECT_TRUE(WasLogged(ERROR, "test log 2=55\n"));
Austin Schuhf257f3c2019-10-27 21:00:43 -0700115 AOS_LOG(ERROR, "test log 3\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800116 EXPECT_FALSE(WasLogged(WARNING, "test log 3\n"));
Austin Schuhf257f3c2019-10-27 21:00:43 -0700117 AOS_LOG(WARNING, "test log 4\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800118 EXPECT_TRUE(WasAnythingLogged());
119}
Brian Silvermanf665d692013-02-17 22:11:39 -0800120
Brian Silvermanf665d692013-02-17 22:11:39 -0800121TEST_F(LoggingDeathTest, Fatal) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700122 ASSERT_EXIT(AOS_LOG(FATAL, "this should crash it\n"),
123 ::testing::KilledBySignal(SIGABRT), "this should crash it");
Brian Silvermanf665d692013-02-17 22:11:39 -0800124}
Brian Silvermanfe457de2014-05-26 22:04:08 -0700125
126TEST_F(LoggingDeathTest, PCHECK) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700127 EXPECT_DEATH(AOS_PCHECK(fprintf(stdin, "nope")),
Brian Silvermanfe457de2014-05-26 22:04:08 -0700128 ".*fprintf\\(stdin, \"nope\"\\).*failed.*");
129}
130
Austin Schuhf257f3c2019-10-27 21:00:43 -0700131TEST_F(LoggingTest, PCHECK) { EXPECT_EQ(7, AOS_PCHECK(printf("abc123\n"))); }
Brian Silvermanf665d692013-02-17 22:11:39 -0800132
133TEST_F(LoggingTest, PrintfDirectives) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700134 AOS_LOG(INFO, "test log %%1 %%d\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800135 EXPECT_TRUE(WasLogged(INFO, "test log %1 %d\n"));
Austin Schuhf257f3c2019-10-27 21:00:43 -0700136 AOS_LOG_DYNAMIC(WARNING, "test log %%2 %%f\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800137 EXPECT_TRUE(WasLogged(WARNING, "test log %2 %f\n"));
Brian Silvermanf665d692013-02-17 22:11:39 -0800138}
139
140TEST_F(LoggingTest, Timing) {
141 // For writing only.
Tyler Chatow4b471e12020-01-05 20:19:36 -0800142 // static const long kTimingCycles = 5000000;
Brian Silvermanf665d692013-02-17 22:11:39 -0800143 static const long kTimingCycles = 5000;
144
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800145 monotonic_clock::time_point start = monotonic_clock::now();
Brian Silvermanf665d692013-02-17 22:11:39 -0800146 for (long i = 0; i < kTimingCycles; ++i) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700147 AOS_LOG(INFO, "a\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800148 }
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800149 monotonic_clock::time_point end = monotonic_clock::now();
150 auto diff = end - start;
Brian Silverman41abe012014-02-08 18:25:02 -0800151 printf("short message took %" PRId64 " nsec for %ld\n",
Alex Perrycb7da4b2019-08-28 19:35:56 -0700152 static_cast<int64_t>(
153 chrono::duration_cast<chrono::nanoseconds>(diff).count()),
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800154 kTimingCycles);
Brian Silvermanf665d692013-02-17 22:11:39 -0800155
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800156 start = monotonic_clock::now();
Brian Silvermanf665d692013-02-17 22:11:39 -0800157 for (long i = 0; i < kTimingCycles; ++i) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700158 AOS_LOG(INFO, "something longer than just \"a\" to log to test timing\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800159 }
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800160 end = monotonic_clock::now();
Brian Silvermanf665d692013-02-17 22:11:39 -0800161 diff = end - start;
Brian Silverman41abe012014-02-08 18:25:02 -0800162 printf("long message took %" PRId64 " nsec for %ld\n",
Alex Perrycb7da4b2019-08-28 19:35:56 -0700163 static_cast<int64_t>(
164 chrono::duration_cast<chrono::nanoseconds>(diff).count()),
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800165 kTimingCycles);
Brian Silvermanf665d692013-02-17 22:11:39 -0800166}
167
Brian Silverman54a368e2015-02-14 20:05:33 -0500168TEST(LoggingPrintFormatTest, Time) {
169 char buffer[1024];
170
171 // Easy ones.
172 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
173 AOS_TIME_ARGS(2, 0)));
174 EXPECT_EQ("0000000002.000000s", ::std::string(buffer));
175 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
176 AOS_TIME_ARGS(2, 1)));
177 EXPECT_EQ("0000000002.000000s", ::std::string(buffer));
178
179 // This one should be exact.
180 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
181 AOS_TIME_ARGS(2, 1000)));
182 EXPECT_EQ("0000000002.000001s", ::std::string(buffer));
183
184 // Make sure rounding works correctly.
185 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
186 AOS_TIME_ARGS(2, 999)));
Austin Schuh648b3612017-11-20 01:02:24 -0800187 EXPECT_EQ("0000000002.000000s", ::std::string(buffer));
Brian Silverman54a368e2015-02-14 20:05:33 -0500188 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
189 AOS_TIME_ARGS(2, 500)));
Austin Schuh648b3612017-11-20 01:02:24 -0800190 EXPECT_EQ("0000000002.000000s", ::std::string(buffer));
Brian Silverman54a368e2015-02-14 20:05:33 -0500191 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
192 AOS_TIME_ARGS(2, 499)));
193 EXPECT_EQ("0000000002.000000s", ::std::string(buffer));
194
195 // This used to result in "0000000001.099500s".
196 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
197 AOS_TIME_ARGS(1, 995000000)));
198 EXPECT_EQ("0000000001.995000s", ::std::string(buffer));
Austin Schuh648b3612017-11-20 01:02:24 -0800199
200 // This used to result in "0000000001.099500s".
201 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
202 AOS_TIME_ARGS(1, 999999999)));
203 EXPECT_EQ("0000000001.999999s", ::std::string(buffer));
Brian Silverman54a368e2015-02-14 20:05:33 -0500204}
205
206TEST(LoggingPrintFormatTest, Base) {
207 char buffer[1024];
208
209 static const ::std::string kExpected1 =
210 "name(971)(01678): ERROR at 0000000001.995000s: ";
211 ASSERT_GT(sizeof(buffer), kExpected1.size());
212 ASSERT_EQ(
213 kExpected1.size(),
214 static_cast<size_t>(snprintf(
215 buffer, sizeof(buffer), AOS_LOGGING_BASE_FORMAT,
216 AOS_LOGGING_BASE_ARGS(4, "name", 971, 1678, ERROR, 1, 995000000))));
217 EXPECT_EQ(kExpected1, ::std::string(buffer));
218}
219
Tyler Chatow67ddb032020-01-12 14:30:04 -0800220TEST(ScopedLogRestorerTest, RestoreTest) {
Austin Schuha0c41ba2020-09-10 22:59:14 -0700221 SetImplementation(std::make_shared<StreamLogImplementation>(stdout));
222 LogImplementation *curr_impl = GetImplementation().get();
Tyler Chatow67ddb032020-01-12 14:30:04 -0800223
224 {
225 ScopedLogRestorer log_restorer;
226
Austin Schuha0c41ba2020-09-10 22:59:14 -0700227 log_restorer.Swap(
228 std::make_shared<CallbackLogImplementation>([](const LogMessage &) {}));
229 ASSERT_NE(curr_impl, GetImplementation().get());
Tyler Chatow67ddb032020-01-12 14:30:04 -0800230 }
231
Austin Schuha0c41ba2020-09-10 22:59:14 -0700232 ASSERT_EQ(curr_impl, GetImplementation().get());
Tyler Chatow67ddb032020-01-12 14:30:04 -0800233}
234
Brian Silvermanf665d692013-02-17 22:11:39 -0800235} // namespace testing
236} // namespace logging
237} // namespace aos