blob: d08f85f780c9e8c17a6409ab828bf4ccf80ce6c4 [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
Brian Silverman1463c092020-10-30 17:28:24 -07007#include "glog/logging.h"
Brian Silvermanf665d692013-02-17 22:11:39 -08008#include "gtest/gtest.h"
9
Philipp Schrader790cb542023-07-05 21:06:52 -070010#include "aos/logging/printf_formats.h"
11#include "aos/time/time.h"
12
Tyler Chatow4b471e12020-01-05 20:19:36 -080013using ::testing::AssertionFailure;
Brian Silvermanf665d692013-02-17 22:11:39 -080014using ::testing::AssertionResult;
15using ::testing::AssertionSuccess;
Brian Silvermanf665d692013-02-17 22:11:39 -080016
17namespace aos {
18namespace logging {
19namespace testing {
20
Austin Schuhf2a50ba2016-12-24 16:16:26 -080021namespace chrono = ::std::chrono;
22
Alex Perrycb7da4b2019-08-28 19:35:56 -070023class TestLogImplementation : public LogImplementation {
24 virtual ::aos::monotonic_clock::time_point monotonic_now() const {
25 return ::aos::monotonic_clock::now();
26 }
27
Austin Schuhad9e5eb2021-11-19 20:33:55 -080028 std::string_view MyName() override {
29 internal::Context *context = internal::Context::Get();
30 return context->MyName();
31 }
32
Alex Perrycb7da4b2019-08-28 19:35:56 -070033 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 0))) void DoLog(
34 log_level level, const char *format, va_list ap) override {
Austin Schuhad9e5eb2021-11-19 20:33:55 -080035 internal::FillInMessage(level, MyName(), monotonic_now(), format, ap,
36 &message_);
Brian Silvermanf665d692013-02-17 22:11:39 -080037
Brian Silvermanfe457de2014-05-26 22:04:08 -070038 if (level == FATAL) {
39 internal::PrintMessage(stderr, message_);
40 abort();
41 }
42
Brian Silvermanf665d692013-02-17 22:11:39 -080043 used_ = true;
44 }
45
46 LogMessage message_;
47
48 public:
49 const LogMessage &message() { return message_; }
50 bool used() { return used_; }
Brian Silvermanf665d692013-02-17 22:11:39 -080051
Austin Schuha0c41ba2020-09-10 22:59:14 -070052 bool used_ = false;
Brian Silvermanf665d692013-02-17 22:11:39 -080053};
54class LoggingTest : public ::testing::Test {
55 protected:
56 AssertionResult WasAnythingLogged() {
57 if (log_implementation->used()) {
Tyler Chatow4b471e12020-01-05 20:19:36 -080058 return AssertionSuccess() << "read message '"
59 << log_implementation->message().message << "'";
Brian Silvermanf665d692013-02-17 22:11:39 -080060 }
61 return AssertionFailure();
62 }
63 AssertionResult WasLogged(log_level level, const std::string message) {
64 if (!log_implementation->used()) {
65 return AssertionFailure() << "nothing was logged";
66 }
67 if (log_implementation->message().level != level) {
Tyler Chatow4b471e12020-01-05 20:19:36 -080068 return AssertionFailure() << "a message with level "
69 << log_str(log_implementation->message().level)
70 << " was logged instead of " << log_str(level);
Brian Silvermanf665d692013-02-17 22:11:39 -080071 }
72 internal::Context *context = internal::Context::Get();
73 if (log_implementation->message().source != context->source) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070074 AOS_LOG(FATAL, "got a message from %" PRIu32 ", but we're %" PRIu32 "\n",
75 static_cast<uint32_t>(log_implementation->message().source),
76 static_cast<uint32_t>(context->source));
Brian Silvermanf665d692013-02-17 22:11:39 -080077 }
Austin Schuhad9e5eb2021-11-19 20:33:55 -080078 if (log_implementation->message().name_length != context->MyName().size() ||
79 memcmp(log_implementation->message().name, context->MyName().data(),
80 context->MyName().size()) != 0) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070081 AOS_LOG(FATAL, "got a message from %.*s, but we're %s\n",
82 static_cast<int>(log_implementation->message().name_length),
Austin Schuhad9e5eb2021-11-19 20:33:55 -080083 log_implementation->message().name,
84 std::string(context->MyName()).c_str());
Brian Silvermanf665d692013-02-17 22:11:39 -080085 }
Tyler Chatow4b471e12020-01-05 20:19:36 -080086 if (strstr(log_implementation->message().message, message.c_str()) ==
87 NULL) {
88 return AssertionFailure()
89 << "got a message of '" << log_implementation->message().message
90 << "' but expected it to contain '" << message << "'";
Brian Silvermanf665d692013-02-17 22:11:39 -080091 }
92
93 return AssertionSuccess() << log_implementation->message().message;
94 }
95
96 private:
Brian Silvermancb5da1f2015-12-05 22:19:58 -050097 void SetUp() override {
Austin Schuha0c41ba2020-09-10 22:59:14 -070098 log_implementation = std::make_shared<TestLogImplementation>();
99 SetImplementation(log_implementation);
Brian Silvermanf665d692013-02-17 22:11:39 -0800100 }
Austin Schuha0c41ba2020-09-10 22:59:14 -0700101 void TearDown() override {
102 SetImplementation(nullptr);
Austin Schuha0c41ba2020-09-10 22:59:14 -0700103 internal::Context::DeleteNow();
104 CHECK_EQ(log_implementation.use_count(), 1);
105 log_implementation.reset();
106 }
Brian Silvermanf665d692013-02-17 22:11:39 -0800107
Austin Schuha0c41ba2020-09-10 22:59:14 -0700108 std::shared_ptr<TestLogImplementation> log_implementation;
Brian Silvermanf665d692013-02-17 22:11:39 -0800109};
Brian Silvermanf665d692013-02-17 22:11:39 -0800110typedef LoggingTest LoggingDeathTest;
111
112// Tests both basic logging functionality and that the test setup works
113// correctly.
114TEST_F(LoggingTest, Basic) {
115 EXPECT_FALSE(WasAnythingLogged());
Austin Schuhf257f3c2019-10-27 21:00:43 -0700116 AOS_LOG(INFO, "test log 1\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800117 EXPECT_TRUE(WasLogged(INFO, "test log 1\n"));
Austin Schuhf257f3c2019-10-27 21:00:43 -0700118 AOS_LOG(INFO, "test log 1.5\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800119 // there's a subtle typo on purpose...
120 EXPECT_FALSE(WasLogged(INFO, "test log 15\n"));
Austin Schuhf257f3c2019-10-27 21:00:43 -0700121 AOS_LOG(ERROR, "test log 2=%d\n", 55);
Brian Silvermanf665d692013-02-17 22:11:39 -0800122 EXPECT_TRUE(WasLogged(ERROR, "test log 2=55\n"));
Austin Schuhf257f3c2019-10-27 21:00:43 -0700123 AOS_LOG(ERROR, "test log 3\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800124 EXPECT_FALSE(WasLogged(WARNING, "test log 3\n"));
Austin Schuhf257f3c2019-10-27 21:00:43 -0700125 AOS_LOG(WARNING, "test log 4\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800126 EXPECT_TRUE(WasAnythingLogged());
127}
Brian Silvermanf665d692013-02-17 22:11:39 -0800128
Brian Silvermanf665d692013-02-17 22:11:39 -0800129TEST_F(LoggingDeathTest, Fatal) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700130 ASSERT_EXIT(AOS_LOG(FATAL, "this should crash it\n"),
131 ::testing::KilledBySignal(SIGABRT), "this should crash it");
Brian Silvermanf665d692013-02-17 22:11:39 -0800132}
Brian Silvermanfe457de2014-05-26 22:04:08 -0700133
134TEST_F(LoggingDeathTest, PCHECK) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700135 EXPECT_DEATH(AOS_PCHECK(fprintf(stdin, "nope")),
Brian Silvermanfe457de2014-05-26 22:04:08 -0700136 ".*fprintf\\(stdin, \"nope\"\\).*failed.*");
137}
138
Austin Schuhf257f3c2019-10-27 21:00:43 -0700139TEST_F(LoggingTest, PCHECK) { EXPECT_EQ(7, AOS_PCHECK(printf("abc123\n"))); }
Brian Silvermanf665d692013-02-17 22:11:39 -0800140
141TEST_F(LoggingTest, PrintfDirectives) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700142 AOS_LOG(INFO, "test log %%1 %%d\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800143 EXPECT_TRUE(WasLogged(INFO, "test log %1 %d\n"));
Austin Schuhf257f3c2019-10-27 21:00:43 -0700144 AOS_LOG_DYNAMIC(WARNING, "test log %%2 %%f\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800145 EXPECT_TRUE(WasLogged(WARNING, "test log %2 %f\n"));
Brian Silvermanf665d692013-02-17 22:11:39 -0800146}
147
148TEST_F(LoggingTest, Timing) {
149 // For writing only.
Tyler Chatow4b471e12020-01-05 20:19:36 -0800150 // static const long kTimingCycles = 5000000;
Brian Silvermanf665d692013-02-17 22:11:39 -0800151 static const long kTimingCycles = 5000;
152
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800153 monotonic_clock::time_point start = monotonic_clock::now();
Brian Silvermanf665d692013-02-17 22:11:39 -0800154 for (long i = 0; i < kTimingCycles; ++i) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700155 AOS_LOG(INFO, "a\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800156 }
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800157 monotonic_clock::time_point end = monotonic_clock::now();
158 auto diff = end - start;
Brian Silverman41abe012014-02-08 18:25:02 -0800159 printf("short message took %" PRId64 " nsec for %ld\n",
Alex Perrycb7da4b2019-08-28 19:35:56 -0700160 static_cast<int64_t>(
161 chrono::duration_cast<chrono::nanoseconds>(diff).count()),
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800162 kTimingCycles);
Brian Silvermanf665d692013-02-17 22:11:39 -0800163
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800164 start = monotonic_clock::now();
Brian Silvermanf665d692013-02-17 22:11:39 -0800165 for (long i = 0; i < kTimingCycles; ++i) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700166 AOS_LOG(INFO, "something longer than just \"a\" to log to test timing\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800167 }
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800168 end = monotonic_clock::now();
Brian Silvermanf665d692013-02-17 22:11:39 -0800169 diff = end - start;
Brian Silverman41abe012014-02-08 18:25:02 -0800170 printf("long message took %" PRId64 " nsec for %ld\n",
Alex Perrycb7da4b2019-08-28 19:35:56 -0700171 static_cast<int64_t>(
172 chrono::duration_cast<chrono::nanoseconds>(diff).count()),
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800173 kTimingCycles);
Brian Silvermanf665d692013-02-17 22:11:39 -0800174}
175
Brian Silverman54a368e2015-02-14 20:05:33 -0500176TEST(LoggingPrintFormatTest, Time) {
177 char buffer[1024];
178
179 // Easy ones.
180 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
181 AOS_TIME_ARGS(2, 0)));
182 EXPECT_EQ("0000000002.000000s", ::std::string(buffer));
183 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
184 AOS_TIME_ARGS(2, 1)));
185 EXPECT_EQ("0000000002.000000s", ::std::string(buffer));
186
187 // This one should be exact.
188 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
189 AOS_TIME_ARGS(2, 1000)));
190 EXPECT_EQ("0000000002.000001s", ::std::string(buffer));
191
192 // Make sure rounding works correctly.
193 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
194 AOS_TIME_ARGS(2, 999)));
Austin Schuh648b3612017-11-20 01:02:24 -0800195 EXPECT_EQ("0000000002.000000s", ::std::string(buffer));
Brian Silverman54a368e2015-02-14 20:05:33 -0500196 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
197 AOS_TIME_ARGS(2, 500)));
Austin Schuh648b3612017-11-20 01:02:24 -0800198 EXPECT_EQ("0000000002.000000s", ::std::string(buffer));
Brian Silverman54a368e2015-02-14 20:05:33 -0500199 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
200 AOS_TIME_ARGS(2, 499)));
201 EXPECT_EQ("0000000002.000000s", ::std::string(buffer));
202
203 // This used to result in "0000000001.099500s".
204 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
205 AOS_TIME_ARGS(1, 995000000)));
206 EXPECT_EQ("0000000001.995000s", ::std::string(buffer));
Austin Schuh648b3612017-11-20 01:02:24 -0800207
208 // This used to result in "0000000001.099500s".
209 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
210 AOS_TIME_ARGS(1, 999999999)));
211 EXPECT_EQ("0000000001.999999s", ::std::string(buffer));
Brian Silverman54a368e2015-02-14 20:05:33 -0500212}
213
214TEST(LoggingPrintFormatTest, Base) {
215 char buffer[1024];
216
217 static const ::std::string kExpected1 =
218 "name(971)(01678): ERROR at 0000000001.995000s: ";
219 ASSERT_GT(sizeof(buffer), kExpected1.size());
220 ASSERT_EQ(
221 kExpected1.size(),
222 static_cast<size_t>(snprintf(
223 buffer, sizeof(buffer), AOS_LOGGING_BASE_FORMAT,
224 AOS_LOGGING_BASE_ARGS(4, "name", 971, 1678, ERROR, 1, 995000000))));
225 EXPECT_EQ(kExpected1, ::std::string(buffer));
226}
227
Tyler Chatow67ddb032020-01-12 14:30:04 -0800228TEST(ScopedLogRestorerTest, RestoreTest) {
Austin Schuha0c41ba2020-09-10 22:59:14 -0700229 SetImplementation(std::make_shared<StreamLogImplementation>(stdout));
230 LogImplementation *curr_impl = GetImplementation().get();
Tyler Chatow67ddb032020-01-12 14:30:04 -0800231
Austin Schuhad9e5eb2021-11-19 20:33:55 -0800232 std::string name = "name";
Tyler Chatow67ddb032020-01-12 14:30:04 -0800233 {
234 ScopedLogRestorer log_restorer;
235
Austin Schuhad9e5eb2021-11-19 20:33:55 -0800236 log_restorer.Swap(std::make_shared<CallbackLogImplementation>(
237 [](const LogMessage &) {}, &name));
Austin Schuha0c41ba2020-09-10 22:59:14 -0700238 ASSERT_NE(curr_impl, GetImplementation().get());
Tyler Chatow67ddb032020-01-12 14:30:04 -0800239 }
240
Austin Schuha0c41ba2020-09-10 22:59:14 -0700241 ASSERT_EQ(curr_impl, GetImplementation().get());
Tyler Chatow67ddb032020-01-12 14:30:04 -0800242}
243
Brian Silvermanf665d692013-02-17 22:11:39 -0800244} // namespace testing
245} // namespace logging
246} // namespace aos