blob: 9dc9cdb92a86a62e5d0e9c4c3db3dad33ba0c8c9 [file] [log] [blame]
Stephan Pleinesb1177672024-05-27 17:48:32 -07001#include <algorithm>
2#include <memory>
3#include <optional>
4#include <ostream>
5#include <set>
6#include <string>
7#include <vector>
8
Austin Schuh99f7c6a2024-06-25 22:07:44 -07009#include "absl/flags/flag.h"
10#include "absl/log/check.h"
11#include "absl/log/log.h"
Stephan Pleinesb1177672024-05-27 17:48:32 -070012#include "flatbuffers/reflection_generated.h"
Stephan Pleinesb1177672024-05-27 17:48:32 -070013
James Kuszmaul80d6c422023-01-06 14:16:04 -080014#include "aos/configuration.h"
Stephan Pleinesb1177672024-05-27 17:48:32 -070015#include "aos/events/event_loop.h"
James Kuszmaul4ed5fb12022-03-22 15:20:04 -070016#include "aos/events/logging/log_reader.h"
Stephan Pleinesb1177672024-05-27 17:48:32 -070017#include "aos/events/logging/logfile_sorting.h"
18#include "aos/events/simulated_event_loop.h"
19#include "aos/flatbuffers.h"
James Kuszmaul4ed5fb12022-03-22 15:20:04 -070020#include "aos/init.h"
James Kuszmaul80d6c422023-01-06 14:16:04 -080021#include "aos/util/clock_publisher.h"
22#include "aos/util/clock_timepoints_schema.h"
James Kuszmaul4ed5fb12022-03-22 15:20:04 -070023#include "aos/util/mcap_logger.h"
24
Austin Schuh99f7c6a2024-06-25 22:07:44 -070025ABSL_FLAG(std::string, node, "", "Node to replay from the perspective of.");
26ABSL_FLAG(std::string, output_path, "/tmp/log.mcap", "Log to output.");
27ABSL_FLAG(std::string, mode, "flatbuffer", "json or flatbuffer serialization.");
28ABSL_FLAG(
29 bool, canonical_channel_names, false,
James Kuszmaul9f607c62022-10-27 17:01:55 -070030 "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 Schuh99f7c6a2024-06-25 22:07:44 -070032ABSL_FLAG(bool, compress, true, "Whether to use LZ4 compression in MCAP file.");
33ABSL_FLAG(bool, include_clocks, true,
34 "Whether to add a /clocks channel that publishes all nodes' clock "
35 "offsets.");
36ABSL_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 Kuszmaul4ed5fb12022-03-22 15:20:04 -070042
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).
50int 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 Kuszmaulaccf5702022-10-27 16:57:53 -070055 CHECK(!logfiles.empty());
James Kuszmaul20e3db92023-12-09 15:42:09 -080056 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 Schuh99f7c6a2024-06-25 22:07:44 -070059 std::string replay_node = absl::GetFlag(FLAGS_node);
James Kuszmaul20e3db92023-12-09 15:42:09 -080060 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 Kuszmaulaccf5702022-10-27 16:57:53 -070069 }
James Kuszmaul4ed5fb12022-03-22 15:20:04 -070070
James Kuszmaul80d6c422023-01-06 14:16:04 -080071 std::optional<aos::FlatbufferDetachedBuffer<aos::Configuration>> config;
72
Austin Schuh99f7c6a2024-06-25 22:07:44 -070073 if (absl::GetFlag(FLAGS_include_clocks)) {
James Kuszmaul80d6c422023-01-06 14:16:04 -080074 aos::logger::LogReader config_reader(logfiles);
75
James Kuszmaulcb469ad2024-02-16 12:14:04 -080076 if (aos::configuration::MultiNode(config_reader.configuration())) {
77 CHECK(!replay_node.empty()) << ": Must supply a --node.";
78 }
79
James Kuszmaulbed2af02023-01-28 15:57:24 -080080 const aos::Configuration *raw_config = config_reader.logged_configuration();
James Kuszmaul80d6c422023-01-06 14:16:04 -080081 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 Kuszmaul6a5479b2022-12-02 10:01:03 -080091 aos::SimulatedEventLoopFactory factory(reader.configuration());
92 reader.RegisterWithoutStarting(&factory);
James Kuszmaul4ed5fb12022-03-22 15:20:04 -070093
James Kuszmaulcb469ad2024-02-16 12:14:04 -080094 if (aos::configuration::MultiNode(reader.configuration())) {
95 CHECK(!replay_node.empty()) << ": Must supply a --node.";
96 }
97
James Kuszmaul4ed5fb12022-03-22 15:20:04 -070098 const aos::Node *node =
James Kuszmaulcb469ad2024-02-16 12:14:04 -080099 !aos::configuration::MultiNode(reader.configuration())
James Kuszmaul5c56ed32022-03-30 15:10:07 -0700100 ? nullptr
James Kuszmaulaccf5702022-10-27 16:57:53 -0700101 : aos::configuration::GetNode(reader.configuration(), replay_node);
102
James Kuszmaul80d6c422023-01-06 14:16:04 -0800103 std::unique_ptr<aos::EventLoop> clock_event_loop;
104 std::unique_ptr<aos::ClockPublisher> clock_publisher;
James Kuszmaul80d6c422023-01-06 14:16:04 -0800105
James Kuszmaulbed2af02023-01-28 15:57:24 -0800106 std::unique_ptr<aos::EventLoop> mcap_event_loop;
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700107 CHECK(!absl::GetFlag(FLAGS_output_path).empty());
James Kuszmaulbed2af02023-01-28 15:57:24 -0800108 std::unique_ptr<aos::McapLogger> relogger;
James Kuszmaulbe117582023-07-08 20:28:15 -0700109 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 Schuh1ec25482023-05-03 13:07:01 -0700114 mcap_event_loop = reader.event_loop_factory()->MakeEventLoop("mcap", node);
115 relogger = std::make_unique<aos::McapLogger>(
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700116 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 Schuh1ec25482023-05-03 13:07:01 -0700121 ? aos::McapLogger::CanonicalChannelNames::kCanonical
122 : aos::McapLogger::CanonicalChannelNames::kShortened,
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700123 absl::GetFlag(FLAGS_compress) ? aos::McapLogger::Compression::kLz4
124 : aos::McapLogger::Compression::kNone);
125 if (absl::GetFlag(FLAGS_include_clocks)) {
James Kuszmaulbe117582023-07-08 20:28:15 -0700126 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 Schuh99f7c6a2024-06-25 22:07:44 -0700132 if (absl::GetFlag(FLAGS_include_pre_start_messages)) {
James Kuszmaulbe117582023-07-08 20:28:15 -0700133 // 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 Kuszmaul4ed5fb12022-03-22 15:20:04 -0700141 reader.event_loop_factory()->Run();
James Kuszmaul6a5479b2022-12-02 10:01:03 -0800142 reader.Deregister();
James Kuszmaul4ed5fb12022-03-22 15:20:04 -0700143}