James Kuszmaul | ecafe1f | 2024-02-27 20:29:53 -0800 | [diff] [blame] | 1 | #include <sys/resource.h> |
| 2 | #include <sys/time.h> |
| 3 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 4 | #include "absl/flags/flag.h" |
| 5 | #include "absl/flags/usage.h" |
| 6 | #include "absl/log/check.h" |
| 7 | #include "absl/log/log.h" |
James Kuszmaul | ecafe1f | 2024-02-27 20:29:53 -0800 | [diff] [blame] | 8 | |
| 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 Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 16 | ABSL_FLAG(std::string, config, "aos_config.json", "Config file to use."); |
James Kuszmaul | ecafe1f | 2024-02-27 20:29:53 -0800 | [diff] [blame] | 17 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 18 | ABSL_FLAG(double, rotate_every, 30.0, |
| 19 | "If set, rotate the logger after this many seconds"); |
James Kuszmaul | ecafe1f | 2024-02-27 20:29:53 -0800 | [diff] [blame] | 20 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 21 | ABSL_DECLARE_FLAG(int32_t, flush_size); |
James Kuszmaul | ecafe1f | 2024-02-27 20:29:53 -0800 | [diff] [blame] | 22 | |
| 23 | int main(int argc, char *argv[]) { |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 24 | absl::SetProgramUsageMessage( |
James Kuszmaul | ecafe1f | 2024-02-27 20:29:53 -0800 | [diff] [blame] | 25 | "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 Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 33 | aos::configuration::ReadConfig(absl::GetFlag(FLAGS_config)); |
James Kuszmaul | ecafe1f | 2024-02-27 20:29:53 -0800 | [diff] [blame] | 34 | |
| 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 Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 45 | return std::make_unique<aos::logger::SnappyEncoder>( |
| 46 | max_message_size, absl::GetFlag(FLAGS_flush_size)); |
James Kuszmaul | ecafe1f | 2024-02-27 20:29:53 -0800 | [diff] [blame] | 47 | }); |
| 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 Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 58 | if (absl::GetFlag(FLAGS_rotate_every) != 0.0) { |
James Kuszmaul | ecafe1f | 2024-02-27 20:29:53 -0800 | [diff] [blame] | 59 | logger.set_on_logged_period( |
| 60 | [&logger, &last_rotation_time](aos::monotonic_clock::time_point t) { |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 61 | if (t > last_rotation_time + std::chrono::duration<double>( |
| 62 | absl::GetFlag(FLAGS_rotate_every))) { |
James Kuszmaul | ecafe1f | 2024-02-27 20:29:53 -0800 | [diff] [blame] | 63 | 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 | } |