blob: 29a95db87302ab9d902fbda37be2fc90af1e4b25 [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
Alexei Strots01395492023-03-20 13:59:56 -070021std::unique_ptr<aos::logger::MultiNodeFilesLogNamer> MakeLogNamer(
milind-ua96c5182023-03-10 23:31:11 -080022 aos::EventLoop *event_loop) {
Ravago Jonescd823f82023-04-09 16:22:54 -070023 std::optional<std::string> log_name =
24 aos::logging::MaybeGetLogName("fbs_log");
25
26 if (!log_name.has_value()) {
27 return nullptr;
28 }
29
Alexei Strots01395492023-03-20 13:59:56 -070030 return std::make_unique<aos::logger::MultiNodeFilesLogNamer>(
Ravago Jonescd823f82023-04-09 16:22:54 -070031 absl::StrCat(log_name.value(), "/"), event_loop);
milind-ua96c5182023-03-10 23:31:11 -080032}
33
34int main(int argc, char *argv[]) {
35 gflags::SetUsageMessage(
36 "This program provides a simple logger binary that logs all SHMEM data "
37 "directly to a file specified at the command line when the robot is "
38 "enabled and for a bit of time after.");
39 aos::InitGoogle(&argc, &argv);
40
41 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
42 aos::configuration::ReadConfig(FLAGS_config);
43
44 aos::ShmEventLoop event_loop(&config.message());
45
46 bool logging = false;
47 bool enabled = false;
48 aos::monotonic_clock::time_point last_disable_time =
49 event_loop.monotonic_now();
50 aos::monotonic_clock::time_point last_rotation_time =
51 event_loop.monotonic_now();
52 aos::logger::Logger logger(&event_loop);
53
54 if (FLAGS_rotate_every != 0.0) {
55 logger.set_on_logged_period([&] {
56 const auto now = event_loop.monotonic_now();
57 if (logging && now > last_rotation_time + std::chrono::duration<double>(
58 FLAGS_rotate_every)) {
59 logger.Rotate();
60 last_rotation_time = now;
61 }
62 });
63 }
64
65 event_loop.OnRun([]() {
66 errno = 0;
67 setpriority(PRIO_PROCESS, 0, -20);
68 PCHECK(errno == 0) << ": Renicing to -20 failed.";
69 });
70
71 event_loop.MakeWatcher(
Yash Chainani4b91ff12023-03-14 19:56:07 -070072 "/imu/aos", [&](const aos::JoystickState &joystick_state) {
milind-ua96c5182023-03-10 23:31:11 -080073 const auto timestamp = event_loop.context().monotonic_event_time;
74 // Store the last time we got disabled
75 if (enabled && !joystick_state.enabled()) {
76 last_disable_time = timestamp;
77 }
78 enabled = joystick_state.enabled();
79
80 if (!logging && enabled) {
Ravago Jonescd823f82023-04-09 16:22:54 -070081 auto log_namer = MakeLogNamer(&event_loop);
82 if (log_namer == nullptr) {
83 return;
84 }
85
milind-ua96c5182023-03-10 23:31:11 -080086 // Start logging if we just got enabled
87 LOG(INFO) << "Starting logging";
Ravago Jonescd823f82023-04-09 16:22:54 -070088 logger.StartLogging(std::move(log_namer));
milind-ua96c5182023-03-10 23:31:11 -080089 logging = true;
90 last_rotation_time = event_loop.monotonic_now();
91 } else if (logging && !enabled &&
92 (timestamp - last_disable_time) >
93 std::chrono::duration<double>(FLAGS_disabled_time)) {
94 // Stop logging if we've been disabled for a non-negligible amount of
95 // time
96 LOG(INFO) << "Stopping logging";
97 logger.StopLogging(event_loop.monotonic_now());
98 logging = false;
99 }
100 });
101
102 event_loop.Run();
103
104 LOG(INFO) << "Shutting down";
105
106 return 0;
107}