logd: create FlushToState class

ChattyLogBuffer::FlushTo() needs an array of pid_t's to differentiate
between deduplication and spam removal chatty messages, but that won't
be useful to other log buffers, so it doesn't deserve its own entry in
the abstruct LogBuffer::FlushTo() function.

Other log buffers may need their own data stored for each reader, so
we create an interface that the reader itself owns and passes to the
log buffer.  It uses a unique_ptr, such that the when the reader is
destroyed, so will this state.

FlushToState will additionally contain the start point, that it will
increment itself and the log mask, which LogBuffers can use to
efficiently keep track of the next elements that will be read during a
call to FlushTo().

Side benefit: this allows ChattyLogBufferTests to correctly report
'identical' instead of 'expired' lines the deduplication tests.

Side benefit #2: This updates LogReaderThread::start() more
aggressively, which should result in readers being disconnected less
often, particularly readers who read only a certain UID.

Test: logging unit tests
Change-Id: I969565eb2996afb1431f20e7ccaaa906fcb8f6d1
diff --git a/logd/LogReaderThread.h b/logd/LogReaderThread.h
index ba81063..f288d68 100644
--- a/logd/LogReaderThread.h
+++ b/logd/LogReaderThread.h
@@ -38,7 +38,7 @@
   public:
     LogReaderThread(LogBuffer* log_buffer, LogReaderList* reader_list,
                     std::unique_ptr<LogWriter> writer, bool non_block, unsigned long tail,
-                    unsigned int log_mask, pid_t pid, log_time start_time, uint64_t sequence,
+                    LogMask log_mask, pid_t pid, log_time start_time, uint64_t sequence,
                     std::chrono::steady_clock::time_point deadline);
     void triggerReader_Locked() { thread_triggered_condition_.notify_all(); }
 
@@ -52,11 +52,13 @@
         thread_triggered_condition_.notify_all();
     }
 
-    bool IsWatching(log_id_t id) const { return log_mask_ & (1 << id); }
-    bool IsWatchingMultiple(unsigned int log_mask) const { return log_mask_ & log_mask; }
+    bool IsWatching(log_id_t id) const { return flush_to_state_->log_mask() & (1 << id); }
+    bool IsWatchingMultiple(LogMask log_mask) const {
+        return flush_to_state_->log_mask() & log_mask;
+    }
 
     std::string name() const { return writer_->name(); }
-    uint64_t start() const { return start_; }
+    uint64_t start() const { return flush_to_state_->start(); }
     std::chrono::steady_clock::time_point deadline() const { return deadline_; }
 
   private:
@@ -78,16 +80,14 @@
     // messages should be ignored.
     bool leading_dropped_;
 
-    // A mask of the logs buffers that are read by this reader.
-    const unsigned int log_mask_;
     // If set to non-zero, only pids equal to this are read by the reader.
     const pid_t pid_;
     // When a reader is referencing (via start_) old elements in the log buffer, and the log
     // buffer's size grows past its memory limit, the log buffer may request the reader to skip
     // ahead a specified number of logs.
     unsigned int skip_ahead_[LOG_ID_MAX];
-    // Used for distinguishing 'dropped' messages for duplicate logs vs chatty drops
-    pid_t last_tid_[LOG_ID_MAX];
+    // LogBuffer::FlushTo() needs to store state across subsequent calls.
+    std::unique_ptr<FlushToState> flush_to_state_;
 
     // These next three variables are used for reading only the most recent lines aka `adb logcat
     // -t` / `adb logcat -T`.
@@ -103,8 +103,6 @@
     // When a reader requests logs starting from a given timestamp, its stored here for the first
     // pass, such that logs before this time stamp that are accumulated in the buffer are ignored.
     log_time start_time_;
-    // The point from which the reader will read logs once awoken.
-    uint64_t start_;
     // CLOCK_MONOTONIC based deadline used for log wrapping.  If this deadline expires before logs
     // wrap, then wake up and send the logs to the reader anyway.
     std::chrono::steady_clock::time_point deadline_;