blob: c571bd7511e625dc6b4ba8b1a85325ef5e193711 [file] [log] [blame]
James Kuszmaulecafe1f2024-02-27 20:29:53 -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"
James Kuszmaulecafe1f2024-02-27 20:29:53 -08008
9#include "aos/configuration.h"
10#include "aos/events/logging/log_writer.h"
11#include "aos/events/logging/snappy_encoder.h"
12#include "aos/events/shm_event_loop.h"
13#include "aos/init.h"
14#include "aos/logging/log_namer.h"
15
Austin Schuh99f7c6a2024-06-25 22:07:44 -070016ABSL_FLAG(std::string, config, "aos_config.json", "Config file to use.");
James Kuszmaulecafe1f2024-02-27 20:29:53 -080017
Austin Schuh99f7c6a2024-06-25 22:07:44 -070018ABSL_FLAG(double, rotate_every, 30.0,
19 "If set, rotate the logger after this many seconds");
James Kuszmaulecafe1f2024-02-27 20:29:53 -080020
Austin Schuh99f7c6a2024-06-25 22:07:44 -070021ABSL_DECLARE_FLAG(int32_t, flush_size);
James Kuszmaulecafe1f2024-02-27 20:29:53 -080022
23int main(int argc, char *argv[]) {
Austin Schuh99f7c6a2024-06-25 22:07:44 -070024 absl::SetProgramUsageMessage(
James Kuszmaulecafe1f2024-02-27 20:29:53 -080025 "This program provides a simple logger binary that logs all SHMEM data "
26 "directly to a file specified at the command line. It does not manage "
27 "filenames, so it will just crash if you attempt to overwrite an "
28 "existing file, and the user must specify the logfile manually at the "
29 "command line.");
30 aos::InitGoogle(&argc, &argv);
31
32 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
Austin Schuh99f7c6a2024-06-25 22:07:44 -070033 aos::configuration::ReadConfig(absl::GetFlag(FLAGS_config));
James Kuszmaulecafe1f2024-02-27 20:29:53 -080034
35 aos::ShmEventLoop event_loop(&config.message());
36
37 auto log_namer = std::make_unique<aos::logger::MultiNodeFilesLogNamer>(
38 &event_loop,
39 std::make_unique<aos::logger::RenamableFileBackend>(
40 absl::StrCat(aos::logging::GetLogName("localizer_log"), "/"),
41 /*O_DIRECT*/ true));
42
43 log_namer->set_extension(aos::logger::SnappyDecoder::kExtension);
44 log_namer->set_encoder_factory([](size_t max_message_size) {
Austin Schuh99f7c6a2024-06-25 22:07:44 -070045 return std::make_unique<aos::logger::SnappyEncoder>(
46 max_message_size, absl::GetFlag(FLAGS_flush_size));
James Kuszmaulecafe1f2024-02-27 20:29:53 -080047 });
48
49 aos::monotonic_clock::time_point last_rotation_time =
50 event_loop.monotonic_now();
51 aos::logger::Logger logger(
52 &event_loop, event_loop.configuration(),
53 // Only log channels smaller than ~10 MB / sec.
54 [](const aos::Channel *channel) {
55 return (channel->max_size() * channel->frequency()) < 10e6;
56 });
57
Austin Schuh99f7c6a2024-06-25 22:07:44 -070058 if (absl::GetFlag(FLAGS_rotate_every) != 0.0) {
James Kuszmaulecafe1f2024-02-27 20:29:53 -080059 logger.set_on_logged_period(
60 [&logger, &last_rotation_time](aos::monotonic_clock::time_point t) {
Austin Schuh99f7c6a2024-06-25 22:07:44 -070061 if (t > last_rotation_time + std::chrono::duration<double>(
62 absl::GetFlag(FLAGS_rotate_every))) {
James Kuszmaulecafe1f2024-02-27 20:29:53 -080063 logger.Rotate();
64 last_rotation_time = t;
65 }
66 });
67 }
68
69 event_loop.OnRun([&log_namer, &logger]() {
70 errno = 0;
71 setpriority(PRIO_PROCESS, 0, -20);
72 PCHECK(errno == 0)
73 << ": Renicing to -20 failed, use --skip_renicing to skip renicing.";
74 logger.StartLogging(std::move(log_namer));
75 });
76
77 event_loop.Run();
78
79 LOG(INFO) << "Shutting down";
80
81 return 0;
82}