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