blob: 526eb3141269af1af4f47f226555ce113fa92e16 [file] [log] [blame]
Austin Schuh47aabde2020-10-10 17:46:37 -07001#include <sys/resource.h>
2#include <sys/time.h>
3
James Kuszmaul38735e82019-12-07 16:42:06 -08004#include "aos/configuration.h"
Austin Schuhb06f03b2021-02-17 22:00:37 -08005#include "aos/events/logging/log_writer.h"
James Kuszmauldd0a5042021-10-28 23:38:04 -07006#include "aos/events/logging/snappy_encoder.h"
James Kuszmaul38735e82019-12-07 16:42:06 -08007#include "aos/events/shm_event_loop.h"
8#include "aos/init.h"
Brian Silvermand90905f2020-09-23 14:42:56 -07009#include "aos/logging/log_namer.h"
James Kuszmaul38735e82019-12-07 16:42:06 -080010#include "gflags/gflags.h"
11#include "glog/logging.h"
12
Austin Schuhc5fa6d92022-02-25 14:36:28 -080013DEFINE_string(config, "aos_config.json", "Config file to use.");
James Kuszmaul38735e82019-12-07 16:42:06 -080014
Austin Schuh27553152020-11-18 21:26:37 -080015DEFINE_bool(skip_renicing, false,
16 "If true, skip renicing the logger. This leaves it lower priority "
17 "and increases the likelihood of dropping messages and crashing.");
18
James Kuszmauldd0a5042021-10-28 23:38:04 -070019DEFINE_bool(snappy_compress, false, "If true, compress log data using snappy.");
20
Milind Upadhyayb2b8cd82022-03-21 08:51:32 -070021DEFINE_double(rotate_every, 0.0,
22 "If set, rotate the logger after this many seconds");
23
James Kuszmaul38735e82019-12-07 16:42:06 -080024int main(int argc, char *argv[]) {
25 gflags::SetUsageMessage(
26 "This program provides a simple logger binary that logs all SHMEM data "
27 "directly to a file specified at the command line. It does not manage "
28 "filenames, so it will just crash if you attempt to overwrite an "
29 "existing file, and the user must specify the logfile manually at the "
30 "command line.");
31 aos::InitGoogle(&argc, &argv);
32
33 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
34 aos::configuration::ReadConfig(FLAGS_config);
35
36 aos::ShmEventLoop event_loop(&config.message());
37
James Kuszmauldd0a5042021-10-28 23:38:04 -070038 std::unique_ptr<aos::logger::MultiNodeLogNamer> log_namer;
Milind Upadhyayd3a7bdc2020-12-26 16:07:03 -080039 log_namer = std::make_unique<aos::logger::MultiNodeLogNamer>(
Austin Schuha499cea2021-07-31 19:49:53 -070040 absl::StrCat(aos::logging::GetLogName("fbs_log"), "/"), &event_loop);
Austin Schuhcde938c2020-02-02 17:30:07 -080041
James Kuszmauldd0a5042021-10-28 23:38:04 -070042 if (FLAGS_snappy_compress) {
43 log_namer->set_extension(aos::logger::SnappyDecoder::kExtension);
44 log_namer->set_encoder_factory(
45 []() { return std::make_unique<aos::logger::SnappyEncoder>(); });
46 }
47
Austin Schuhb4674632022-09-19 19:12:13 -070048 aos::monotonic_clock::time_point last_rotation_time =
49 event_loop.monotonic_now();
Brian Silverman1f345222020-09-24 21:14:48 -070050 aos::logger::Logger logger(&event_loop);
Milind Upadhyayb2b8cd82022-03-21 08:51:32 -070051
52 if (FLAGS_rotate_every != 0.0) {
Milind Upadhyayb2b8cd82022-03-21 08:51:32 -070053 logger.set_on_logged_period([&] {
54 const auto now = event_loop.monotonic_now();
55 if (now > last_rotation_time +
56 std::chrono::duration<double>(FLAGS_rotate_every)) {
57 logger.Rotate();
58 last_rotation_time = now;
59 }
60 });
61 }
62
Austin Schuh47aabde2020-10-10 17:46:37 -070063 event_loop.OnRun([&log_namer, &logger]() {
Austin Schuh27553152020-11-18 21:26:37 -080064 if (FLAGS_skip_renicing) {
65 LOG(WARNING) << "Ignoring request to renice to -20 due to "
66 "--skip_renicing.";
67 } else {
68 errno = 0;
69 setpriority(PRIO_PROCESS, 0, -20);
70 PCHECK(errno == 0)
71 << ": Renicing to -20 failed, use --skip_renicing to skip renicing.";
72 }
Austin Schuh47aabde2020-10-10 17:46:37 -070073 logger.StartLogging(std::move(log_namer));
74 });
James Kuszmaul38735e82019-12-07 16:42:06 -080075
76 event_loop.Run();
77
Austin Schuh2f8fd752020-09-01 22:38:28 -070078 LOG(INFO) << "Shutting down";
79
James Kuszmaul38735e82019-12-07 16:42:06 -080080 return 0;
81}