blob: 9a0af48a8a878cb2c2f7102d01b08dbd25834ae3 [file] [log] [blame]
James Kuszmaul4ed5fb12022-03-22 15:20:04 -07001#include "aos/events/event_loop_generated.h"
2#include "aos/events/logging/log_reader.h"
3#include "aos/init.h"
4#include "aos/util/mcap_logger.h"
5
6DEFINE_string(node, "", "Node to replay from the perspective of.");
7DEFINE_string(output_path, "/tmp/log.mcap", "Log to output.");
James Kuszmaulaccf5702022-10-27 16:57:53 -07008DEFINE_string(mode, "flatbuffer", "json or flatbuffer serialization.");
James Kuszmaul9f607c62022-10-27 17:01:55 -07009DEFINE_bool(
10 canonical_channel_names, false,
11 "If set, use full channel names; by default, will shorten names to be the "
12 "shortest possible version of the name (e.g., /aos instead of /pi/aos).");
James Kuszmaul4ed5fb12022-03-22 15:20:04 -070013
14// Converts an AOS log to an MCAP log that can be fed into Foxglove. To try this
15// out, run:
16// bazel run -c opt //aos/util:log_to_mcap -- --node NODE_NAME /path/to/logfile
17//
18// Then navigate to http://studio.foxglove.dev (or spin up your own instance
19// locally), and use it to open the file (this doesn't upload the file to
20// foxglove's servers or anything).
21int main(int argc, char *argv[]) {
22 aos::InitGoogle(&argc, &argv);
23
24 const std::vector<aos::logger::LogFile> logfiles =
25 aos::logger::SortParts(aos::logger::FindLogs(argc, argv));
James Kuszmaulaccf5702022-10-27 16:57:53 -070026 CHECK(!logfiles.empty());
27 const std::string logger_node = logfiles.at(0).logger_node;
28 bool all_logs_from_same_node = true;
29 for (const aos::logger::LogFile &log : logfiles) {
30 if (log.logger_node != logger_node) {
31 all_logs_from_same_node = false;
32 break;
33 }
34 }
35 std::string replay_node = FLAGS_node;
36 if (replay_node.empty() && all_logs_from_same_node) {
37 LOG(INFO) << "Guessing \"" << logger_node
38 << "\" as node given that --node was not specified.";
39 replay_node = logger_node;
40 }
James Kuszmaul4ed5fb12022-03-22 15:20:04 -070041
42 aos::logger::LogReader reader(logfiles);
43
44 reader.Register();
45
46 const aos::Node *node =
James Kuszmaulaccf5702022-10-27 16:57:53 -070047 (replay_node.empty() ||
48 !aos::configuration::MultiNode(reader.configuration()))
James Kuszmaul5c56ed32022-03-30 15:10:07 -070049 ? nullptr
James Kuszmaulaccf5702022-10-27 16:57:53 -070050 : aos::configuration::GetNode(reader.configuration(), replay_node);
51
James Kuszmaul4ed5fb12022-03-22 15:20:04 -070052 std::unique_ptr<aos::EventLoop> mcap_event_loop =
53 reader.event_loop_factory()->MakeEventLoop("mcap", node);
54 CHECK(!FLAGS_output_path.empty());
James Kuszmaul9f607c62022-10-27 17:01:55 -070055 aos::McapLogger relogger(
56 mcap_event_loop.get(), FLAGS_output_path,
57 FLAGS_mode == "flatbuffer" ? aos::McapLogger::Serialization::kFlatbuffer
58 : aos::McapLogger::Serialization::kJson,
59 FLAGS_canonical_channel_names
60 ? aos::McapLogger::CanonicalChannelNames::kCanonical
61 : aos::McapLogger::CanonicalChannelNames::kShortened);
James Kuszmaul4ed5fb12022-03-22 15:20:04 -070062 reader.event_loop_factory()->Run();
63}