blob: 0d51e527afedc78657e1eded6de7235438075d8a [file] [log] [blame]
James Kuszmaul80d6c422023-01-06 14:16:04 -08001#include "aos/configuration.h"
James Kuszmaul4ed5fb12022-03-22 15:20:04 -07002#include "aos/events/event_loop_generated.h"
3#include "aos/events/logging/log_reader.h"
4#include "aos/init.h"
James Kuszmaul80d6c422023-01-06 14:16:04 -08005#include "aos/util/clock_publisher.h"
6#include "aos/util/clock_timepoints_schema.h"
James Kuszmaul4ed5fb12022-03-22 15:20:04 -07007#include "aos/util/mcap_logger.h"
8
9DEFINE_string(node, "", "Node to replay from the perspective of.");
10DEFINE_string(output_path, "/tmp/log.mcap", "Log to output.");
James Kuszmaulaccf5702022-10-27 16:57:53 -070011DEFINE_string(mode, "flatbuffer", "json or flatbuffer serialization.");
James Kuszmaul9f607c62022-10-27 17:01:55 -070012DEFINE_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 Kuszmaul5ab990d2022-11-07 16:35:49 -080016DEFINE_bool(compress, true, "Whether to use LZ4 compression in MCAP file.");
James Kuszmaul80d6c422023-01-06 14:16:04 -080017DEFINE_bool(include_clocks, true,
18 "Whether to add a /clocks channel that publishes all nodes' clock "
19 "offsets.");
James Kuszmaulbe117582023-07-08 20:28:15 -070020DEFINE_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 Kuszmaul4ed5fb12022-03-22 15:20:04 -070026
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).
34int 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 Kuszmaulaccf5702022-10-27 16:57:53 -070039 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 Kuszmaul4ed5fb12022-03-22 15:20:04 -070054
James Kuszmaul80d6c422023-01-06 14:16:04 -080055 std::optional<aos::FlatbufferDetachedBuffer<aos::Configuration>> config;
56
57 if (FLAGS_include_clocks) {
58 aos::logger::LogReader config_reader(logfiles);
59
James Kuszmaulbed2af02023-01-28 15:57:24 -080060 const aos::Configuration *raw_config = config_reader.logged_configuration();
James Kuszmaul80d6c422023-01-06 14:16:04 -080061 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 Kuszmaul6a5479b2022-12-02 10:01:03 -080071 aos::SimulatedEventLoopFactory factory(reader.configuration());
72 reader.RegisterWithoutStarting(&factory);
James Kuszmaul4ed5fb12022-03-22 15:20:04 -070073
74 const aos::Node *node =
James Kuszmaulaccf5702022-10-27 16:57:53 -070075 (replay_node.empty() ||
76 !aos::configuration::MultiNode(reader.configuration()))
James Kuszmaul5c56ed32022-03-30 15:10:07 -070077 ? nullptr
James Kuszmaulaccf5702022-10-27 16:57:53 -070078 : aos::configuration::GetNode(reader.configuration(), replay_node);
79
James Kuszmaul80d6c422023-01-06 14:16:04 -080080 std::unique_ptr<aos::EventLoop> clock_event_loop;
81 std::unique_ptr<aos::ClockPublisher> clock_publisher;
James Kuszmaul80d6c422023-01-06 14:16:04 -080082
James Kuszmaulbed2af02023-01-28 15:57:24 -080083 std::unique_ptr<aos::EventLoop> mcap_event_loop;
James Kuszmaul4ed5fb12022-03-22 15:20:04 -070084 CHECK(!FLAGS_output_path.empty());
James Kuszmaulbed2af02023-01-28 15:57:24 -080085 std::unique_ptr<aos::McapLogger> relogger;
James Kuszmaulbe117582023-07-08 20:28:15 -070086 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 Schuh1ec25482023-05-03 13:07:01 -070091 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 Kuszmaulbe117582023-07-08 20:28:15 -0700101 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 Kuszmaul4ed5fb12022-03-22 15:20:04 -0700117 reader.event_loop_factory()->Run();
James Kuszmaul6a5479b2022-12-02 10:01:03 -0800118 reader.Deregister();
James Kuszmaul4ed5fb12022-03-22 15:20:04 -0700119}