Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 1 | #include "aos/logging/dynamic_logging.h" |
| 2 | |
Sarah Newman | a7e8793 | 2022-04-11 15:00:03 -0700 | [diff] [blame] | 3 | #include <sys/stat.h> |
| 4 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 5 | #include "glog/logging.h" |
| 6 | #include "gtest/gtest.h" |
| 7 | |
Sarah Newman | a7e8793 | 2022-04-11 15:00:03 -0700 | [diff] [blame] | 8 | #include "aos/events/event_loop.h" |
| 9 | #include "aos/events/simulated_event_loop.h" |
Sarah Newman | a7e8793 | 2022-04-11 15:00:03 -0700 | [diff] [blame] | 10 | #include "aos/testing/path.h" |
Sarah Newman | a7e8793 | 2022-04-11 15:00:03 -0700 | [diff] [blame] | 11 | |
| 12 | using aos::testing::ArtifactPath; |
| 13 | |
| 14 | namespace aos { |
| 15 | namespace logging { |
| 16 | namespace testing { |
| 17 | |
| 18 | namespace chrono = std::chrono; |
| 19 | |
| 20 | class DynamicLoggingTest : public ::testing::Test { |
| 21 | public: |
| 22 | DynamicLoggingTest() |
| 23 | : config_(aos::configuration::ReadConfig( |
| 24 | ArtifactPath("aos/events/pingpong_config.json"))), |
| 25 | event_loop_factory_(&config_.message()), |
| 26 | event_loop_send_(event_loop_factory_.MakeEventLoop("send")), |
| 27 | event_loop_main_(event_loop_factory_.MakeEventLoop("main")) {} |
| 28 | |
| 29 | aos::FlatbufferDetachedBuffer<aos::Configuration> config_; |
| 30 | SimulatedEventLoopFactory event_loop_factory_; |
| 31 | |
| 32 | std::unique_ptr<EventLoop> event_loop_send_; |
| 33 | std::unique_ptr<EventLoop> event_loop_main_; |
| 34 | }; |
| 35 | |
| 36 | TEST_F(DynamicLoggingTest, TestVLog) { |
| 37 | aos::Sender<DynamicLogCommand> dynamic_log_command_sender = |
| 38 | event_loop_send_->MakeSender<DynamicLogCommand>("/aos"); |
| 39 | |
| 40 | // Set VLOG level to 1 at t=50us and then back to 0 at t=150us. |
| 41 | int log_level = 1; |
| 42 | aos::TimerHandler *timer_handler = event_loop_send_->AddTimer( |
| 43 | [this, &dynamic_log_command_sender, &log_level, &timer_handler]() { |
| 44 | aos::Sender<DynamicLogCommand>::Builder message = |
| 45 | dynamic_log_command_sender.MakeBuilder(); |
| 46 | const auto name_str = message.fbb()->CreateString("main"); |
| 47 | |
| 48 | DynamicLogCommand::Builder builder = |
| 49 | message.MakeBuilder<DynamicLogCommand>(); |
| 50 | builder.add_name(name_str); |
| 51 | builder.add_vlog_level(log_level); |
| 52 | CHECK_EQ(message.Send(builder.Finish()), RawSender::Error::kOk); |
| 53 | --log_level; |
| 54 | if (log_level >= 0) { |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 55 | timer_handler->Schedule(event_loop_send_->monotonic_now() + |
| 56 | chrono::microseconds(100)); |
Sarah Newman | a7e8793 | 2022-04-11 15:00:03 -0700 | [diff] [blame] | 57 | } |
| 58 | }); |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 59 | timer_handler->Schedule(event_loop_send_->monotonic_now() + |
| 60 | chrono::microseconds(50)); |
Sarah Newman | a7e8793 | 2022-04-11 15:00:03 -0700 | [diff] [blame] | 61 | |
| 62 | // VLOG(1) at t=0us, t=100us, t=200us |
| 63 | aos::TimerHandler *vlog_timer_handler = |
| 64 | event_loop_main_->AddTimer([]() { VLOG(1) << "VLOG 1"; }); |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 65 | vlog_timer_handler->Schedule(event_loop_main_->monotonic_now(), |
| 66 | chrono::microseconds(100)); |
Sarah Newman | a7e8793 | 2022-04-11 15:00:03 -0700 | [diff] [blame] | 67 | |
| 68 | DynamicLogging dynamic_logging(event_loop_main_.get()); |
| 69 | |
| 70 | { |
| 71 | // Validate no log message in first 50us. |
| 72 | ::testing::internal::CaptureStderr(); |
| 73 | event_loop_factory_.RunFor(chrono::microseconds(50)); |
| 74 | std::string output = ::testing::internal::GetCapturedStderr(); |
| 75 | EXPECT_EQ(output.find("VLOG 1"), std::string::npos); |
| 76 | } |
| 77 | { |
| 78 | // Validate 1 log message between 50us to 150us |
| 79 | ::testing::internal::CaptureStderr(); |
| 80 | event_loop_factory_.RunFor(chrono::microseconds(100)); |
| 81 | std::string output = ::testing::internal::GetCapturedStderr(); |
| 82 | EXPECT_NE(output.find("VLOG 1"), std::string::npos); |
| 83 | } |
| 84 | { |
| 85 | // Validate no log message between 150us to 250us |
| 86 | ::testing::internal::CaptureStderr(); |
| 87 | event_loop_factory_.RunFor(chrono::microseconds(100)); |
| 88 | std::string output = ::testing::internal::GetCapturedStderr(); |
| 89 | EXPECT_EQ(output.find("VLOG 1"), std::string::npos); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | } // namespace testing |
| 94 | } // namespace logging |
| 95 | } // namespace aos |