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