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