CommandProcessor: add setters for TimestampHeader
am: 188f047fb8

Change-Id: I15ed66ff20ecc47336c1486b1677e952f5491c3e
diff --git a/command_processor.cpp b/command_processor.cpp
index 67484de..64e1489 100644
--- a/command_processor.cpp
+++ b/command_processor.cpp
@@ -41,6 +41,46 @@
 
 namespace {
 
+uint32_t NsecToUsec(uint32_t nsec);
+
+class TimestampHeader {
+ public:
+  TimestampHeader& set_since_boot_awake_only(Os::Timestamp new_value) {
+    since_boot_awake_only = new_value;
+    return *this;
+  }
+
+  TimestampHeader& set_since_boot_with_sleep(Os::Timestamp new_value) {
+    since_boot_with_sleep = new_value;
+    return *this;
+  }
+
+  TimestampHeader& set_since_epoch(Os::Timestamp new_value) {
+    since_epoch = new_value;
+    return *this;
+  }
+
+  // Returns a string with a formatted representation of the timestamps
+  // contained within this header.
+  std::string ToString() const {
+    const auto& awake_time = since_boot_awake_only;
+    const auto& up_time = since_boot_with_sleep;
+    const auto& wall_time = since_epoch;
+    return base::StringPrintf("%" PRIu32 ".%06" PRIu32
+                              " "
+                              "%" PRIu32 ".%06" PRIu32
+                              " "
+                              "%" PRIu32 ".%06" PRIu32,
+                              awake_time.secs, NsecToUsec(awake_time.nsecs),
+                              up_time.secs, NsecToUsec(up_time.nsecs),
+                              wall_time.secs, NsecToUsec(wall_time.nsecs));
+  }
+
+  Os::Timestamp since_boot_awake_only;
+  Os::Timestamp since_boot_with_sleep;
+  Os::Timestamp since_epoch;  // non-monotonic
+};
+
 constexpr char kUnprintableCharReplacement = '?';
 
 std::string MakeSanitizedString(const uint8_t* buf, size_t buf_len);
@@ -121,20 +161,6 @@
 
 // Private methods below.
 
-std::string CommandProcessor::TimestampHeader::ToString() const {
-  const auto& awake_time = since_boot_awake_only;
-  const auto& up_time = since_boot_with_sleep;
-  const auto& wall_time = since_epoch;
-  return base::StringPrintf("%" PRIu32 ".%06" PRIu32
-                            " "
-                            "%" PRIu32 ".%06" PRIu32
-                            " "
-                            "%" PRIu32 ".%06" PRIu32,
-                            awake_time.secs, NsecToUsec(awake_time.nsecs),
-                            up_time.secs, NsecToUsec(up_time.nsecs),
-                            wall_time.secs, NsecToUsec(wall_time.nsecs));
-}
-
 bool CommandProcessor::CopyCommandToLog(const void* command_buffer,
                                         size_t command_len_in) {
   const uint16_t command_len =
@@ -157,11 +183,11 @@
   }
   CHECK(current_log_buffer_.CanFitNow(total_size));
 
-  TimestampHeader tstamp_header;
-  tstamp_header.since_boot_awake_only = os_->GetTimestamp(CLOCK_MONOTONIC);
-  tstamp_header.since_boot_with_sleep = os_->GetTimestamp(CLOCK_BOOTTIME);
-  tstamp_header.since_epoch = os_->GetTimestamp(CLOCK_REALTIME);
-
+  const auto& tstamp_header =
+      TimestampHeader()
+          .set_since_boot_awake_only(os_->GetTimestamp(CLOCK_MONOTONIC))
+          .set_since_boot_with_sleep(os_->GetTimestamp(CLOCK_BOOTTIME))
+          .set_since_epoch(os_->GetTimestamp(CLOCK_REALTIME));
   const auto& message_buf =
       ByteBuffer<sizeof(TimestampHeader) + protocol::kMaxMessageSize>()
           .AppendOrDie(&tstamp_header, sizeof(tstamp_header))
diff --git a/command_processor.h b/command_processor.h
index 24ac21f..ae9d91c 100644
--- a/command_processor.h
+++ b/command_processor.h
@@ -55,17 +55,6 @@
                       int fd);
 
  private:
-  class TimestampHeader {
-   public:
-    // Returns a string with a formatted representation of the timestamps
-    // contained within this header.
-    std::string ToString() const;
-
-    Os::Timestamp since_boot_awake_only;
-    Os::Timestamp since_boot_with_sleep;
-    Os::Timestamp since_epoch;  // non-monotonic
-  };
-
   // Copies |command_buffer| into the log buffer. Returns true if the
   // command was copied. If |command_len| exceeds protocol::kMaxMessageSize,
   // copies the first protocol::kMaxMessageSize of |command_buffer|, and returns