blob: b87cec0f0e2bc3e51aae14a5bc7b74978e5f5b33 [file] [log] [blame]
milind-ua96c5182023-03-10 23:31:11 -08001#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
13DEFINE_string(config, "aos_config.json", "Config file to use.");
14
15DEFINE_double(rotate_every, 0.0,
16 "If set, rotate the logger after this many seconds");
17DECLARE_int32(flush_size);
18DEFINE_double(disabled_time, 5.0,
19 "Continue logging if disabled for this amount of time or less");
20
21std::unique_ptr<aos::logger::MultiNodeLogNamer> MakeLogNamer(
22 aos::EventLoop *event_loop) {
23 return std::make_unique<aos::logger::MultiNodeLogNamer>(
24 absl::StrCat(aos::logging::GetLogName("fbs_log"), "/"), event_loop);
25}
26
27int main(int argc, char *argv[]) {
28 gflags::SetUsageMessage(
29 "This program provides a simple logger binary that logs all SHMEM data "
30 "directly to a file specified at the command line when the robot is "
31 "enabled and for a bit of time after.");
32 aos::InitGoogle(&argc, &argv);
33
34 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
35 aos::configuration::ReadConfig(FLAGS_config);
36
37 aos::ShmEventLoop event_loop(&config.message());
38
39 bool logging = false;
40 bool enabled = false;
41 aos::monotonic_clock::time_point last_disable_time =
42 event_loop.monotonic_now();
43 aos::monotonic_clock::time_point last_rotation_time =
44 event_loop.monotonic_now();
45 aos::logger::Logger logger(&event_loop);
46
47 if (FLAGS_rotate_every != 0.0) {
48 logger.set_on_logged_period([&] {
49 const auto now = event_loop.monotonic_now();
50 if (logging && now > last_rotation_time + std::chrono::duration<double>(
51 FLAGS_rotate_every)) {
52 logger.Rotate();
53 last_rotation_time = now;
54 }
55 });
56 }
57
58 event_loop.OnRun([]() {
59 errno = 0;
60 setpriority(PRIO_PROCESS, 0, -20);
61 PCHECK(errno == 0) << ": Renicing to -20 failed.";
62 });
63
64 event_loop.MakeWatcher(
65 "/roborio/aos", [&](const aos::JoystickState &joystick_state) {
66 const auto timestamp = event_loop.context().monotonic_event_time;
67 // Store the last time we got disabled
68 if (enabled && !joystick_state.enabled()) {
69 last_disable_time = timestamp;
70 }
71 enabled = joystick_state.enabled();
72
73 if (!logging && enabled) {
74 // Start logging if we just got enabled
75 LOG(INFO) << "Starting logging";
76 logger.StartLogging(MakeLogNamer(&event_loop));
77 logging = true;
78 last_rotation_time = event_loop.monotonic_now();
79 } else if (logging && !enabled &&
80 (timestamp - last_disable_time) >
81 std::chrono::duration<double>(FLAGS_disabled_time)) {
82 // Stop logging if we've been disabled for a non-negligible amount of
83 // time
84 LOG(INFO) << "Stopping logging";
85 logger.StopLogging(event_loop.monotonic_now());
86 logging = false;
87 }
88 });
89
90 event_loop.Run();
91
92 LOG(INFO) << "Shutting down";
93
94 return 0;
95}