blob: 631f41f86cf8730e27d80159ce062dc4233edce5 [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"
Austin Schuh48d10d62022-10-16 22:19:23 -07006#ifdef LZMA
7#include "aos/events/logging/lzma_encoder.h"
8#endif
Philipp Schrader790cb542023-07-05 21:06:52 -07009#include "gflags/gflags.h"
10#include "glog/logging.h"
11
James Kuszmauldd0a5042021-10-28 23:38:04 -070012#include "aos/events/logging/snappy_encoder.h"
James Kuszmaul38735e82019-12-07 16:42:06 -080013#include "aos/events/shm_event_loop.h"
14#include "aos/init.h"
Brian Silvermand90905f2020-09-23 14:42:56 -070015#include "aos/logging/log_namer.h"
James Kuszmaul38735e82019-12-07 16:42:06 -080016
Austin Schuhc5fa6d92022-02-25 14:36:28 -080017DEFINE_string(config, "aos_config.json", "Config file to use.");
James Kuszmaul38735e82019-12-07 16:42:06 -080018
Austin Schuh27553152020-11-18 21:26:37 -080019DEFINE_bool(skip_renicing, false,
20 "If true, skip renicing the logger. This leaves it lower priority "
21 "and increases the likelihood of dropping messages and crashing.");
22
James Kuszmauldd0a5042021-10-28 23:38:04 -070023DEFINE_bool(snappy_compress, false, "If true, compress log data using snappy.");
24
Austin Schuh48d10d62022-10-16 22:19:23 -070025#ifdef LZMA
26DEFINE_bool(xz_compress, false, "If true, compress log data using xz.");
27#endif
28
Milind Upadhyayb2b8cd82022-03-21 08:51:32 -070029DEFINE_double(rotate_every, 0.0,
30 "If set, rotate the logger after this many seconds");
31
Austin Schuh48d10d62022-10-16 22:19:23 -070032#ifdef LZMA
33DEFINE_int32(xz_compression_level, 9, "Compression level for the LZMA Encoder");
34#endif
35
Austin Schuh8bdfc492023-02-11 12:53:13 -080036DECLARE_int32(flush_size);
37
James Kuszmaul38735e82019-12-07 16:42:06 -080038int main(int argc, char *argv[]) {
39 gflags::SetUsageMessage(
40 "This program provides a simple logger binary that logs all SHMEM data "
41 "directly to a file specified at the command line. It does not manage "
42 "filenames, so it will just crash if you attempt to overwrite an "
43 "existing file, and the user must specify the logfile manually at the "
44 "command line.");
45 aos::InitGoogle(&argc, &argv);
46
47 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
48 aos::configuration::ReadConfig(FLAGS_config);
49
50 aos::ShmEventLoop event_loop(&config.message());
51
Alexei Strots01395492023-03-20 13:59:56 -070052 auto log_namer = std::make_unique<aos::logger::MultiNodeFilesLogNamer>(
Austin Schuha499cea2021-07-31 19:49:53 -070053 absl::StrCat(aos::logging::GetLogName("fbs_log"), "/"), &event_loop);
Austin Schuhcde938c2020-02-02 17:30:07 -080054
James Kuszmauldd0a5042021-10-28 23:38:04 -070055 if (FLAGS_snappy_compress) {
56 log_namer->set_extension(aos::logger::SnappyDecoder::kExtension);
Austin Schuh48d10d62022-10-16 22:19:23 -070057 log_namer->set_encoder_factory([](size_t max_message_size) {
Austin Schuh8bdfc492023-02-11 12:53:13 -080058 return std::make_unique<aos::logger::SnappyEncoder>(max_message_size,
59 FLAGS_flush_size);
Austin Schuh48d10d62022-10-16 22:19:23 -070060 });
61#ifdef LZMA
62 } else if (FLAGS_xz_compress) {
63 log_namer->set_extension(aos::logger::LzmaEncoder::kExtension);
64 log_namer->set_encoder_factory([](size_t max_message_size) {
65 return std::make_unique<aos::logger::LzmaEncoder>(
Austin Schuh8bdfc492023-02-11 12:53:13 -080066 max_message_size, FLAGS_xz_compression_level, FLAGS_flush_size);
Austin Schuh48d10d62022-10-16 22:19:23 -070067 });
68#endif
James Kuszmauldd0a5042021-10-28 23:38:04 -070069 }
70
Austin Schuhb4674632022-09-19 19:12:13 -070071 aos::monotonic_clock::time_point last_rotation_time =
72 event_loop.monotonic_now();
Brian Silverman1f345222020-09-24 21:14:48 -070073 aos::logger::Logger logger(&event_loop);
Milind Upadhyayb2b8cd82022-03-21 08:51:32 -070074
75 if (FLAGS_rotate_every != 0.0) {
Milind Upadhyayb2b8cd82022-03-21 08:51:32 -070076 logger.set_on_logged_period([&] {
77 const auto now = event_loop.monotonic_now();
78 if (now > last_rotation_time +
79 std::chrono::duration<double>(FLAGS_rotate_every)) {
80 logger.Rotate();
81 last_rotation_time = now;
82 }
83 });
84 }
85
Austin Schuh47aabde2020-10-10 17:46:37 -070086 event_loop.OnRun([&log_namer, &logger]() {
Austin Schuh27553152020-11-18 21:26:37 -080087 if (FLAGS_skip_renicing) {
88 LOG(WARNING) << "Ignoring request to renice to -20 due to "
89 "--skip_renicing.";
90 } else {
91 errno = 0;
92 setpriority(PRIO_PROCESS, 0, -20);
93 PCHECK(errno == 0)
94 << ": Renicing to -20 failed, use --skip_renicing to skip renicing.";
95 }
Austin Schuh47aabde2020-10-10 17:46:37 -070096 logger.StartLogging(std::move(log_namer));
97 });
James Kuszmaul38735e82019-12-07 16:42:06 -080098
99 event_loop.Run();
100
Austin Schuh2f8fd752020-09-01 22:38:28 -0700101 LOG(INFO) << "Shutting down";
102
James Kuszmaul38735e82019-12-07 16:42:06 -0800103 return 0;
104}