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