Change the start time to prefer local logs

We have some logs where the remotely logged channels start before the
local log.  For those, the RT time is min_time.

Thinking back about it, that remote start isn't all that meaningful for
a node if we have a local log.  It typically has a very minimal set of
data that isn't worth parsing.  So we should prioritize local log file
starts when determining the start time.

Change-Id: I1f9795217212d6e9c5d6d74e0abd21973b46db8e
Signed-off-by: Austin Schuh <austin.schuh@bluerivertech.com>
diff --git a/aos/events/logging/logfile_utils.cc b/aos/events/logging/logfile_utils.cc
index 9b66caf..73578e6 100644
--- a/aos/events/logging/logfile_utils.cc
+++ b/aos/events/logging/logfile_utils.cc
@@ -804,15 +804,39 @@
   }
 
   monotonic_start_time_ = monotonic_clock::max_time;
-  realtime_start_time_ = realtime_clock::max_time;
+  realtime_start_time_ = realtime_clock::min_time;
   for (const LogPartsSorter &parts_sorter : parts_sorters_) {
     // We want to capture the earliest meaningful start time here. The start
     // time defaults to min_time when there's no meaningful value to report, so
     // let's ignore those.
-    if (parts_sorter.monotonic_start_time() != monotonic_clock::min_time &&
-        parts_sorter.monotonic_start_time() < monotonic_start_time_) {
-      monotonic_start_time_ = parts_sorter.monotonic_start_time();
-      realtime_start_time_ = parts_sorter.realtime_start_time();
+    if (parts_sorter.monotonic_start_time() != monotonic_clock::min_time) {
+      bool accept = false;
+      // We want to prioritize start times from the logger node.  Really, we
+      // want to prioritize start times with a valid realtime_clock time.  So,
+      // if we have a start time without a RT clock, prefer a start time with a
+      // RT clock, even it if is later.
+      if (parts_sorter.realtime_start_time() != realtime_clock::min_time) {
+        // We've got a good one.  See if the current start time has a good RT
+        // clock, or if we should use this one instead.
+        if (parts_sorter.monotonic_start_time() < monotonic_start_time_) {
+          accept = true;
+        } else if (realtime_start_time_ == realtime_clock::min_time) {
+          // The previous start time doesn't have a good RT time, so it is very
+          // likely the start time from a remote part file.  We just found a
+          // better start time with a real RT time, so switch to that instead.
+          accept = true;
+        }
+      } else if (realtime_start_time_ == realtime_clock::min_time) {
+        // We don't have a RT time, so take the oldest.
+        if (parts_sorter.monotonic_start_time() < monotonic_start_time_) {
+          accept = true;
+        }
+      }
+
+      if (accept) {
+        monotonic_start_time_ = parts_sorter.monotonic_start_time();
+        realtime_start_time_ = parts_sorter.realtime_start_time();
+      }
     }
   }