Buffer any timestamps before the start of a log

When you need to set the buffer time to be high in order to sort a log,
all the data at the start of a log gets thrown out.  Instead, buffer it
all up and write when we know the start time.

Change-Id: If1c83ecee87236d4c93c52a58082fc31b6097247
diff --git a/aos/network/timestamp_filter.cc b/aos/network/timestamp_filter.cc
index 5e18185..df42047 100644
--- a/aos/network/timestamp_filter.cc
+++ b/aos/network/timestamp_filter.cc
@@ -535,16 +535,27 @@
     return Line::Fit(TrimTuple(timestamps_[0]), TrimTuple(timestamps_[1]));
   }
 }
+void NoncausalTimestampFilter::FlushSavedSamples() {
+  for (const std::tuple<aos::monotonic_clock::time_point,
+                        std::chrono::nanoseconds> &sample : saved_samples_) {
+    fprintf(samples_fp_, "%.9f, %.9f\n",
+            chrono::duration_cast<chrono::duration<double>>(
+                std::get<0>(sample) - first_time_)
+                .count(),
+            chrono::duration_cast<chrono::duration<double>>(std::get<1>(sample))
+                .count());
+  }
+  saved_samples_.clear();
+}
 
 bool NoncausalTimestampFilter::Sample(
     aos::monotonic_clock::time_point monotonic_now,
     chrono::nanoseconds sample_ns) {
-  if (samples_fp_ && first_time_ != aos::monotonic_clock::min_time) {
-    fprintf(samples_fp_, "%.9f, %.9f\n",
-            chrono::duration_cast<chrono::duration<double>>(monotonic_now -
-                                                            first_time_)
-                .count(),
-            chrono::duration_cast<chrono::duration<double>>(sample_ns).count());
+  if (samples_fp_) {
+    saved_samples_.emplace_back(std::make_pair(monotonic_now, sample_ns));
+    if (first_time_ != aos::monotonic_clock::min_time) {
+      FlushSavedSamples();
+    }
   }
 
   // The first sample is easy.  Just do it!
@@ -644,6 +655,7 @@
   if (samples_fp_) {
     samples_fp_ = freopen(NULL, "wb", samples_fp_);
     PrintNoncausalTimestampFilterSamplesHeader(samples_fp_);
+    FlushSavedSamples();
   }
 }
 
diff --git a/aos/network/timestamp_filter.h b/aos/network/timestamp_filter.h
index 82cc9b8..bcb6bce 100644
--- a/aos/network/timestamp_filter.h
+++ b/aos/network/timestamp_filter.h
@@ -371,6 +371,9 @@
                                       std::chrono::nanoseconds, bool>
                                timestamp);
 
+  // Writes any saved timestamps to file.
+  void FlushSavedSamples();
+
   // Timestamp, offest, and then a boolean representing if this sample is frozen
   // and can't be modified or not.
   // TODO(austin): Actually use and update the bool.
@@ -378,6 +381,12 @@
                         std::chrono::nanoseconds, bool>>
       timestamps_;
 
+  // Holds any timestamps from before the start of the log to be flushed when we
+  // know when the log starts.
+  std::vector<
+      std::tuple<aos::monotonic_clock::time_point, std::chrono::nanoseconds>>
+      saved_samples_;
+
   std::string csv_file_name_;
   FILE *fp_ = nullptr;
   FILE *samples_fp_ = nullptr;