James Kuszmaul | ecafe1f | 2024-02-27 20:29:53 -0800 | [diff] [blame^] | 1 | #include <sys/resource.h> |
| 2 | #include <sys/time.h> |
| 3 | |
| 4 | #include "gflags/gflags.h" |
| 5 | #include "glog/logging.h" |
| 6 | |
| 7 | #include "aos/configuration.h" |
| 8 | #include "aos/events/logging/log_writer.h" |
| 9 | #include "aos/events/logging/snappy_encoder.h" |
| 10 | #include "aos/events/shm_event_loop.h" |
| 11 | #include "aos/init.h" |
| 12 | #include "aos/logging/log_namer.h" |
| 13 | |
| 14 | DEFINE_string(config, "aos_config.json", "Config file to use."); |
| 15 | |
| 16 | DEFINE_double(rotate_every, 30.0, |
| 17 | "If set, rotate the logger after this many seconds"); |
| 18 | |
| 19 | DECLARE_int32(flush_size); |
| 20 | |
| 21 | int main(int argc, char *argv[]) { |
| 22 | gflags::SetUsageMessage( |
| 23 | "This program provides a simple logger binary that logs all SHMEM data " |
| 24 | "directly to a file specified at the command line. It does not manage " |
| 25 | "filenames, so it will just crash if you attempt to overwrite an " |
| 26 | "existing file, and the user must specify the logfile manually at the " |
| 27 | "command line."); |
| 28 | aos::InitGoogle(&argc, &argv); |
| 29 | |
| 30 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 31 | aos::configuration::ReadConfig(FLAGS_config); |
| 32 | |
| 33 | aos::ShmEventLoop event_loop(&config.message()); |
| 34 | |
| 35 | auto log_namer = std::make_unique<aos::logger::MultiNodeFilesLogNamer>( |
| 36 | &event_loop, |
| 37 | std::make_unique<aos::logger::RenamableFileBackend>( |
| 38 | absl::StrCat(aos::logging::GetLogName("localizer_log"), "/"), |
| 39 | /*O_DIRECT*/ true)); |
| 40 | |
| 41 | log_namer->set_extension(aos::logger::SnappyDecoder::kExtension); |
| 42 | log_namer->set_encoder_factory([](size_t max_message_size) { |
| 43 | return std::make_unique<aos::logger::SnappyEncoder>(max_message_size, |
| 44 | FLAGS_flush_size); |
| 45 | }); |
| 46 | |
| 47 | aos::monotonic_clock::time_point last_rotation_time = |
| 48 | event_loop.monotonic_now(); |
| 49 | aos::logger::Logger logger( |
| 50 | &event_loop, event_loop.configuration(), |
| 51 | // Only log channels smaller than ~10 MB / sec. |
| 52 | [](const aos::Channel *channel) { |
| 53 | return (channel->max_size() * channel->frequency()) < 10e6; |
| 54 | }); |
| 55 | |
| 56 | if (FLAGS_rotate_every != 0.0) { |
| 57 | logger.set_on_logged_period( |
| 58 | [&logger, &last_rotation_time](aos::monotonic_clock::time_point t) { |
| 59 | if (t > last_rotation_time + |
| 60 | std::chrono::duration<double>(FLAGS_rotate_every)) { |
| 61 | logger.Rotate(); |
| 62 | last_rotation_time = t; |
| 63 | } |
| 64 | }); |
| 65 | } |
| 66 | |
| 67 | event_loop.OnRun([&log_namer, &logger]() { |
| 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 | logger.StartLogging(std::move(log_namer)); |
| 73 | }); |
| 74 | |
| 75 | event_loop.Run(); |
| 76 | |
| 77 | LOG(INFO) << "Shutting down"; |
| 78 | |
| 79 | return 0; |
| 80 | } |