blob: 693a91ec5c3c8d6b12718c4b2b2b2c66174aac98 [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
Stephan Pleinesf63bde82024-01-13 15:59:33 -080017namespace aos::logging::testing {
Brian Silvermanf665d692013-02-17 22:11:39 -080018
Austin Schuhf2a50ba2016-12-24 16:16:26 -080019namespace chrono = ::std::chrono;
20
Alex Perrycb7da4b2019-08-28 19:35:56 -070021class TestLogImplementation : public LogImplementation {
22 virtual ::aos::monotonic_clock::time_point monotonic_now() const {
23 return ::aos::monotonic_clock::now();
24 }
25
Austin Schuhad9e5eb2021-11-19 20:33:55 -080026 std::string_view MyName() override {
27 internal::Context *context = internal::Context::Get();
28 return context->MyName();
29 }
30
Alex Perrycb7da4b2019-08-28 19:35:56 -070031 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 0))) void DoLog(
32 log_level level, const char *format, va_list ap) override {
Austin Schuhad9e5eb2021-11-19 20:33:55 -080033 internal::FillInMessage(level, MyName(), monotonic_now(), format, ap,
34 &message_);
Brian Silvermanf665d692013-02-17 22:11:39 -080035
Brian Silvermanfe457de2014-05-26 22:04:08 -070036 if (level == FATAL) {
37 internal::PrintMessage(stderr, message_);
38 abort();
39 }
40
Brian Silvermanf665d692013-02-17 22:11:39 -080041 used_ = true;
42 }
43
44 LogMessage message_;
45
46 public:
47 const LogMessage &message() { return message_; }
48 bool used() { return used_; }
Brian Silvermanf665d692013-02-17 22:11:39 -080049
Austin Schuha0c41ba2020-09-10 22:59:14 -070050 bool used_ = false;
Brian Silvermanf665d692013-02-17 22:11:39 -080051};
52class LoggingTest : public ::testing::Test {
53 protected:
54 AssertionResult WasAnythingLogged() {
55 if (log_implementation->used()) {
Tyler Chatow4b471e12020-01-05 20:19:36 -080056 return AssertionSuccess() << "read message '"
57 << log_implementation->message().message << "'";
Brian Silvermanf665d692013-02-17 22:11:39 -080058 }
59 return AssertionFailure();
60 }
61 AssertionResult WasLogged(log_level level, const std::string message) {
62 if (!log_implementation->used()) {
63 return AssertionFailure() << "nothing was logged";
64 }
65 if (log_implementation->message().level != level) {
Tyler Chatow4b471e12020-01-05 20:19:36 -080066 return AssertionFailure() << "a message with level "
67 << log_str(log_implementation->message().level)
68 << " was logged instead of " << log_str(level);
Brian Silvermanf665d692013-02-17 22:11:39 -080069 }
70 internal::Context *context = internal::Context::Get();
71 if (log_implementation->message().source != context->source) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070072 AOS_LOG(FATAL, "got a message from %" PRIu32 ", but we're %" PRIu32 "\n",
73 static_cast<uint32_t>(log_implementation->message().source),
74 static_cast<uint32_t>(context->source));
Brian Silvermanf665d692013-02-17 22:11:39 -080075 }
Austin Schuhad9e5eb2021-11-19 20:33:55 -080076 if (log_implementation->message().name_length != context->MyName().size() ||
77 memcmp(log_implementation->message().name, context->MyName().data(),
78 context->MyName().size()) != 0) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070079 AOS_LOG(FATAL, "got a message from %.*s, but we're %s\n",
80 static_cast<int>(log_implementation->message().name_length),
Austin Schuhad9e5eb2021-11-19 20:33:55 -080081 log_implementation->message().name,
82 std::string(context->MyName()).c_str());
Brian Silvermanf665d692013-02-17 22:11:39 -080083 }
Tyler Chatow4b471e12020-01-05 20:19:36 -080084 if (strstr(log_implementation->message().message, message.c_str()) ==
85 NULL) {
86 return AssertionFailure()
87 << "got a message of '" << log_implementation->message().message
88 << "' but expected it to contain '" << message << "'";
Brian Silvermanf665d692013-02-17 22:11:39 -080089 }
90
91 return AssertionSuccess() << log_implementation->message().message;
92 }
93
94 private:
Brian Silvermancb5da1f2015-12-05 22:19:58 -050095 void SetUp() override {
Austin Schuha0c41ba2020-09-10 22:59:14 -070096 log_implementation = std::make_shared<TestLogImplementation>();
97 SetImplementation(log_implementation);
Brian Silvermanf665d692013-02-17 22:11:39 -080098 }
Austin Schuha0c41ba2020-09-10 22:59:14 -070099 void TearDown() override {
100 SetImplementation(nullptr);
Austin Schuha0c41ba2020-09-10 22:59:14 -0700101 internal::Context::DeleteNow();
102 CHECK_EQ(log_implementation.use_count(), 1);
103 log_implementation.reset();
104 }
Brian Silvermanf665d692013-02-17 22:11:39 -0800105
Austin Schuha0c41ba2020-09-10 22:59:14 -0700106 std::shared_ptr<TestLogImplementation> log_implementation;
Brian Silvermanf665d692013-02-17 22:11:39 -0800107};
Brian Silvermanf665d692013-02-17 22:11:39 -0800108typedef LoggingTest LoggingDeathTest;
109
110// Tests both basic logging functionality and that the test setup works
111// correctly.
112TEST_F(LoggingTest, Basic) {
113 EXPECT_FALSE(WasAnythingLogged());
Austin Schuhf257f3c2019-10-27 21:00:43 -0700114 AOS_LOG(INFO, "test log 1\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800115 EXPECT_TRUE(WasLogged(INFO, "test log 1\n"));
Austin Schuhf257f3c2019-10-27 21:00:43 -0700116 AOS_LOG(INFO, "test log 1.5\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800117 // there's a subtle typo on purpose...
118 EXPECT_FALSE(WasLogged(INFO, "test log 15\n"));
Austin Schuhf257f3c2019-10-27 21:00:43 -0700119 AOS_LOG(ERROR, "test log 2=%d\n", 55);
Brian Silvermanf665d692013-02-17 22:11:39 -0800120 EXPECT_TRUE(WasLogged(ERROR, "test log 2=55\n"));
Austin Schuhf257f3c2019-10-27 21:00:43 -0700121 AOS_LOG(ERROR, "test log 3\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800122 EXPECT_FALSE(WasLogged(WARNING, "test log 3\n"));
Austin Schuhf257f3c2019-10-27 21:00:43 -0700123 AOS_LOG(WARNING, "test log 4\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800124 EXPECT_TRUE(WasAnythingLogged());
125}
Brian Silvermanf665d692013-02-17 22:11:39 -0800126
Brian Silvermanf665d692013-02-17 22:11:39 -0800127TEST_F(LoggingDeathTest, Fatal) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700128 ASSERT_EXIT(AOS_LOG(FATAL, "this should crash it\n"),
129 ::testing::KilledBySignal(SIGABRT), "this should crash it");
Brian Silvermanf665d692013-02-17 22:11:39 -0800130}
Brian Silvermanfe457de2014-05-26 22:04:08 -0700131
132TEST_F(LoggingDeathTest, PCHECK) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700133 EXPECT_DEATH(AOS_PCHECK(fprintf(stdin, "nope")),
Brian Silvermanfe457de2014-05-26 22:04:08 -0700134 ".*fprintf\\(stdin, \"nope\"\\).*failed.*");
135}
136
Austin Schuhf257f3c2019-10-27 21:00:43 -0700137TEST_F(LoggingTest, PCHECK) { EXPECT_EQ(7, AOS_PCHECK(printf("abc123\n"))); }
Brian Silvermanf665d692013-02-17 22:11:39 -0800138
139TEST_F(LoggingTest, PrintfDirectives) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700140 AOS_LOG(INFO, "test log %%1 %%d\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800141 EXPECT_TRUE(WasLogged(INFO, "test log %1 %d\n"));
Austin Schuhf257f3c2019-10-27 21:00:43 -0700142 AOS_LOG_DYNAMIC(WARNING, "test log %%2 %%f\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800143 EXPECT_TRUE(WasLogged(WARNING, "test log %2 %f\n"));
Brian Silvermanf665d692013-02-17 22:11:39 -0800144}
145
146TEST_F(LoggingTest, Timing) {
147 // For writing only.
Tyler Chatow4b471e12020-01-05 20:19:36 -0800148 // static const long kTimingCycles = 5000000;
Brian Silvermanf665d692013-02-17 22:11:39 -0800149 static const long kTimingCycles = 5000;
150
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800151 monotonic_clock::time_point start = monotonic_clock::now();
Brian Silvermanf665d692013-02-17 22:11:39 -0800152 for (long i = 0; i < kTimingCycles; ++i) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700153 AOS_LOG(INFO, "a\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800154 }
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800155 monotonic_clock::time_point end = monotonic_clock::now();
156 auto diff = end - start;
Brian Silverman41abe012014-02-08 18:25:02 -0800157 printf("short message took %" PRId64 " nsec for %ld\n",
Alex Perrycb7da4b2019-08-28 19:35:56 -0700158 static_cast<int64_t>(
159 chrono::duration_cast<chrono::nanoseconds>(diff).count()),
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800160 kTimingCycles);
Brian Silvermanf665d692013-02-17 22:11:39 -0800161
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800162 start = monotonic_clock::now();
Brian Silvermanf665d692013-02-17 22:11:39 -0800163 for (long i = 0; i < kTimingCycles; ++i) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700164 AOS_LOG(INFO, "something longer than just \"a\" to log to test timing\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800165 }
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800166 end = monotonic_clock::now();
Brian Silvermanf665d692013-02-17 22:11:39 -0800167 diff = end - start;
Brian Silverman41abe012014-02-08 18:25:02 -0800168 printf("long message took %" PRId64 " nsec for %ld\n",
Alex Perrycb7da4b2019-08-28 19:35:56 -0700169 static_cast<int64_t>(
170 chrono::duration_cast<chrono::nanoseconds>(diff).count()),
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800171 kTimingCycles);
Brian Silvermanf665d692013-02-17 22:11:39 -0800172}
173
Brian Silverman54a368e2015-02-14 20:05:33 -0500174TEST(LoggingPrintFormatTest, Time) {
175 char buffer[1024];
176
177 // Easy ones.
178 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
179 AOS_TIME_ARGS(2, 0)));
180 EXPECT_EQ("0000000002.000000s", ::std::string(buffer));
181 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
182 AOS_TIME_ARGS(2, 1)));
183 EXPECT_EQ("0000000002.000000s", ::std::string(buffer));
184
185 // This one should be exact.
186 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
187 AOS_TIME_ARGS(2, 1000)));
188 EXPECT_EQ("0000000002.000001s", ::std::string(buffer));
189
190 // Make sure rounding works correctly.
191 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
192 AOS_TIME_ARGS(2, 999)));
Austin Schuh648b3612017-11-20 01:02:24 -0800193 EXPECT_EQ("0000000002.000000s", ::std::string(buffer));
Brian Silverman54a368e2015-02-14 20:05:33 -0500194 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
195 AOS_TIME_ARGS(2, 500)));
Austin Schuh648b3612017-11-20 01:02:24 -0800196 EXPECT_EQ("0000000002.000000s", ::std::string(buffer));
Brian Silverman54a368e2015-02-14 20:05:33 -0500197 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
198 AOS_TIME_ARGS(2, 499)));
199 EXPECT_EQ("0000000002.000000s", ::std::string(buffer));
200
201 // This used to result in "0000000001.099500s".
202 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
203 AOS_TIME_ARGS(1, 995000000)));
204 EXPECT_EQ("0000000001.995000s", ::std::string(buffer));
Austin Schuh648b3612017-11-20 01:02:24 -0800205
206 // This used to result in "0000000001.099500s".
207 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
208 AOS_TIME_ARGS(1, 999999999)));
209 EXPECT_EQ("0000000001.999999s", ::std::string(buffer));
Brian Silverman54a368e2015-02-14 20:05:33 -0500210}
211
212TEST(LoggingPrintFormatTest, Base) {
213 char buffer[1024];
214
215 static const ::std::string kExpected1 =
216 "name(971)(01678): ERROR at 0000000001.995000s: ";
217 ASSERT_GT(sizeof(buffer), kExpected1.size());
218 ASSERT_EQ(
219 kExpected1.size(),
220 static_cast<size_t>(snprintf(
221 buffer, sizeof(buffer), AOS_LOGGING_BASE_FORMAT,
222 AOS_LOGGING_BASE_ARGS(4, "name", 971, 1678, ERROR, 1, 995000000))));
223 EXPECT_EQ(kExpected1, ::std::string(buffer));
224}
225
Tyler Chatow67ddb032020-01-12 14:30:04 -0800226TEST(ScopedLogRestorerTest, RestoreTest) {
Austin Schuha0c41ba2020-09-10 22:59:14 -0700227 SetImplementation(std::make_shared<StreamLogImplementation>(stdout));
228 LogImplementation *curr_impl = GetImplementation().get();
Tyler Chatow67ddb032020-01-12 14:30:04 -0800229
Austin Schuhad9e5eb2021-11-19 20:33:55 -0800230 std::string name = "name";
Tyler Chatow67ddb032020-01-12 14:30:04 -0800231 {
232 ScopedLogRestorer log_restorer;
233
Austin Schuhad9e5eb2021-11-19 20:33:55 -0800234 log_restorer.Swap(std::make_shared<CallbackLogImplementation>(
235 [](const LogMessage &) {}, &name));
Austin Schuha0c41ba2020-09-10 22:59:14 -0700236 ASSERT_NE(curr_impl, GetImplementation().get());
Tyler Chatow67ddb032020-01-12 14:30:04 -0800237 }
238
Austin Schuha0c41ba2020-09-10 22:59:14 -0700239 ASSERT_EQ(curr_impl, GetImplementation().get());
Tyler Chatow67ddb032020-01-12 14:30:04 -0800240}
241
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800242} // namespace aos::logging::testing