Make SimulatedEventLoop log the correct time

Mock time out for LOG statements so the logged time is the simulated
time.  This makes it so all the plotting tools will work, and makes it
so it's easier to debug simulations.

Change-Id: I902b6c52dc6008e5812cde9ec6cacdf72f13206e
diff --git a/aos/testing/test_logging.cc b/aos/testing/test_logging.cc
index fa70d47..bc60081 100644
--- a/aos/testing/test_logging.cc
+++ b/aos/testing/test_logging.cc
@@ -20,6 +20,22 @@
  public:
   const ::std::vector<LogMessage> &messages() { return messages_; }
 
+  // Sets the current thread's time to be monotonic_now for logging.
+  void MockTime(::aos::monotonic_clock::time_point monotonic_now) {
+    mock_time_ = true;
+    monotonic_now_ = monotonic_now;
+  }
+
+  // Clears any mock time for the current thread.
+  void UnMockTime() { mock_time_ = false; }
+
+  ::aos::monotonic_clock::time_point monotonic_now() const override {
+    if (mock_time_) {
+      return monotonic_now_;
+    }
+    return ::aos::monotonic_clock::now();
+  }
+
   // This class has to be a singleton so that everybody can get access to the
   // same instance to read out the messages etc.
   static TestLogImplementation *GetInstance() {
@@ -80,10 +96,20 @@
   bool print_as_messages_come_in_ = false;
   FILE *output_file_ = stdout;
   ::aos::Mutex messages_mutex_;
+
+  // Thread local storage for mock time.  This is thread local because if
+  // someone spawns a thread and goes to town in parallel with a simulated event
+  // loop, we want to just print the actual monotonic clock out.
+  static thread_local bool mock_time_;
+  static thread_local ::aos::monotonic_clock::time_point monotonic_now_;
 };
 
+thread_local bool TestLogImplementation::mock_time_ = false;
+thread_local ::aos::monotonic_clock::time_point
+    TestLogImplementation::monotonic_now_ = ::aos::monotonic_clock::min_time;
+
 class MyTestEventListener : public ::testing::EmptyTestEventListener {
-  virtual void OnTestStart(const ::testing::TestInfo &/*test_info*/) {
+  virtual void OnTestStart(const ::testing::TestInfo & /*test_info*/) {
     TestLogImplementation::GetInstance()->ClearMessages();
   }
   virtual void OnTestEnd(const ::testing::TestInfo &test_info) {
@@ -141,5 +167,12 @@
   TestLogImplementation::GetInstance()->PrintMessagesAsTheyComeIn();
 }
 
+void MockTime(::aos::monotonic_clock::time_point monotonic_now) {
+  TestLogImplementation::GetInstance()->MockTime(monotonic_now);
+}
+void UnMockTime() {
+  TestLogImplementation::GetInstance()->UnMockTime();
+}
+
 }  // namespace testing
 }  // namespace aos
diff --git a/aos/testing/test_logging.h b/aos/testing/test_logging.h
index a4d835a..e7059d1 100644
--- a/aos/testing/test_logging.h
+++ b/aos/testing/test_logging.h
@@ -1,6 +1,8 @@
 #ifndef AOS_TESTING_TEST_LOGGING_H_
 #define AOS_TESTING_TEST_LOGGING_H_
 
+#include "aos/time/time.h"
+
 namespace aos {
 namespace testing {
 
@@ -21,6 +23,13 @@
 // we want to use graphing tools to verify what's happening.
 void ForcePrintLogsDuringTests();
 
+// Sets the current mock logging time to monotonic_now.  This only applies to
+// the current thread.
+void MockTime(::aos::monotonic_clock::time_point monotonic_now);
+// Clears the mock logging time for the current thread and goes back to using
+// monotonic_clock::now().
+void UnMockTime();
+
 }  // namespace testing
 }  // namespace aos