blob: 5f98c3565e82b07066b2da29bd2e3c296a6eef0b [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
Austin Schuhad9e5eb2021-11-19 20:33:55 -080027 std::string_view MyName() override {
28 internal::Context *context = internal::Context::Get();
29 return context->MyName();
30 }
31
Alex Perrycb7da4b2019-08-28 19:35:56 -070032 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 0))) void DoLog(
33 log_level level, const char *format, va_list ap) override {
Austin Schuhad9e5eb2021-11-19 20:33:55 -080034 internal::FillInMessage(level, MyName(), monotonic_now(), format, ap,
35 &message_);
Brian Silvermanf665d692013-02-17 22:11:39 -080036
Brian Silvermanfe457de2014-05-26 22:04:08 -070037 if (level == FATAL) {
38 internal::PrintMessage(stderr, message_);
39 abort();
40 }
41
Brian Silvermanf665d692013-02-17 22:11:39 -080042 used_ = true;
43 }
44
45 LogMessage message_;
46
47 public:
48 const LogMessage &message() { return message_; }
49 bool used() { return used_; }
Brian Silvermanf665d692013-02-17 22:11:39 -080050
Austin Schuha0c41ba2020-09-10 22:59:14 -070051 bool used_ = false;
Brian Silvermanf665d692013-02-17 22:11:39 -080052};
53class LoggingTest : public ::testing::Test {
54 protected:
55 AssertionResult WasAnythingLogged() {
56 if (log_implementation->used()) {
Tyler Chatow4b471e12020-01-05 20:19:36 -080057 return AssertionSuccess() << "read message '"
58 << log_implementation->message().message << "'";
Brian Silvermanf665d692013-02-17 22:11:39 -080059 }
60 return AssertionFailure();
61 }
62 AssertionResult WasLogged(log_level level, const std::string message) {
63 if (!log_implementation->used()) {
64 return AssertionFailure() << "nothing was logged";
65 }
66 if (log_implementation->message().level != level) {
Tyler Chatow4b471e12020-01-05 20:19:36 -080067 return AssertionFailure() << "a message with level "
68 << log_str(log_implementation->message().level)
69 << " was logged instead of " << log_str(level);
Brian Silvermanf665d692013-02-17 22:11:39 -080070 }
71 internal::Context *context = internal::Context::Get();
72 if (log_implementation->message().source != context->source) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070073 AOS_LOG(FATAL, "got a message from %" PRIu32 ", but we're %" PRIu32 "\n",
74 static_cast<uint32_t>(log_implementation->message().source),
75 static_cast<uint32_t>(context->source));
Brian Silvermanf665d692013-02-17 22:11:39 -080076 }
Austin Schuhad9e5eb2021-11-19 20:33:55 -080077 if (log_implementation->message().name_length != context->MyName().size() ||
78 memcmp(log_implementation->message().name, context->MyName().data(),
79 context->MyName().size()) != 0) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070080 AOS_LOG(FATAL, "got a message from %.*s, but we're %s\n",
81 static_cast<int>(log_implementation->message().name_length),
Austin Schuhad9e5eb2021-11-19 20:33:55 -080082 log_implementation->message().name,
83 std::string(context->MyName()).c_str());
Brian Silvermanf665d692013-02-17 22:11:39 -080084 }
Tyler Chatow4b471e12020-01-05 20:19:36 -080085 if (strstr(log_implementation->message().message, message.c_str()) ==
86 NULL) {
87 return AssertionFailure()
88 << "got a message of '" << log_implementation->message().message
89 << "' but expected it to contain '" << message << "'";
Brian Silvermanf665d692013-02-17 22:11:39 -080090 }
91
92 return AssertionSuccess() << log_implementation->message().message;
93 }
94
95 private:
Brian Silvermancb5da1f2015-12-05 22:19:58 -050096 void SetUp() override {
Austin Schuha0c41ba2020-09-10 22:59:14 -070097 log_implementation = std::make_shared<TestLogImplementation>();
98 SetImplementation(log_implementation);
Brian Silvermanf665d692013-02-17 22:11:39 -080099 }
Austin Schuha0c41ba2020-09-10 22:59:14 -0700100 void TearDown() override {
101 SetImplementation(nullptr);
Austin Schuha0c41ba2020-09-10 22:59:14 -0700102 internal::Context::DeleteNow();
103 CHECK_EQ(log_implementation.use_count(), 1);
104 log_implementation.reset();
105 }
Brian Silvermanf665d692013-02-17 22:11:39 -0800106
Austin Schuha0c41ba2020-09-10 22:59:14 -0700107 std::shared_ptr<TestLogImplementation> log_implementation;
Brian Silvermanf665d692013-02-17 22:11:39 -0800108};
Brian Silvermanf665d692013-02-17 22:11:39 -0800109typedef LoggingTest LoggingDeathTest;
110
111// Tests both basic logging functionality and that the test setup works
112// correctly.
113TEST_F(LoggingTest, Basic) {
114 EXPECT_FALSE(WasAnythingLogged());
Austin Schuhf257f3c2019-10-27 21:00:43 -0700115 AOS_LOG(INFO, "test log 1\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800116 EXPECT_TRUE(WasLogged(INFO, "test log 1\n"));
Austin Schuhf257f3c2019-10-27 21:00:43 -0700117 AOS_LOG(INFO, "test log 1.5\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800118 // there's a subtle typo on purpose...
119 EXPECT_FALSE(WasLogged(INFO, "test log 15\n"));
Austin Schuhf257f3c2019-10-27 21:00:43 -0700120 AOS_LOG(ERROR, "test log 2=%d\n", 55);
Brian Silvermanf665d692013-02-17 22:11:39 -0800121 EXPECT_TRUE(WasLogged(ERROR, "test log 2=55\n"));
Austin Schuhf257f3c2019-10-27 21:00:43 -0700122 AOS_LOG(ERROR, "test log 3\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800123 EXPECT_FALSE(WasLogged(WARNING, "test log 3\n"));
Austin Schuhf257f3c2019-10-27 21:00:43 -0700124 AOS_LOG(WARNING, "test log 4\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800125 EXPECT_TRUE(WasAnythingLogged());
126}
Brian Silvermanf665d692013-02-17 22:11:39 -0800127
Brian Silvermanf665d692013-02-17 22:11:39 -0800128TEST_F(LoggingDeathTest, Fatal) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700129 ASSERT_EXIT(AOS_LOG(FATAL, "this should crash it\n"),
130 ::testing::KilledBySignal(SIGABRT), "this should crash it");
Brian Silvermanf665d692013-02-17 22:11:39 -0800131}
Brian Silvermanfe457de2014-05-26 22:04:08 -0700132
133TEST_F(LoggingDeathTest, PCHECK) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700134 EXPECT_DEATH(AOS_PCHECK(fprintf(stdin, "nope")),
Brian Silvermanfe457de2014-05-26 22:04:08 -0700135 ".*fprintf\\(stdin, \"nope\"\\).*failed.*");
136}
137
Austin Schuhf257f3c2019-10-27 21:00:43 -0700138TEST_F(LoggingTest, PCHECK) { EXPECT_EQ(7, AOS_PCHECK(printf("abc123\n"))); }
Brian Silvermanf665d692013-02-17 22:11:39 -0800139
140TEST_F(LoggingTest, PrintfDirectives) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700141 AOS_LOG(INFO, "test log %%1 %%d\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800142 EXPECT_TRUE(WasLogged(INFO, "test log %1 %d\n"));
Austin Schuhf257f3c2019-10-27 21:00:43 -0700143 AOS_LOG_DYNAMIC(WARNING, "test log %%2 %%f\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800144 EXPECT_TRUE(WasLogged(WARNING, "test log %2 %f\n"));
Brian Silvermanf665d692013-02-17 22:11:39 -0800145}
146
147TEST_F(LoggingTest, Timing) {
148 // For writing only.
Tyler Chatow4b471e12020-01-05 20:19:36 -0800149 // static const long kTimingCycles = 5000000;
Brian Silvermanf665d692013-02-17 22:11:39 -0800150 static const long kTimingCycles = 5000;
151
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800152 monotonic_clock::time_point start = monotonic_clock::now();
Brian Silvermanf665d692013-02-17 22:11:39 -0800153 for (long i = 0; i < kTimingCycles; ++i) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700154 AOS_LOG(INFO, "a\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800155 }
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800156 monotonic_clock::time_point end = monotonic_clock::now();
157 auto diff = end - start;
Brian Silverman41abe012014-02-08 18:25:02 -0800158 printf("short message took %" PRId64 " nsec for %ld\n",
Alex Perrycb7da4b2019-08-28 19:35:56 -0700159 static_cast<int64_t>(
160 chrono::duration_cast<chrono::nanoseconds>(diff).count()),
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800161 kTimingCycles);
Brian Silvermanf665d692013-02-17 22:11:39 -0800162
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800163 start = monotonic_clock::now();
Brian Silvermanf665d692013-02-17 22:11:39 -0800164 for (long i = 0; i < kTimingCycles; ++i) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700165 AOS_LOG(INFO, "something longer than just \"a\" to log to test timing\n");
Brian Silvermanf665d692013-02-17 22:11:39 -0800166 }
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800167 end = monotonic_clock::now();
Brian Silvermanf665d692013-02-17 22:11:39 -0800168 diff = end - start;
Brian Silverman41abe012014-02-08 18:25:02 -0800169 printf("long message took %" PRId64 " nsec for %ld\n",
Alex Perrycb7da4b2019-08-28 19:35:56 -0700170 static_cast<int64_t>(
171 chrono::duration_cast<chrono::nanoseconds>(diff).count()),
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800172 kTimingCycles);
Brian Silvermanf665d692013-02-17 22:11:39 -0800173}
174
Brian Silverman54a368e2015-02-14 20:05:33 -0500175TEST(LoggingPrintFormatTest, Time) {
176 char buffer[1024];
177
178 // Easy ones.
179 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
180 AOS_TIME_ARGS(2, 0)));
181 EXPECT_EQ("0000000002.000000s", ::std::string(buffer));
182 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
183 AOS_TIME_ARGS(2, 1)));
184 EXPECT_EQ("0000000002.000000s", ::std::string(buffer));
185
186 // This one should be exact.
187 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
188 AOS_TIME_ARGS(2, 1000)));
189 EXPECT_EQ("0000000002.000001s", ::std::string(buffer));
190
191 // Make sure rounding works correctly.
192 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
193 AOS_TIME_ARGS(2, 999)));
Austin Schuh648b3612017-11-20 01:02:24 -0800194 EXPECT_EQ("0000000002.000000s", ::std::string(buffer));
Brian Silverman54a368e2015-02-14 20:05:33 -0500195 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
196 AOS_TIME_ARGS(2, 500)));
Austin Schuh648b3612017-11-20 01:02:24 -0800197 EXPECT_EQ("0000000002.000000s", ::std::string(buffer));
Brian Silverman54a368e2015-02-14 20:05:33 -0500198 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
199 AOS_TIME_ARGS(2, 499)));
200 EXPECT_EQ("0000000002.000000s", ::std::string(buffer));
201
202 // This used to result in "0000000001.099500s".
203 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
204 AOS_TIME_ARGS(1, 995000000)));
205 EXPECT_EQ("0000000001.995000s", ::std::string(buffer));
Austin Schuh648b3612017-11-20 01:02:24 -0800206
207 // This used to result in "0000000001.099500s".
208 ASSERT_EQ(18, snprintf(buffer, sizeof(buffer), AOS_TIME_FORMAT,
209 AOS_TIME_ARGS(1, 999999999)));
210 EXPECT_EQ("0000000001.999999s", ::std::string(buffer));
Brian Silverman54a368e2015-02-14 20:05:33 -0500211}
212
213TEST(LoggingPrintFormatTest, Base) {
214 char buffer[1024];
215
216 static const ::std::string kExpected1 =
217 "name(971)(01678): ERROR at 0000000001.995000s: ";
218 ASSERT_GT(sizeof(buffer), kExpected1.size());
219 ASSERT_EQ(
220 kExpected1.size(),
221 static_cast<size_t>(snprintf(
222 buffer, sizeof(buffer), AOS_LOGGING_BASE_FORMAT,
223 AOS_LOGGING_BASE_ARGS(4, "name", 971, 1678, ERROR, 1, 995000000))));
224 EXPECT_EQ(kExpected1, ::std::string(buffer));
225}
226
Tyler Chatow67ddb032020-01-12 14:30:04 -0800227TEST(ScopedLogRestorerTest, RestoreTest) {
Austin Schuha0c41ba2020-09-10 22:59:14 -0700228 SetImplementation(std::make_shared<StreamLogImplementation>(stdout));
229 LogImplementation *curr_impl = GetImplementation().get();
Tyler Chatow67ddb032020-01-12 14:30:04 -0800230
Austin Schuhad9e5eb2021-11-19 20:33:55 -0800231 std::string name = "name";
Tyler Chatow67ddb032020-01-12 14:30:04 -0800232 {
233 ScopedLogRestorer log_restorer;
234
Austin Schuhad9e5eb2021-11-19 20:33:55 -0800235 log_restorer.Swap(std::make_shared<CallbackLogImplementation>(
236 [](const LogMessage &) {}, &name));
Austin Schuha0c41ba2020-09-10 22:59:14 -0700237 ASSERT_NE(curr_impl, GetImplementation().get());
Tyler Chatow67ddb032020-01-12 14:30:04 -0800238 }
239
Austin Schuha0c41ba2020-09-10 22:59:14 -0700240 ASSERT_EQ(curr_impl, GetImplementation().get());
Tyler Chatow67ddb032020-01-12 14:30:04 -0800241}
242
Brian Silvermanf665d692013-02-17 22:11:39 -0800243} // namespace testing
244} // namespace logging
245} // namespace aos