milind-u | a96c518 | 2023-03-10 23:31:11 -0800 | [diff] [blame] | 1 | #include <sys/resource.h> |
| 2 | #include <sys/time.h> |
| 3 | |
| 4 | #include "aos/configuration.h" |
| 5 | #include "aos/events/logging/log_writer.h" |
| 6 | #include "aos/events/shm_event_loop.h" |
| 7 | #include "aos/init.h" |
| 8 | #include "aos/logging/log_namer.h" |
| 9 | #include "frc971/input/joystick_state_generated.h" |
| 10 | #include "gflags/gflags.h" |
| 11 | #include "glog/logging.h" |
| 12 | |
| 13 | DEFINE_string(config, "aos_config.json", "Config file to use."); |
| 14 | |
| 15 | DEFINE_double(rotate_every, 0.0, |
| 16 | "If set, rotate the logger after this many seconds"); |
| 17 | DECLARE_int32(flush_size); |
| 18 | DEFINE_double(disabled_time, 5.0, |
| 19 | "Continue logging if disabled for this amount of time or less"); |
| 20 | |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 21 | std::unique_ptr<aos::logger::MultiNodeFilesLogNamer> MakeLogNamer( |
milind-u | a96c518 | 2023-03-10 23:31:11 -0800 | [diff] [blame] | 22 | aos::EventLoop *event_loop) { |
Ravago Jones | cd823f8 | 2023-04-09 16:22:54 -0700 | [diff] [blame^] | 23 | std::optional<std::string> log_name = |
| 24 | aos::logging::MaybeGetLogName("fbs_log"); |
| 25 | |
| 26 | if (!log_name.has_value()) { |
| 27 | return nullptr; |
| 28 | } |
| 29 | |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 30 | return std::make_unique<aos::logger::MultiNodeFilesLogNamer>( |
Ravago Jones | cd823f8 | 2023-04-09 16:22:54 -0700 | [diff] [blame^] | 31 | absl::StrCat(log_name.value(), "/"), event_loop); |
milind-u | a96c518 | 2023-03-10 23:31:11 -0800 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | int main(int argc, char *argv[]) { |
| 35 | gflags::SetUsageMessage( |
| 36 | "This program provides a simple logger binary that logs all SHMEM data " |
| 37 | "directly to a file specified at the command line when the robot is " |
| 38 | "enabled and for a bit of time after."); |
| 39 | aos::InitGoogle(&argc, &argv); |
| 40 | |
| 41 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 42 | aos::configuration::ReadConfig(FLAGS_config); |
| 43 | |
| 44 | aos::ShmEventLoop event_loop(&config.message()); |
| 45 | |
| 46 | bool logging = false; |
| 47 | bool enabled = false; |
| 48 | aos::monotonic_clock::time_point last_disable_time = |
| 49 | event_loop.monotonic_now(); |
| 50 | aos::monotonic_clock::time_point last_rotation_time = |
| 51 | event_loop.monotonic_now(); |
| 52 | aos::logger::Logger logger(&event_loop); |
| 53 | |
| 54 | if (FLAGS_rotate_every != 0.0) { |
| 55 | logger.set_on_logged_period([&] { |
| 56 | const auto now = event_loop.monotonic_now(); |
| 57 | if (logging && now > last_rotation_time + std::chrono::duration<double>( |
| 58 | FLAGS_rotate_every)) { |
| 59 | logger.Rotate(); |
| 60 | last_rotation_time = now; |
| 61 | } |
| 62 | }); |
| 63 | } |
| 64 | |
| 65 | event_loop.OnRun([]() { |
| 66 | errno = 0; |
| 67 | setpriority(PRIO_PROCESS, 0, -20); |
| 68 | PCHECK(errno == 0) << ": Renicing to -20 failed."; |
| 69 | }); |
| 70 | |
| 71 | event_loop.MakeWatcher( |
Yash Chainani | 4b91ff1 | 2023-03-14 19:56:07 -0700 | [diff] [blame] | 72 | "/imu/aos", [&](const aos::JoystickState &joystick_state) { |
milind-u | a96c518 | 2023-03-10 23:31:11 -0800 | [diff] [blame] | 73 | const auto timestamp = event_loop.context().monotonic_event_time; |
| 74 | // Store the last time we got disabled |
| 75 | if (enabled && !joystick_state.enabled()) { |
| 76 | last_disable_time = timestamp; |
| 77 | } |
| 78 | enabled = joystick_state.enabled(); |
| 79 | |
| 80 | if (!logging && enabled) { |
Ravago Jones | cd823f8 | 2023-04-09 16:22:54 -0700 | [diff] [blame^] | 81 | auto log_namer = MakeLogNamer(&event_loop); |
| 82 | if (log_namer == nullptr) { |
| 83 | return; |
| 84 | } |
| 85 | |
milind-u | a96c518 | 2023-03-10 23:31:11 -0800 | [diff] [blame] | 86 | // Start logging if we just got enabled |
| 87 | LOG(INFO) << "Starting logging"; |
Ravago Jones | cd823f8 | 2023-04-09 16:22:54 -0700 | [diff] [blame^] | 88 | logger.StartLogging(std::move(log_namer)); |
milind-u | a96c518 | 2023-03-10 23:31:11 -0800 | [diff] [blame] | 89 | logging = true; |
| 90 | last_rotation_time = event_loop.monotonic_now(); |
| 91 | } else if (logging && !enabled && |
| 92 | (timestamp - last_disable_time) > |
| 93 | std::chrono::duration<double>(FLAGS_disabled_time)) { |
| 94 | // Stop logging if we've been disabled for a non-negligible amount of |
| 95 | // time |
| 96 | LOG(INFO) << "Stopping logging"; |
| 97 | logger.StopLogging(event_loop.monotonic_now()); |
| 98 | logging = false; |
| 99 | } |
| 100 | }); |
| 101 | |
| 102 | event_loop.Run(); |
| 103 | |
| 104 | LOG(INFO) << "Shutting down"; |
| 105 | |
| 106 | return 0; |
| 107 | } |