Austin Schuh | 47aabde | 2020-10-10 17:46:37 -0700 | [diff] [blame] | 1 | #include <sys/resource.h> |
| 2 | #include <sys/time.h> |
| 3 | |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 4 | #include "aos/configuration.h" |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 5 | #include "aos/events/logging/log_writer.h" |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 6 | #ifdef LZMA |
| 7 | #include "aos/events/logging/lzma_encoder.h" |
| 8 | #endif |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 9 | #include "gflags/gflags.h" |
| 10 | #include "glog/logging.h" |
| 11 | |
James Kuszmaul | dd0a504 | 2021-10-28 23:38:04 -0700 | [diff] [blame] | 12 | #include "aos/events/logging/snappy_encoder.h" |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 13 | #include "aos/events/shm_event_loop.h" |
| 14 | #include "aos/init.h" |
Brian Silverman | d90905f | 2020-09-23 14:42:56 -0700 | [diff] [blame] | 15 | #include "aos/logging/log_namer.h" |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 16 | |
colleen | 61276dc | 2023-06-01 09:23:29 -0700 | [diff] [blame] | 17 | DEFINE_bool(direct, false, |
| 18 | "If true, write using O_DIRECT and write 512 byte aligned blocks " |
| 19 | "whenever possible."); |
| 20 | |
Austin Schuh | c5fa6d9 | 2022-02-25 14:36:28 -0800 | [diff] [blame] | 21 | DEFINE_string(config, "aos_config.json", "Config file to use."); |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 22 | |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame] | 23 | DEFINE_bool(skip_renicing, false, |
| 24 | "If true, skip renicing the logger. This leaves it lower priority " |
| 25 | "and increases the likelihood of dropping messages and crashing."); |
| 26 | |
James Kuszmaul | dd0a504 | 2021-10-28 23:38:04 -0700 | [diff] [blame] | 27 | DEFINE_bool(snappy_compress, false, "If true, compress log data using snappy."); |
| 28 | |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 29 | #ifdef LZMA |
| 30 | DEFINE_bool(xz_compress, false, "If true, compress log data using xz."); |
| 31 | #endif |
| 32 | |
Milind Upadhyay | b2b8cd8 | 2022-03-21 08:51:32 -0700 | [diff] [blame] | 33 | DEFINE_double(rotate_every, 0.0, |
| 34 | "If set, rotate the logger after this many seconds"); |
| 35 | |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 36 | #ifdef LZMA |
| 37 | DEFINE_int32(xz_compression_level, 9, "Compression level for the LZMA Encoder"); |
| 38 | #endif |
| 39 | |
Austin Schuh | 8bdfc49 | 2023-02-11 12:53:13 -0800 | [diff] [blame] | 40 | DECLARE_int32(flush_size); |
| 41 | |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 42 | int main(int argc, char *argv[]) { |
| 43 | gflags::SetUsageMessage( |
| 44 | "This program provides a simple logger binary that logs all SHMEM data " |
| 45 | "directly to a file specified at the command line. It does not manage " |
| 46 | "filenames, so it will just crash if you attempt to overwrite an " |
| 47 | "existing file, and the user must specify the logfile manually at the " |
| 48 | "command line."); |
| 49 | aos::InitGoogle(&argc, &argv); |
| 50 | |
| 51 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 52 | aos::configuration::ReadConfig(FLAGS_config); |
| 53 | |
| 54 | aos::ShmEventLoop event_loop(&config.message()); |
| 55 | |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 56 | auto log_namer = std::make_unique<aos::logger::MultiNodeFilesLogNamer>( |
colleen | 61276dc | 2023-06-01 09:23:29 -0700 | [diff] [blame] | 57 | &event_loop, std::make_unique<aos::logger::RenamableFileBackend>( |
| 58 | absl::StrCat(aos::logging::GetLogName("fbs_log"), "/"), |
| 59 | FLAGS_direct)); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 60 | |
James Kuszmaul | dd0a504 | 2021-10-28 23:38:04 -0700 | [diff] [blame] | 61 | if (FLAGS_snappy_compress) { |
| 62 | log_namer->set_extension(aos::logger::SnappyDecoder::kExtension); |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 63 | log_namer->set_encoder_factory([](size_t max_message_size) { |
Austin Schuh | 8bdfc49 | 2023-02-11 12:53:13 -0800 | [diff] [blame] | 64 | return std::make_unique<aos::logger::SnappyEncoder>(max_message_size, |
| 65 | FLAGS_flush_size); |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 66 | }); |
| 67 | #ifdef LZMA |
| 68 | } else if (FLAGS_xz_compress) { |
| 69 | log_namer->set_extension(aos::logger::LzmaEncoder::kExtension); |
| 70 | log_namer->set_encoder_factory([](size_t max_message_size) { |
| 71 | return std::make_unique<aos::logger::LzmaEncoder>( |
Austin Schuh | 8bdfc49 | 2023-02-11 12:53:13 -0800 | [diff] [blame] | 72 | max_message_size, FLAGS_xz_compression_level, FLAGS_flush_size); |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 73 | }); |
| 74 | #endif |
James Kuszmaul | dd0a504 | 2021-10-28 23:38:04 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Austin Schuh | b467463 | 2022-09-19 19:12:13 -0700 | [diff] [blame] | 77 | aos::monotonic_clock::time_point last_rotation_time = |
| 78 | event_loop.monotonic_now(); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 79 | aos::logger::Logger logger(&event_loop); |
Milind Upadhyay | b2b8cd8 | 2022-03-21 08:51:32 -0700 | [diff] [blame] | 80 | |
| 81 | if (FLAGS_rotate_every != 0.0) { |
Austin Schuh | 2f86445 | 2023-07-17 14:53:08 -0700 | [diff] [blame] | 82 | logger.set_on_logged_period([&](aos::monotonic_clock::time_point t) { |
| 83 | if (t > last_rotation_time + |
| 84 | std::chrono::duration<double>(FLAGS_rotate_every)) { |
Milind Upadhyay | b2b8cd8 | 2022-03-21 08:51:32 -0700 | [diff] [blame] | 85 | logger.Rotate(); |
Austin Schuh | 2f86445 | 2023-07-17 14:53:08 -0700 | [diff] [blame] | 86 | last_rotation_time = t; |
Milind Upadhyay | b2b8cd8 | 2022-03-21 08:51:32 -0700 | [diff] [blame] | 87 | } |
| 88 | }); |
| 89 | } |
| 90 | |
Austin Schuh | 47aabde | 2020-10-10 17:46:37 -0700 | [diff] [blame] | 91 | event_loop.OnRun([&log_namer, &logger]() { |
Austin Schuh | 2755315 | 2020-11-18 21:26:37 -0800 | [diff] [blame] | 92 | if (FLAGS_skip_renicing) { |
| 93 | LOG(WARNING) << "Ignoring request to renice to -20 due to " |
| 94 | "--skip_renicing."; |
| 95 | } else { |
| 96 | errno = 0; |
| 97 | setpriority(PRIO_PROCESS, 0, -20); |
| 98 | PCHECK(errno == 0) |
| 99 | << ": Renicing to -20 failed, use --skip_renicing to skip renicing."; |
| 100 | } |
Austin Schuh | 47aabde | 2020-10-10 17:46:37 -0700 | [diff] [blame] | 101 | logger.StartLogging(std::move(log_namer)); |
| 102 | }); |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 103 | |
| 104 | event_loop.Run(); |
| 105 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 106 | LOG(INFO) << "Shutting down"; |
| 107 | |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 108 | return 0; |
| 109 | } |