Autodetect replay node in log_to_mcap

This makes it so that if you fail to specify a --node, we will attempt
to autodetect the node based on what node logged the logfiles. This
makes it work much better in most use-cases.

Change-Id: I52ec5c8ba41532c0f5dd33ddf7116ca9377dc486
Signed-off-by: James Kuszmaul <james.kuszmaul@bluerivertech.com>
diff --git a/aos/util/log_to_mcap.cc b/aos/util/log_to_mcap.cc
index 5330c60..2e66e67 100644
--- a/aos/util/log_to_mcap.cc
+++ b/aos/util/log_to_mcap.cc
@@ -5,7 +5,7 @@
 
 DEFINE_string(node, "", "Node to replay from the perspective of.");
 DEFINE_string(output_path, "/tmp/log.mcap", "Log to output.");
-DEFINE_string(mode, "json", "json or flatbuffer serialization.");
+DEFINE_string(mode, "flatbuffer", "json or flatbuffer serialization.");
 
 // Converts an AOS log to an MCAP log that can be fed into Foxglove. To try this
 // out, run:
@@ -19,15 +19,32 @@
 
   const std::vector<aos::logger::LogFile> logfiles =
       aos::logger::SortParts(aos::logger::FindLogs(argc, argv));
+  CHECK(!logfiles.empty());
+  const std::string logger_node = logfiles.at(0).logger_node;
+  bool all_logs_from_same_node = true;
+  for (const aos::logger::LogFile &log : logfiles) {
+    if (log.logger_node != logger_node) {
+      all_logs_from_same_node = false;
+      break;
+    }
+  }
+  std::string replay_node = FLAGS_node;
+  if (replay_node.empty() && all_logs_from_same_node) {
+    LOG(INFO) << "Guessing \"" << logger_node
+              << "\" as node given that --node was not specified.";
+    replay_node = logger_node;
+  }
 
   aos::logger::LogReader reader(logfiles);
 
   reader.Register();
 
   const aos::Node *node =
-      FLAGS_node.empty()
+      (replay_node.empty() ||
+       !aos::configuration::MultiNode(reader.configuration()))
           ? nullptr
-          : aos::configuration::GetNode(reader.configuration(), FLAGS_node);
+          : aos::configuration::GetNode(reader.configuration(), replay_node);
+
   std::unique_ptr<aos::EventLoop> mcap_event_loop =
       reader.event_loop_factory()->MakeEventLoop("mcap", node);
   CHECK(!FLAGS_output_path.empty());