blob: a7c233df6f3d1504b7409a897777a0dda2a86623 [file] [log] [blame]
milind-ua96c5182023-03-10 23:31:11 -08001#include <sys/resource.h>
2#include <sys/time.h>
3
Austin Schuh99f7c6a2024-06-25 22:07:44 -07004#include "absl/flags/flag.h"
5#include "absl/flags/usage.h"
6#include "absl/log/check.h"
7#include "absl/log/log.h"
Philipp Schrader790cb542023-07-05 21:06:52 -07008
milind-ua96c5182023-03-10 23:31:11 -08009#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-ua96c5182023-03-10 23:31:11 -080015
Austin Schuh99f7c6a2024-06-25 22:07:44 -070016ABSL_FLAG(std::string, config, "aos_config.json", "Config file to use.");
milind-ua96c5182023-03-10 23:31:11 -080017
Austin Schuh99f7c6a2024-06-25 22:07:44 -070018ABSL_FLAG(double, rotate_every, 0.0,
19 "If set, rotate the logger after this many seconds");
20ABSL_DECLARE_FLAG(int32_t, flush_size);
21ABSL_FLAG(double, disabled_time, 5.0,
22 "Continue logging if disabled for this amount of time or less");
23ABSL_FLAG(bool, direct, false,
24 "If true, write using O_DIRECT and write 512 byte aligned blocks "
25 "whenever possible.");
milind-ua96c5182023-03-10 23:31:11 -080026
Alexei Strots01395492023-03-20 13:59:56 -070027std::unique_ptr<aos::logger::MultiNodeFilesLogNamer> MakeLogNamer(
milind-ua96c5182023-03-10 23:31:11 -080028 aos::EventLoop *event_loop) {
Ravago Jonescd823f82023-04-09 16:22:54 -070029 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 Strots01395492023-03-20 13:59:56 -070036 return std::make_unique<aos::logger::MultiNodeFilesLogNamer>(
Austin Schuh99f7c6a2024-06-25 22:07:44 -070037 event_loop,
38 std::make_unique<aos::logger::RenamableFileBackend>(
39 absl::StrCat(log_name.value(), "/"), absl::GetFlag(FLAGS_direct)));
milind-ua96c5182023-03-10 23:31:11 -080040}
41
42int main(int argc, char *argv[]) {
Austin Schuh99f7c6a2024-06-25 22:07:44 -070043 absl::SetProgramUsageMessage(
milind-ua96c5182023-03-10 23:31:11 -080044 "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 Schuh99f7c6a2024-06-25 22:07:44 -070050 aos::configuration::ReadConfig(absl::GetFlag(FLAGS_config));
milind-ua96c5182023-03-10 23:31:11 -080051
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 Schuh99f7c6a2024-06-25 22:07:44 -070062 if (absl::GetFlag(FLAGS_rotate_every) != 0.0) {
Austin Schuh2f864452023-07-17 14:53:08 -070063 logger.set_on_logged_period([&](aos::monotonic_clock::time_point) {
milind-ua96c5182023-03-10 23:31:11 -080064 const auto now = event_loop.monotonic_now();
Austin Schuh99f7c6a2024-06-25 22:07:44 -070065 if (logging &&
66 now > last_rotation_time + std::chrono::duration<double>(
67 absl::GetFlag(FLAGS_rotate_every))) {
milind-ua96c5182023-03-10 23:31:11 -080068 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 Chainani4b91ff12023-03-14 19:56:07 -070081 "/imu/aos", [&](const aos::JoystickState &joystick_state) {
milind-ua96c5182023-03-10 23:31:11 -080082 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 Jonescd823f82023-04-09 16:22:54 -070090 auto log_namer = MakeLogNamer(&event_loop);
91 if (log_namer == nullptr) {
92 return;
93 }
94
milind-ua96c5182023-03-10 23:31:11 -080095 // Start logging if we just got enabled
96 LOG(INFO) << "Starting logging";
Ravago Jonescd823f82023-04-09 16:22:54 -070097 logger.StartLogging(std::move(log_namer));
milind-ua96c5182023-03-10 23:31:11 -080098 logging = true;
99 last_rotation_time = event_loop.monotonic_now();
100 } else if (logging && !enabled &&
101 (timestamp - last_disable_time) >
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700102 std::chrono::duration<double>(
103 absl::GetFlag(FLAGS_disabled_time))) {
milind-ua96c5182023-03-10 23:31:11 -0800104 // 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}