logd: remove SocketClient from LogBuffer and LogBufferElement

In the future, we'll want to be able to write to outputs that are not
necessarily a libsysutils SocketClient, for example host tests of
LogBuffer.  Therefore, we add a LogWriter class to be used instead of
SocketClient.

Test: logging unit tests
Change-Id: I4385be65e14e83a635691a7ba79e9bf060e49484
diff --git a/logd/LogReaderThread.cpp b/logd/LogReaderThread.cpp
index e58e3eb..b2001b5 100644
--- a/logd/LogReaderThread.cpp
+++ b/logd/LogReaderThread.cpp
@@ -23,50 +23,40 @@
 #include <thread>
 
 #include "LogBuffer.h"
-#include "LogReader.h"
+#include "LogReaderList.h"
 
 using namespace std::placeholders;
 
-LogReaderThread::LogReaderThread(LogReader& reader, LogReaderList& reader_list,
-                                 SocketClient* client, bool non_block, unsigned long tail,
-                                 unsigned int log_mask, pid_t pid, log_time start_time,
-                                 uint64_t start, std::chrono::steady_clock::time_point deadline,
-                                 bool privileged, bool can_read_security_logs)
-    : leading_dropped_(false),
-      reader_(reader),
+LogReaderThread::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 start,
+                                 std::chrono::steady_clock::time_point deadline)
+    : log_buffer_(log_buffer),
       reader_list_(reader_list),
+      writer_(std::move(writer)),
+      leading_dropped_(false),
       log_mask_(log_mask),
       pid_(pid),
       tail_(tail),
       count_(0),
       index_(0),
-      client_(client),
       start_time_(start_time),
       start_(start),
       deadline_(deadline),
-      non_block_(non_block),
-      privileged_(privileged),
-      can_read_security_logs_(can_read_security_logs) {
+      non_block_(non_block) {
     memset(last_tid_, 0, sizeof(last_tid_));
     cleanSkip_Locked();
-}
-
-bool LogReaderThread::startReader_Locked() {
     auto thread = std::thread{&LogReaderThread::ThreadFunction, this};
     thread.detach();
-    return true;
 }
 
 void LogReaderThread::ThreadFunction() {
     prctl(PR_SET_NAME, "logd.reader.per");
 
-    SocketClient* client = client_;
-
-    LogBuffer& logbuf = *reader_.log_buffer();
-
     leading_dropped_ = true;
 
-    auto lock = std::unique_lock{reader_list_.reader_threads_lock()};
+    auto lock = std::unique_lock{reader_list_->reader_threads_lock()};
 
     uint64_t start = start_;
 
@@ -84,14 +74,14 @@
         lock.unlock();
 
         if (tail_) {
-            logbuf.FlushTo(client, start, nullptr, privileged_, can_read_security_logs_,
-                           std::bind(&LogReaderThread::FilterFirstPass, this, _1));
+            log_buffer_->FlushTo(writer_.get(), start, nullptr,
+                                 std::bind(&LogReaderThread::FilterFirstPass, this, _1));
             leading_dropped_ =
                     true;  // TODO: Likely a bug, if leading_dropped_ was not true before calling
                            // flushTo(), then it should not be reset to true after.
         }
-        start = logbuf.FlushTo(client, start, last_tid_, privileged_, can_read_security_logs_,
-                               std::bind(&LogReaderThread::FilterSecondPass, this, _1));
+        start = log_buffer_->FlushTo(writer_.get(), start, last_tid_,
+                                     std::bind(&LogReaderThread::FilterSecondPass, this, _1));
 
         // We only ignore entries before the original start time for the first flushTo(), if we
         // get entries after this first flush before the original start time, then the client
@@ -104,7 +94,7 @@
 
         lock.lock();
 
-        if (start == LogBufferElement::FLUSH_ERROR) {
+        if (start == LogBuffer::FLUSH_ERROR) {
             break;
         }
 
@@ -121,10 +111,9 @@
         }
     }
 
-    reader_.release(client);
-    client->decRef();
+    writer_->Release();
 
-    auto& log_reader_threads = reader_list_.reader_threads();
+    auto& log_reader_threads = reader_list_->reader_threads();
     auto it = std::find_if(log_reader_threads.begin(), log_reader_threads.end(),
                            [this](const auto& other) { return other.get() == this; });
 
@@ -135,7 +124,7 @@
 
 // A first pass to count the number of elements
 FlushToResult LogReaderThread::FilterFirstPass(const LogBufferElement* element) {
-    auto lock = std::lock_guard{reader_list_.reader_threads_lock()};
+    auto lock = std::lock_guard{reader_list_->reader_threads_lock()};
 
     if (leading_dropped_) {
         if (element->getDropped()) {
@@ -158,7 +147,7 @@
 
 // A second pass to send the selected elements
 FlushToResult LogReaderThread::FilterSecondPass(const LogBufferElement* element) {
-    auto lock = std::lock_guard{reader_list_.reader_threads_lock()};
+    auto lock = std::lock_guard{reader_list_->reader_threads_lock()};
 
     start_ = element->getSequence();