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