logd: create private/android_logger.h

- create a structure to depict the private header
  expected at logd end of socket.
- utilize this new structure instead of unscalable
  byte stream technique used to unpack in logd.

Change-Id: I2d0e5c3531c279f2dc1fbd74807210ff8d804de0
diff --git a/include/private/android_logger.h b/include/private/android_logger.h
new file mode 100644
index 0000000..54aa86b
--- /dev/null
+++ b/include/private/android_logger.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2015 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.
+ */
+
+/* This file is used to define the internal protocol for the Android Logger */
+
+#ifndef _SYSTEM_CORE_INCLUDE_PRIVATE_ANDROID_LOGGER_H_
+#define _SYSTEM_CORE_INCLUDE_PRIVATE_ANDROID_LOGGER_H_
+
+#include <stdint.h>
+
+#include <log/log.h>
+#include <log/log_read.h>
+
+/* Header Structure to logd */
+typedef struct __attribute__((__packed__)) {
+    typeof_log_id_t id;
+    uint16_t tid;
+    log_time realtime;
+} android_log_header_t;
+
+#endif
diff --git a/logd/LogListener.cpp b/logd/LogListener.cpp
index 8186cea..fc9e30f 100644
--- a/logd/LogListener.cpp
+++ b/logd/LogListener.cpp
@@ -23,6 +23,7 @@
 
 #include <cutils/sockets.h>
 #include <log/logger.h>
+#include <private/android_logger.h>
 
 #include "LogListener.h"
 
@@ -54,7 +55,7 @@
     int socket = cli->getSocket();
 
     ssize_t n = recvmsg(socket, &hdr, 0);
-    if (n <= (ssize_t)(sizeof_log_id_t + sizeof(uint16_t) + sizeof(log_time))) {
+    if (n <= (ssize_t)(sizeof(android_log_header_t))) {
         return false;
     }
 
@@ -81,28 +82,19 @@
         return false;
     }
 
-    // First log element is always log_id.
-    log_id_t log_id = (log_id_t) *((typeof_log_id_t *) buffer);
-    if (log_id < 0 || log_id >= LOG_ID_MAX) {
+    android_log_header_t *header = reinterpret_cast<android_log_header_t *>(buffer);
+    if (/* header->id < LOG_ID_MIN || */ header->id >= LOG_ID_MAX) {
         return false;
     }
-    char *msg = ((char *)buffer) + sizeof_log_id_t;
-    n -= sizeof_log_id_t;
 
-    // second element is the thread id of the caller
-    pid_t tid = (pid_t) *((uint16_t *) msg);
-    msg += sizeof(uint16_t);
-    n -= sizeof(uint16_t);
-
-    // third element is the realtime at point of caller
-    log_time realtime(msg);
-    msg += sizeof(log_time);
-    n -= sizeof(log_time);
+    char *msg = ((char *)buffer) + sizeof(android_log_header_t);
+    n -= sizeof(android_log_header_t);
 
     // NB: hdr.msg_flags & MSG_TRUNC is not tested, silently passing a
     // truncated message to the logs.
 
-    logbuf->log(log_id, realtime, cred->uid, cred->pid, tid, msg,
+    logbuf->log((log_id_t)header->id, header->realtime,
+        cred->uid, cred->pid, header->tid, msg,
         ((size_t) n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
     reader->notifyNewLog();