Add statistics for how far behind the logger falls

This patch adds a new couple of fields to the logger status message to
show how far behind we are in logging.

I added a new unit test to validate that the fields are set.

Change-Id: I41675096056992bcafa3db24d094c4f78379be86
Signed-off-by: James Kuszmaul <james.kuszmaul@bluerivertech.com>
diff --git a/aos/events/logging/log_writer.cc b/aos/events/logging/log_writer.cc
index 69b0542..e8b8819 100644
--- a/aos/events/logging/log_writer.cc
+++ b/aos/events/logging/log_writer.cc
@@ -684,6 +684,8 @@
   total_copy_time_ = std::chrono::nanoseconds::zero();
   total_copy_count_ = 0;
   total_copy_bytes_ = 0;
+  max_log_delay_ = std::chrono::nanoseconds::zero();
+  max_log_delay_channel_ = -1;
 }
 
 void Logger::Rotate() {
@@ -912,6 +914,11 @@
     max_copy_time_channel_ = fetcher.channel_index;
     max_copy_time_size_ = fetcher.fetcher->context().size;
   }
+  const auto log_delay = end - fetcher.fetcher->context().monotonic_event_time;
+  if (log_delay > max_log_delay_) {
+    max_log_delay_ = log_delay;
+    max_log_delay_channel_ = fetcher.channel_index;
+  }
 }
 
 }  // namespace logger
diff --git a/aos/events/logging/log_writer.h b/aos/events/logging/log_writer.h
index 265513f..ec02b7d 100644
--- a/aos/events/logging/log_writer.h
+++ b/aos/events/logging/log_writer.h
@@ -77,6 +77,8 @@
   void set_logging_delay(std::chrono::nanoseconds logging_delay) {
     logging_delay_ = logging_delay;
   }
+  // Returns the current logging delay.
+  std::chrono::nanoseconds logging_delay() const { return logging_delay_; }
 
   // Sets the period between polling the data. Defaults to 100ms.
   //
@@ -137,6 +139,13 @@
   // The total number of bytes copied.
   int64_t total_copy_bytes() const { return total_copy_bytes_; }
 
+  // The maximum time between when a message was sent and when it was logged.
+  // This is 0 if no message has been logged.
+  std::chrono::nanoseconds max_log_delay() const { return max_log_delay_; }
+  // The channel for longest logging delay, or -1 if no messages have been
+  // logged.
+  int max_log_delay_channel() const { return max_log_delay_channel_; }
+
   void ResetStatisics();
 
   // Rotates the log file(s), triggering new part files to be written for each
@@ -328,6 +337,9 @@
   int total_message_fetch_count_ = 0;
   int64_t total_message_fetch_bytes_ = 0;
 
+  std::chrono::nanoseconds max_log_delay_ = std::chrono::nanoseconds::zero();
+  int max_log_delay_channel_ = -1;
+
   std::chrono::nanoseconds total_nop_fetch_time_ =
       std::chrono::nanoseconds::zero();
   int total_nop_fetch_count_ = 0;