James Kuszmaul | 80d6c42 | 2023-01-06 14:16:04 -0800 | [diff] [blame] | 1 | #include "aos/configuration.h" |
James Kuszmaul | 4ed5fb1 | 2022-03-22 15:20:04 -0700 | [diff] [blame] | 2 | #include "aos/events/event_loop_generated.h" |
| 3 | #include "aos/events/logging/log_reader.h" |
| 4 | #include "aos/init.h" |
James Kuszmaul | 80d6c42 | 2023-01-06 14:16:04 -0800 | [diff] [blame] | 5 | #include "aos/util/clock_publisher.h" |
| 6 | #include "aos/util/clock_timepoints_schema.h" |
James Kuszmaul | 4ed5fb1 | 2022-03-22 15:20:04 -0700 | [diff] [blame] | 7 | #include "aos/util/mcap_logger.h" |
| 8 | |
| 9 | DEFINE_string(node, "", "Node to replay from the perspective of."); |
| 10 | DEFINE_string(output_path, "/tmp/log.mcap", "Log to output."); |
James Kuszmaul | accf570 | 2022-10-27 16:57:53 -0700 | [diff] [blame] | 11 | DEFINE_string(mode, "flatbuffer", "json or flatbuffer serialization."); |
James Kuszmaul | 9f607c6 | 2022-10-27 17:01:55 -0700 | [diff] [blame] | 12 | DEFINE_bool( |
| 13 | canonical_channel_names, false, |
| 14 | "If set, use full channel names; by default, will shorten names to be the " |
| 15 | "shortest possible version of the name (e.g., /aos instead of /pi/aos)."); |
James Kuszmaul | 5ab990d | 2022-11-07 16:35:49 -0800 | [diff] [blame] | 16 | DEFINE_bool(compress, true, "Whether to use LZ4 compression in MCAP file."); |
James Kuszmaul | 80d6c42 | 2023-01-06 14:16:04 -0800 | [diff] [blame] | 17 | DEFINE_bool(include_clocks, true, |
| 18 | "Whether to add a /clocks channel that publishes all nodes' clock " |
| 19 | "offsets."); |
James Kuszmaul | be11758 | 2023-07-08 20:28:15 -0700 | [diff] [blame] | 20 | DEFINE_bool(include_pre_start_messages, false, |
| 21 | "If set, *all* messages in the logfile will be included, including " |
| 22 | "any that may have occurred prior to the start of the log. This " |
| 23 | "can be used to see additional data, but given that data may be " |
| 24 | "incomplete prior to the start of the log, you should be careful " |
| 25 | "about interpretting data flow when using this flag."); |
James Kuszmaul | 4ed5fb1 | 2022-03-22 15:20:04 -0700 | [diff] [blame] | 26 | |
| 27 | // Converts an AOS log to an MCAP log that can be fed into Foxglove. To try this |
| 28 | // out, run: |
| 29 | // bazel run -c opt //aos/util:log_to_mcap -- --node NODE_NAME /path/to/logfile |
| 30 | // |
| 31 | // Then navigate to http://studio.foxglove.dev (or spin up your own instance |
| 32 | // locally), and use it to open the file (this doesn't upload the file to |
| 33 | // foxglove's servers or anything). |
| 34 | int main(int argc, char *argv[]) { |
| 35 | aos::InitGoogle(&argc, &argv); |
| 36 | |
| 37 | const std::vector<aos::logger::LogFile> logfiles = |
| 38 | aos::logger::SortParts(aos::logger::FindLogs(argc, argv)); |
James Kuszmaul | accf570 | 2022-10-27 16:57:53 -0700 | [diff] [blame] | 39 | CHECK(!logfiles.empty()); |
| 40 | const std::string logger_node = logfiles.at(0).logger_node; |
| 41 | bool all_logs_from_same_node = true; |
| 42 | for (const aos::logger::LogFile &log : logfiles) { |
| 43 | if (log.logger_node != logger_node) { |
| 44 | all_logs_from_same_node = false; |
| 45 | break; |
| 46 | } |
| 47 | } |
| 48 | std::string replay_node = FLAGS_node; |
| 49 | if (replay_node.empty() && all_logs_from_same_node) { |
| 50 | LOG(INFO) << "Guessing \"" << logger_node |
| 51 | << "\" as node given that --node was not specified."; |
| 52 | replay_node = logger_node; |
| 53 | } |
James Kuszmaul | 4ed5fb1 | 2022-03-22 15:20:04 -0700 | [diff] [blame] | 54 | |
James Kuszmaul | 80d6c42 | 2023-01-06 14:16:04 -0800 | [diff] [blame] | 55 | std::optional<aos::FlatbufferDetachedBuffer<aos::Configuration>> config; |
| 56 | |
| 57 | if (FLAGS_include_clocks) { |
| 58 | aos::logger::LogReader config_reader(logfiles); |
| 59 | |
James Kuszmaul | bed2af0 | 2023-01-28 15:57:24 -0800 | [diff] [blame] | 60 | const aos::Configuration *raw_config = config_reader.logged_configuration(); |
James Kuszmaul | 80d6c42 | 2023-01-06 14:16:04 -0800 | [diff] [blame] | 61 | config = aos::configuration::AddChannelToConfiguration( |
| 62 | raw_config, "/clocks", |
| 63 | aos::FlatbufferSpan<reflection::Schema>(aos::ClockTimepointsSchema()), |
| 64 | replay_node.empty() |
| 65 | ? nullptr |
| 66 | : aos::configuration::GetNode(raw_config, replay_node)); |
| 67 | } |
| 68 | |
| 69 | aos::logger::LogReader reader( |
| 70 | logfiles, config.has_value() ? &config.value().message() : nullptr); |
James Kuszmaul | 6a5479b | 2022-12-02 10:01:03 -0800 | [diff] [blame] | 71 | aos::SimulatedEventLoopFactory factory(reader.configuration()); |
| 72 | reader.RegisterWithoutStarting(&factory); |
James Kuszmaul | 4ed5fb1 | 2022-03-22 15:20:04 -0700 | [diff] [blame] | 73 | |
| 74 | const aos::Node *node = |
James Kuszmaul | accf570 | 2022-10-27 16:57:53 -0700 | [diff] [blame] | 75 | (replay_node.empty() || |
| 76 | !aos::configuration::MultiNode(reader.configuration())) |
James Kuszmaul | 5c56ed3 | 2022-03-30 15:10:07 -0700 | [diff] [blame] | 77 | ? nullptr |
James Kuszmaul | accf570 | 2022-10-27 16:57:53 -0700 | [diff] [blame] | 78 | : aos::configuration::GetNode(reader.configuration(), replay_node); |
| 79 | |
James Kuszmaul | 80d6c42 | 2023-01-06 14:16:04 -0800 | [diff] [blame] | 80 | std::unique_ptr<aos::EventLoop> clock_event_loop; |
| 81 | std::unique_ptr<aos::ClockPublisher> clock_publisher; |
James Kuszmaul | 80d6c42 | 2023-01-06 14:16:04 -0800 | [diff] [blame] | 82 | |
James Kuszmaul | bed2af0 | 2023-01-28 15:57:24 -0800 | [diff] [blame] | 83 | std::unique_ptr<aos::EventLoop> mcap_event_loop; |
James Kuszmaul | 4ed5fb1 | 2022-03-22 15:20:04 -0700 | [diff] [blame] | 84 | CHECK(!FLAGS_output_path.empty()); |
James Kuszmaul | bed2af0 | 2023-01-28 15:57:24 -0800 | [diff] [blame] | 85 | std::unique_ptr<aos::McapLogger> relogger; |
James Kuszmaul | be11758 | 2023-07-08 20:28:15 -0700 | [diff] [blame] | 86 | auto startup_handler = [&relogger, &mcap_event_loop, &reader, |
| 87 | &clock_event_loop, &clock_publisher, &factory, |
| 88 | node]() { |
| 89 | CHECK(!mcap_event_loop) << ": log_to_mcap does not support generating MCAP " |
| 90 | "files from multi-boot logs."; |
Austin Schuh | 1ec2548 | 2023-05-03 13:07:01 -0700 | [diff] [blame] | 91 | mcap_event_loop = reader.event_loop_factory()->MakeEventLoop("mcap", node); |
| 92 | relogger = std::make_unique<aos::McapLogger>( |
| 93 | mcap_event_loop.get(), FLAGS_output_path, |
| 94 | FLAGS_mode == "flatbuffer" ? aos::McapLogger::Serialization::kFlatbuffer |
| 95 | : aos::McapLogger::Serialization::kJson, |
| 96 | FLAGS_canonical_channel_names |
| 97 | ? aos::McapLogger::CanonicalChannelNames::kCanonical |
| 98 | : aos::McapLogger::CanonicalChannelNames::kShortened, |
| 99 | FLAGS_compress ? aos::McapLogger::Compression::kLz4 |
| 100 | : aos::McapLogger::Compression::kNone); |
James Kuszmaul | be11758 | 2023-07-08 20:28:15 -0700 | [diff] [blame] | 101 | if (FLAGS_include_clocks) { |
| 102 | clock_event_loop = |
| 103 | reader.event_loop_factory()->MakeEventLoop("clock", node); |
| 104 | clock_publisher = std::make_unique<aos::ClockPublisher>( |
| 105 | &factory, clock_event_loop.get()); |
| 106 | } |
| 107 | }; |
| 108 | if (FLAGS_include_pre_start_messages) { |
| 109 | // Note: This condition is subtly different from just using --fetch from |
| 110 | // mcap_logger.cc. Namely, if there is >1 message on a given channel prior |
| 111 | // to the logfile start, then fetching in the reader OnStart() is |
| 112 | // insufficient to get *all* log data. |
| 113 | factory.GetNodeEventLoopFactory(node)->OnStartup(startup_handler); |
| 114 | } else { |
| 115 | reader.OnStart(node, startup_handler); |
| 116 | } |
James Kuszmaul | 4ed5fb1 | 2022-03-22 15:20:04 -0700 | [diff] [blame] | 117 | reader.event_loop_factory()->Run(); |
James Kuszmaul | 6a5479b | 2022-12-02 10:01:03 -0800 | [diff] [blame] | 118 | reader.Deregister(); |
James Kuszmaul | 4ed5fb1 | 2022-03-22 15:20:04 -0700 | [diff] [blame] | 119 | } |