Merge changes I85e6c989,Ie4ecc502,I1f8cfbb7

* changes:
  logd: build liblogd and its test on host
  logd: add LogBufferTest.cpp
  liblog: remove unused log_time functions, inline the others
diff --git a/liblog/include/log/log_time.h b/liblog/include/log/log_time.h
index 6b4458c..f50764d 100644
--- a/liblog/include/log/log_time.h
+++ b/liblog/include/log/log_time.h
@@ -37,9 +37,7 @@
   uint32_t tv_sec = 0; /* good to Feb 5 2106 */
   uint32_t tv_nsec = 0;
 
-  static const uint32_t tv_sec_max = 0xFFFFFFFFUL;
-  static const uint32_t tv_nsec_max = 999999999UL;
-  static const timespec EPOCH;
+  static constexpr timespec EPOCH = {0, 0};
 
   log_time() {}
   explicit log_time(const timespec& T)
@@ -55,16 +53,6 @@
     tv_nsec = static_cast<uint32_t>(T.tv_nsec);
   }
 #endif
-  explicit log_time(const char* T) {
-    const uint8_t* c = reinterpret_cast<const uint8_t*>(T);
-    tv_sec = c[0] | (static_cast<uint32_t>(c[1]) << 8) |
-             (static_cast<uint32_t>(c[2]) << 16) |
-             (static_cast<uint32_t>(c[3]) << 24);
-    tv_nsec = c[4] | (static_cast<uint32_t>(c[5]) << 8) |
-              (static_cast<uint32_t>(c[6]) << 16) |
-              (static_cast<uint32_t>(c[7]) << 24);
-  }
-
   /* timespec */
   bool operator==(const timespec& T) const {
     return (tv_sec == static_cast<uint32_t>(T.tv_sec)) &&
@@ -90,17 +78,6 @@
     return !(*this > T);
   }
 
-  log_time operator-=(const timespec& T);
-  log_time operator-(const timespec& T) const {
-    log_time local(*this);
-    return local -= T;
-  }
-  log_time operator+=(const timespec& T);
-  log_time operator+(const timespec& T) const {
-    log_time local(*this);
-    return local += T;
-  }
-
   /* log_time */
   bool operator==(const log_time& T) const {
     return (tv_sec == T.tv_sec) && (tv_nsec == T.tv_nsec);
@@ -123,12 +100,36 @@
     return !(*this > T);
   }
 
-  log_time operator-=(const log_time& T);
+  log_time operator-=(const log_time& T) {
+    // No concept of negative time, clamp to EPOCH
+    if (*this <= T) {
+      return *this = log_time(EPOCH);
+    }
+
+    if (this->tv_nsec < T.tv_nsec) {
+      --this->tv_sec;
+      this->tv_nsec = NS_PER_SEC + this->tv_nsec - T.tv_nsec;
+    } else {
+      this->tv_nsec -= T.tv_nsec;
+    }
+    this->tv_sec -= T.tv_sec;
+
+    return *this;
+  }
   log_time operator-(const log_time& T) const {
     log_time local(*this);
     return local -= T;
   }
-  log_time operator+=(const log_time& T);
+  log_time operator+=(const log_time& T) {
+    this->tv_nsec += T.tv_nsec;
+    if (this->tv_nsec >= NS_PER_SEC) {
+      this->tv_nsec -= NS_PER_SEC;
+      ++this->tv_sec;
+    }
+    this->tv_sec += T.tv_sec;
+
+    return *this;
+  }
   log_time operator+(const log_time& T) const {
     log_time local(*this);
     return local += T;
@@ -146,10 +147,8 @@
            tv_nsec / (NS_PER_SEC / MS_PER_SEC);
   }
 
-  static const char default_format[];
-
   /* Add %#q for the fraction of a second to the standard library functions */
-  char* strptime(const char* s, const char* format = default_format);
+  char* strptime(const char* s, const char* format);
 } __attribute__((__packed__));
 }
 
diff --git a/liblog/log_time.cpp b/liblog/log_time.cpp
index 3fbe1cb..14c408c 100644
--- a/liblog/log_time.cpp
+++ b/liblog/log_time.cpp
@@ -21,11 +21,7 @@
 
 #include <private/android_logger.h>
 
-const char log_time::default_format[] = "%m-%d %H:%M:%S.%q";
-const timespec log_time::EPOCH = {0, 0};
-
 // Add %#q for fractional seconds to standard strptime function
-
 char* log_time::strptime(const char* s, const char* format) {
   time_t now;
 #ifdef __linux__
@@ -131,59 +127,3 @@
 #endif
   return ret;
 }
-
-log_time log_time::operator-=(const timespec& T) {
-  // No concept of negative time, clamp to EPOCH
-  if (*this <= T) {
-    return *this = log_time(EPOCH);
-  }
-
-  if (this->tv_nsec < (unsigned long int)T.tv_nsec) {
-    --this->tv_sec;
-    this->tv_nsec = NS_PER_SEC + this->tv_nsec - T.tv_nsec;
-  } else {
-    this->tv_nsec -= T.tv_nsec;
-  }
-  this->tv_sec -= T.tv_sec;
-
-  return *this;
-}
-
-log_time log_time::operator+=(const timespec& T) {
-  this->tv_nsec += (unsigned long int)T.tv_nsec;
-  if (this->tv_nsec >= NS_PER_SEC) {
-    this->tv_nsec -= NS_PER_SEC;
-    ++this->tv_sec;
-  }
-  this->tv_sec += T.tv_sec;
-
-  return *this;
-}
-
-log_time log_time::operator-=(const log_time& T) {
-  // No concept of negative time, clamp to EPOCH
-  if (*this <= T) {
-    return *this = log_time(EPOCH);
-  }
-
-  if (this->tv_nsec < T.tv_nsec) {
-    --this->tv_sec;
-    this->tv_nsec = NS_PER_SEC + this->tv_nsec - T.tv_nsec;
-  } else {
-    this->tv_nsec -= T.tv_nsec;
-  }
-  this->tv_sec -= T.tv_sec;
-
-  return *this;
-}
-
-log_time log_time::operator+=(const log_time& T) {
-  this->tv_nsec += T.tv_nsec;
-  if (this->tv_nsec >= NS_PER_SEC) {
-    this->tv_nsec -= NS_PER_SEC;
-    ++this->tv_sec;
-  }
-  this->tv_sec += T.tv_sec;
-
-  return *this;
-}
diff --git a/liblog/tests/liblog_benchmark.cpp b/liblog/tests/liblog_benchmark.cpp
index a4e4def..3bd5cf2 100644
--- a/liblog/tests/liblog_benchmark.cpp
+++ b/liblog/tests/liblog_benchmark.cpp
@@ -684,8 +684,8 @@
       if (!eventData || (eventData[4] != EVENT_TYPE_LONG)) {
         continue;
       }
-      log_time tx(eventData + 4 + 1);
-      if (ts != tx) {
+      log_time* tx = reinterpret_cast<log_time*>(eventData + 4 + 1);
+      if (ts != *tx) {
         if (0xDEADBEEFA55A5AA5ULL == caught_convert(eventData + 4 + 1)) {
           state.SkipWithError("signal");
           break;
@@ -757,8 +757,8 @@
       if (!eventData || (eventData[4] != EVENT_TYPE_LONG)) {
         continue;
       }
-      log_time tx(eventData + 4 + 1);
-      if (ts != tx) {
+      log_time* tx = reinterpret_cast<log_time*>(eventData + 4 + 1);
+      if (ts != *tx) {
         if (0xDEADBEEFA55A5AA6ULL == caught_convert(eventData + 4 + 1)) {
           state.SkipWithError("signal");
           break;
diff --git a/liblog/tests/liblog_test.cpp b/liblog/tests/liblog_test.cpp
index bbc985a..fbc3d7a 100644
--- a/liblog/tests/liblog_test.cpp
+++ b/liblog/tests/liblog_test.cpp
@@ -270,10 +270,10 @@
       return;
     }
 
-    log_time tx(reinterpret_cast<char*>(&eventData->payload.data));
-    if (ts == tx) {
+    log_time* tx = reinterpret_cast<log_time*>(&eventData->payload.data);
+    if (ts == *tx) {
       ++count;
-    } else if (ts1 == tx) {
+    } else if (ts1 == *tx) {
       ++second_count;
     }
 
diff --git a/logcat/tests/logcat_test.cpp b/logcat/tests/logcat_test.cpp
index 3a55c4e..f3fdfd7 100644
--- a/logcat/tests/logcat_test.cpp
+++ b/logcat/tests/logcat_test.cpp
@@ -475,8 +475,8 @@
             continue;
         }
 
-        log_time tx((const char*)&t);
-        if (ts == tx) {
+        log_time* tx = reinterpret_cast<log_time*>(&t);
+        if (ts == *tx) {
             ++count;
         }
     }
@@ -521,8 +521,8 @@
                 continue;
             }
 
-            log_time tx((const char*)&t);
-            if (ts == tx) {
+            log_time* tx = reinterpret_cast<log_time*>(&t);
+            if (ts == *tx) {
                 ++count;
             }
         }
diff --git a/logd/Android.bp b/logd/Android.bp
index d79e258..7e58f07 100644
--- a/logd/Android.bp
+++ b/logd/Android.bp
@@ -45,7 +45,7 @@
 cc_library_static {
     name: "liblogd",
     defaults: ["logd_defaults"],
-
+    host_supported: true,
     srcs: [
         "ChattyLogBuffer.cpp",
         "LogReaderList.cpp",
@@ -110,3 +110,61 @@
     src: "logtagd.rc",
     sub_dir: "init",
 }
+
+// -----------------------------------------------------------------------------
+// Unit tests.
+// -----------------------------------------------------------------------------
+
+cc_defaults {
+    name: "logd-unit-test-defaults",
+
+    cflags: [
+        "-fstack-protector-all",
+        "-g",
+        "-Wall",
+        "-Wextra",
+        "-Werror",
+        "-fno-builtin",
+
+        "-DAUDITD_LOG_TAG=1003",
+        "-DCHATTY_LOG_TAG=1004",
+    ],
+
+    srcs: [
+        "logd_test.cpp",
+        "LogBufferTest.cpp",
+    ],
+
+    static_libs: [
+        "libbase",
+        "libcutils",
+        "liblog",
+        "liblogd",
+        "libselinux",
+    ],
+}
+
+// Build tests for the logger. Run with:
+//   adb shell /data/nativetest/logd-unit-tests/logd-unit-tests
+cc_test {
+    name: "logd-unit-tests",
+    host_supported: true,
+    defaults: ["logd-unit-test-defaults"],
+}
+
+cc_test {
+    name: "CtsLogdTestCases",
+    defaults: ["logd-unit-test-defaults"],
+    multilib: {
+        lib32: {
+            suffix: "32",
+        },
+        lib64: {
+            suffix: "64",
+        },
+    },
+    test_suites: [
+        "cts",
+        "vts10",
+    ],
+}
diff --git a/logd/tests/AndroidTest.xml b/logd/AndroidTest.xml
similarity index 100%
rename from logd/tests/AndroidTest.xml
rename to logd/AndroidTest.xml
diff --git a/logd/ChattyLogBuffer.cpp b/logd/ChattyLogBuffer.cpp
index c6c9a7c..9e08e9d 100644
--- a/logd/ChattyLogBuffer.cpp
+++ b/logd/ChattyLogBuffer.cpp
@@ -426,31 +426,14 @@
 // Define a temporary mechanism to report the last LogBufferElement pointer
 // for the specified uid, pid and tid. Used below to help merge-sort when
 // pruning for worst UID.
-class LogBufferElementKey {
-    const union {
-        struct {
-            uint32_t uid;
-            uint16_t pid;
-            uint16_t tid;
-        } __packed;
-        uint64_t value;
-    } __packed;
-
-  public:
-    LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid) : uid(uid), pid(pid), tid(tid) {}
-    explicit LogBufferElementKey(uint64_t key) : value(key) {}
-
-    uint64_t getKey() { return value; }
-};
-
 class LogBufferElementLast {
     typedef std::unordered_map<uint64_t, LogBufferElement*> LogBufferElementMap;
     LogBufferElementMap map;
 
   public:
     bool coalesce(LogBufferElement* element, uint16_t dropped) {
-        LogBufferElementKey key(element->getUid(), element->getPid(), element->getTid());
-        LogBufferElementMap::iterator it = map.find(key.getKey());
+        uint64_t key = LogBufferElementKey(element->getUid(), element->getPid(), element->getTid());
+        LogBufferElementMap::iterator it = map.find(key);
         if (it != map.end()) {
             LogBufferElement* found = it->second;
             uint16_t moreDropped = found->getDropped();
@@ -465,8 +448,8 @@
     }
 
     void add(LogBufferElement* element) {
-        LogBufferElementKey key(element->getUid(), element->getPid(), element->getTid());
-        map[key.getKey()] = element;
+        uint64_t key = LogBufferElementKey(element->getUid(), element->getPid(), element->getTid());
+        map[key] = element;
     }
 
     void clear() { map.clear(); }
@@ -483,6 +466,11 @@
             }
         }
     }
+
+  private:
+    uint64_t LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid) {
+        return uint64_t(uid) << 32 | uint64_t(pid) << 16 | uint64_t(tid);
+    }
 };
 
 // If the selected reader is blocking our pruning progress, decide on
@@ -660,7 +648,7 @@
         if (leading) {
             it = GetOldest(id);
         }
-        static const timespec too_old = {EXPIRE_HOUR_THRESHOLD * 60 * 60, 0};
+        static const log_time too_old{EXPIRE_HOUR_THRESHOLD * 60 * 60, 0};
         LogBufferElementCollection::iterator lastt;
         lastt = mLogElements.end();
         --lastt;
diff --git a/logd/LogBufferElement.cpp b/logd/LogBufferElement.cpp
index 8499715..4f5cabd 100644
--- a/logd/LogBufferElement.cpp
+++ b/logd/LogBufferElement.cpp
@@ -183,16 +183,16 @@
     }
     if (name) {
         char* buf = nullptr;
-        asprintf(&buf, "(%s)", name);
-        if (buf) {
+        int result = asprintf(&buf, "(%s)", name);
+        if (result != -1) {
             free(const_cast<char*>(name));
             name = buf;
         }
     }
     if (commName) {
         char* buf = nullptr;
-        asprintf(&buf, " %s", commName);
-        if (buf) {
+        int result = asprintf(&buf, " %s", commName);
+        if (result != -1) {
             free(const_cast<char*>(commName));
             commName = buf;
         }
diff --git a/logd/LogBufferTest.cpp b/logd/LogBufferTest.cpp
new file mode 100644
index 0000000..cc3cb76
--- /dev/null
+++ b/logd/LogBufferTest.cpp
@@ -0,0 +1,337 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "LogBuffer.h"
+
+#include <unistd.h>
+
+#include <memory>
+#include <vector>
+
+#include <android-base/stringprintf.h>
+#include <android-base/strings.h>
+#include <gtest/gtest.h>
+
+#include "ChattyLogBuffer.h"
+#include "LogReaderList.h"
+#include "LogReaderThread.h"
+#include "LogStatistics.h"
+#include "LogTags.h"
+#include "LogWhiteBlackList.h"
+#include "LogWriter.h"
+
+using android::base::Join;
+using android::base::StringPrintf;
+
+#ifndef __ANDROID__
+unsigned long __android_logger_get_buffer_size(log_id_t) {
+    return 1024 * 1024;
+}
+
+bool __android_logger_valid_buffer_size(unsigned long) {
+    return true;
+}
+#endif
+
+void android::prdebug(const char* fmt, ...) {
+    va_list ap;
+    va_start(ap, fmt);
+    vfprintf(stderr, fmt, ap);
+    fprintf(stderr, "\n");
+    va_end(ap);
+}
+
+char* android::uidToName(uid_t) {
+    return nullptr;
+}
+
+class TestWriter : public LogWriter {
+  public:
+    TestWriter(std::vector<std::pair<logger_entry, std::string>>* msgs, bool* released)
+        : LogWriter(0, true, true), msgs_(msgs), released_(released) {}
+    bool Write(const logger_entry& entry, const char* message) override {
+        msgs_->emplace_back(entry, std::string(message, entry.len));
+        return true;
+    }
+
+    void Release() {
+        if (released_) *released_ = true;
+    }
+
+    std::string name() const override { return "test_writer"; }
+
+  private:
+    std::vector<std::pair<logger_entry, std::string>>* msgs_;
+    bool* released_;
+};
+
+class LogBufferTest : public testing::Test {
+  protected:
+    void SetUp() override {
+        log_buffer_.reset(new ChattyLogBuffer(&reader_list_, &tags_, &prune_, &stats_));
+    }
+
+    void FixupMessages(std::vector<std::pair<logger_entry, std::string>>* messages) {
+        for (auto& [entry, message] : *messages) {
+            entry.hdr_size = sizeof(logger_entry);
+            entry.len = message.size();
+        }
+    }
+
+    void LogMessages(const std::vector<std::pair<logger_entry, std::string>>& messages) {
+        for (auto& [entry, message] : messages) {
+            log_buffer_->Log(static_cast<log_id_t>(entry.lid), log_time(entry.sec, entry.nsec),
+                             entry.uid, entry.pid, entry.tid, message.c_str(), message.size());
+        }
+    }
+
+    std::vector<std::string> CompareLoggerEntries(const logger_entry& expected,
+                                                  const logger_entry& result) {
+        std::vector<std::string> errors;
+        if (expected.len != result.len) {
+            errors.emplace_back(
+                    StringPrintf("len: %" PRIu16 " vs %" PRIu16, expected.len, result.len));
+        }
+        if (expected.hdr_size != result.hdr_size) {
+            errors.emplace_back(StringPrintf("hdr_size: %" PRIu16 " vs %" PRIu16, expected.hdr_size,
+                                             result.hdr_size));
+        }
+        if (expected.pid != result.pid) {
+            errors.emplace_back(
+                    StringPrintf("pid: %" PRIi32 " vs %" PRIi32, expected.pid, result.pid));
+        }
+        if (expected.tid != result.tid) {
+            errors.emplace_back(
+                    StringPrintf("tid: %" PRIu32 " vs %" PRIu32, expected.tid, result.tid));
+        }
+        if (expected.sec != result.sec) {
+            errors.emplace_back(
+                    StringPrintf("sec: %" PRIu32 " vs %" PRIu32, expected.sec, result.sec));
+        }
+        if (expected.nsec != result.nsec) {
+            errors.emplace_back(
+                    StringPrintf("nsec: %" PRIu32 " vs %" PRIu32, expected.nsec, result.nsec));
+        }
+        if (expected.lid != result.lid) {
+            errors.emplace_back(
+                    StringPrintf("lid: %" PRIu32 " vs %" PRIu32, expected.lid, result.lid));
+        }
+        if (expected.uid != result.uid) {
+            errors.emplace_back(
+                    StringPrintf("uid: %" PRIu32 " vs %" PRIu32, expected.uid, result.uid));
+        }
+        return errors;
+    }
+
+    std::string CompareMessages(const std::string& expected, const std::string& result) {
+        if (expected == result) {
+            return {};
+        }
+        auto shorten_string = [](const std::string& in) {
+            if (in.size() > 10) {
+                return in.substr(0, 10) + "...";
+            }
+            return in;
+        };
+
+        size_t diff_index = 0;
+        for (; diff_index < std::min(expected.size(), result.size()); ++diff_index) {
+            if (expected[diff_index] != result[diff_index]) {
+                break;
+            }
+        }
+
+        if (diff_index < 10) {
+            auto expected_short = shorten_string(expected);
+            auto result_short = shorten_string(result);
+            return StringPrintf("msg: %s vs %s", expected_short.c_str(), result_short.c_str());
+        }
+
+        auto expected_short = shorten_string(expected.substr(diff_index));
+        auto result_short = shorten_string(result.substr(diff_index));
+        return StringPrintf("msg: index %zu: %s vs %s", diff_index, expected_short.c_str(),
+                            result_short.c_str());
+    }
+
+    void CompareLogMessages(const std::vector<std::pair<logger_entry, std::string>>& expected,
+                            const std::vector<std::pair<logger_entry, std::string>>& result) {
+        EXPECT_EQ(expected.size(), result.size());
+        size_t end = std::min(expected.size(), result.size());
+        size_t num_errors = 0;
+        for (size_t i = 0; i < end; ++i) {
+            auto errors = CompareLoggerEntries(expected[i].first, result[i].first);
+            auto msg_error = CompareMessages(expected[i].second, result[i].second);
+            if (!msg_error.empty()) {
+                errors.emplace_back(msg_error);
+            }
+            if (!errors.empty()) {
+                GTEST_LOG_(ERROR) << "Mismatch log message " << i << " " << Join(errors, " ");
+                ++num_errors;
+            }
+        }
+        EXPECT_EQ(0U, num_errors);
+    }
+
+    LogReaderList reader_list_;
+    LogTags tags_;
+    PruneList prune_;
+    LogStatistics stats_{false};
+    std::unique_ptr<LogBuffer> log_buffer_;
+};
+
+TEST_F(LogBufferTest, smoke) {
+    std::vector<std::pair<logger_entry, std::string>> log_messages = {
+            {{
+                     .pid = 1,
+                     .tid = 1,
+                     .sec = 1234,
+                     .nsec = 323001,
+                     .lid = LOG_ID_MAIN,
+                     .uid = 0,
+             },
+             "smoke test"},
+    };
+    FixupMessages(&log_messages);
+    LogMessages(log_messages);
+
+    std::vector<std::pair<logger_entry, std::string>> read_log_messages;
+    std::unique_ptr<LogWriter> test_writer(new TestWriter(&read_log_messages, nullptr));
+    uint64_t flush_result = log_buffer_->FlushTo(test_writer.get(), 1, nullptr, nullptr);
+    EXPECT_EQ(1ULL, flush_result);
+    CompareLogMessages(log_messages, read_log_messages);
+}
+
+TEST_F(LogBufferTest, smoke_with_reader_thread) {
+    std::vector<std::pair<logger_entry, std::string>> log_messages = {
+            {{.pid = 1, .tid = 2, .sec = 10000, .nsec = 20001, .lid = LOG_ID_MAIN, .uid = 0},
+             "first"},
+            {{.pid = 10, .tid = 2, .sec = 10000, .nsec = 20002, .lid = LOG_ID_MAIN, .uid = 0},
+             "second"},
+            {{.pid = 100, .tid = 2, .sec = 10000, .nsec = 20003, .lid = LOG_ID_KERNEL, .uid = 0},
+             "third"},
+            {{.pid = 10, .tid = 2, .sec = 10000, .nsec = 20004, .lid = LOG_ID_MAIN, .uid = 0},
+             "fourth"},
+            {{.pid = 1, .tid = 2, .sec = 10000, .nsec = 20005, .lid = LOG_ID_RADIO, .uid = 0},
+             "fifth"},
+            {{.pid = 2, .tid = 2, .sec = 10000, .nsec = 20006, .lid = LOG_ID_RADIO, .uid = 0},
+             "sixth"},
+            {{.pid = 3, .tid = 2, .sec = 10000, .nsec = 20007, .lid = LOG_ID_RADIO, .uid = 0},
+             "seventh"},
+            {{.pid = 4, .tid = 2, .sec = 10000, .nsec = 20008, .lid = LOG_ID_MAIN, .uid = 0},
+             "eighth"},
+            {{.pid = 5, .tid = 2, .sec = 10000, .nsec = 20009, .lid = LOG_ID_CRASH, .uid = 0},
+             "nineth"},
+            {{.pid = 6, .tid = 2, .sec = 10000, .nsec = 20011, .lid = LOG_ID_MAIN, .uid = 0},
+             "tenth"},
+    };
+    FixupMessages(&log_messages);
+    LogMessages(log_messages);
+
+    std::vector<std::pair<logger_entry, std::string>> read_log_messages;
+    bool released = false;
+
+    {
+        auto lock = std::unique_lock{reader_list_.reader_threads_lock()};
+        std::unique_ptr<LogWriter> test_writer(new TestWriter(&read_log_messages, &released));
+        std::unique_ptr<LogReaderThread> log_reader(
+                new LogReaderThread(log_buffer_.get(), &reader_list_, std::move(test_writer), true,
+                                    0, ~0, 0, {}, 1, {}));
+        reader_list_.reader_threads().emplace_back(std::move(log_reader));
+    }
+
+    while (!released) {
+        usleep(5000);
+    }
+    {
+        auto lock = std::unique_lock{reader_list_.reader_threads_lock()};
+        EXPECT_EQ(0U, reader_list_.reader_threads().size());
+    }
+    CompareLogMessages(log_messages, read_log_messages);
+}
+
+// Generate random messages, set the 'sec' parameter explicit though, to be able to track the
+// expected order of messages.
+std::pair<logger_entry, std::string> GenerateRandomLogMessage(uint32_t sec) {
+    auto rand_uint32 = [](int max) -> uint32_t { return rand() % max; };
+    logger_entry entry = {
+            .hdr_size = sizeof(logger_entry),
+            .pid = rand() % 5000,
+            .tid = rand_uint32(5000),
+            .sec = sec,
+            .nsec = rand_uint32(NS_PER_SEC),
+            .lid = rand_uint32(LOG_ID_STATS),
+            .uid = rand_uint32(100000),
+    };
+
+    // See comment in ChattyLogBuffer::Log() for why this is disallowed.
+    if (entry.nsec % 1000 == 0) {
+        ++entry.nsec;
+    }
+
+    if (entry.lid == LOG_ID_EVENTS) {
+        entry.lid = LOG_ID_KERNEL;
+    }
+
+    std::string message;
+    char priority = ANDROID_LOG_INFO + rand() % 2;
+    message.push_back(priority);
+
+    int tag_length = 2 + rand() % 10;
+    for (int i = 0; i < tag_length; ++i) {
+        message.push_back('a' + rand() % 26);
+    }
+    message.push_back('\0');
+
+    int msg_length = 2 + rand() % 1000;
+    for (int i = 0; i < msg_length; ++i) {
+        message.push_back('a' + rand() % 26);
+    }
+    message.push_back('\0');
+
+    entry.len = message.size();
+
+    return {entry, message};
+}
+
+TEST_F(LogBufferTest, random_messages) {
+    srand(1);
+    std::vector<std::pair<logger_entry, std::string>> log_messages;
+    for (size_t i = 0; i < 1000; ++i) {
+        log_messages.emplace_back(GenerateRandomLogMessage(i));
+    }
+    LogMessages(log_messages);
+
+    std::vector<std::pair<logger_entry, std::string>> read_log_messages;
+    bool released = false;
+
+    {
+        auto lock = std::unique_lock{reader_list_.reader_threads_lock()};
+        std::unique_ptr<LogWriter> test_writer(new TestWriter(&read_log_messages, &released));
+        std::unique_ptr<LogReaderThread> log_reader(
+                new LogReaderThread(log_buffer_.get(), &reader_list_, std::move(test_writer), true,
+                                    0, ~0, 0, {}, 1, {}));
+        reader_list_.reader_threads().emplace_back(std::move(log_reader));
+    }
+
+    while (!released) {
+        usleep(5000);
+    }
+    {
+        auto lock = std::unique_lock{reader_list_.reader_threads_lock()};
+        EXPECT_EQ(0U, reader_list_.reader_threads().size());
+    }
+    CompareLogMessages(log_messages, read_log_messages);
+}
diff --git a/logd/LogTags.cpp b/logd/LogTags.cpp
index 8250808..3afe3ee 100644
--- a/logd/LogTags.cpp
+++ b/logd/LogTags.cpp
@@ -32,6 +32,7 @@
 #include <android-base/macros.h>
 #include <android-base/scopeguard.h>
 #include <android-base/stringprintf.h>
+#include <android-base/threads.h>
 #include <log/log_event_list.h>
 #include <log/log_properties.h>
 #include <log/log_read.h>
@@ -550,10 +551,10 @@
     clock_gettime(CLOCK_REALTIME, &ts);
 
     android_log_header_t header = {
-        .id = LOG_ID_EVENTS,
-        .tid = (uint16_t)gettid(),
-        .realtime.tv_sec = (uint32_t)ts.tv_sec,
-        .realtime.tv_nsec = (uint32_t)ts.tv_nsec,
+            .id = LOG_ID_EVENTS,
+            .tid = static_cast<uint16_t>(android::base::GetThreadId()),
+            .realtime.tv_sec = static_cast<uint32_t>(ts.tv_sec),
+            .realtime.tv_nsec = static_cast<uint32_t>(ts.tv_nsec),
     };
 
     uint32_t outTag = TAG_DEF_LOG_TAG;
diff --git a/logd/LogUtils.h b/logd/LogUtils.h
index ce82b41..c472167 100644
--- a/logd/LogUtils.h
+++ b/logd/LogUtils.h
@@ -30,7 +30,7 @@
 
 // Furnished in main.cpp. Caller must own and free returned value
 char* uidToName(uid_t uid);
-void prdebug(const char* fmt, ...) __printflike(1, 2);
+void prdebug(const char* fmt, ...) __attribute__((__format__(printf, 1, 2)));
 
 // Caller must own and free returned value
 char* pidToName(pid_t pid);
diff --git a/logd/tests/logd_test.cpp b/logd/logd_test.cpp
similarity index 98%
rename from logd/tests/logd_test.cpp
rename to logd/logd_test.cpp
index 55737e9..ed34ea4 100644
--- a/logd/tests/logd_test.cpp
+++ b/logd/logd_test.cpp
@@ -39,7 +39,7 @@
 #include <selinux/selinux.h>
 #endif
 
-#include "../LogReader.h"  // pickup LOGD_SNDTIMEO
+#include "LogReader.h"  // pickup LOGD_SNDTIMEO
 
 #ifdef __ANDROID__
 static void send_to_control(char* buf, size_t len) {
@@ -904,10 +904,10 @@
             (log_msg.entry.len == (4 + 1 + 8))) {
             if (tag != 0) continue;
 
-            log_time tx(eventData + 4 + 1);
-            if (ts == tx) {
+            log_time* tx = reinterpret_cast<log_time*>(eventData + 4 + 1);
+            if (ts == *tx) {
                 ++count;
-            } else if (ts1 == tx) {
+            } else if (ts1 == *tx) {
                 ++second_count;
             }
         } else if (eventData[4] == EVENT_TYPE_STRING) {
diff --git a/logd/tests/Android.bp b/logd/tests/Android.bp
deleted file mode 100644
index 9a5defa..0000000
--- a/logd/tests/Android.bp
+++ /dev/null
@@ -1,68 +0,0 @@
-//
-// Copyright (C) 2014 The Android Open Source Project
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//      http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-
-// -----------------------------------------------------------------------------
-// Unit tests.
-// -----------------------------------------------------------------------------
-
-cc_defaults {
-    name: "logd-unit-test-defaults",
-
-    cflags: [
-        "-fstack-protector-all",
-        "-g",
-        "-Wall",
-        "-Wextra",
-        "-Werror",
-        "-fno-builtin",
-
-        "-DAUDITD_LOG_TAG=1003",
-        "-DCHATTY_LOG_TAG=1004",
-    ],
-
-    srcs: ["logd_test.cpp"],
-
-    static_libs: [
-        "libbase",
-        "libcutils",
-        "libselinux",
-        "liblog",
-    ],
-}
-
-// Build tests for the logger. Run with:
-//   adb shell /data/nativetest/logd-unit-tests/logd-unit-tests
-cc_test {
-    name: "logd-unit-tests",
-    defaults: ["logd-unit-test-defaults"],
-}
-
-cc_test {
-    name: "CtsLogdTestCases",
-    defaults: ["logd-unit-test-defaults"],
-    multilib: {
-        lib32: {
-            suffix: "32",
-        },
-        lib64: {
-            suffix: "64",
-        },
-    },
-    test_suites: [
-        "cts",
-        "vts10",
-    ],
-}