logd: build liblogd and its test on host

Plus the various fixups needed for building on host.

Test: run these tests on host
Change-Id: I85e6c989068f80c5a80eaf5ad149fdad0a045c08
diff --git a/logd/Android.bp b/logd/Android.bp
index 32bfefe..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",
@@ -148,6 +148,7 @@
 //   adb shell /data/nativetest/logd-unit-tests/logd-unit-tests
 cc_test {
     name: "logd-unit-tests",
+    host_supported: true,
     defaults: ["logd-unit-test-defaults"],
 }
 
diff --git a/logd/ChattyLogBuffer.cpp b/logd/ChattyLogBuffer.cpp
index 1196c83..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
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
index 806fd16..cc3cb76 100644
--- a/logd/LogBufferTest.cpp
+++ b/logd/LogBufferTest.cpp
@@ -36,6 +36,16 @@
 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);
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);