Log to daemon on AoC

In combination with printf which logs via AoC and outputs through the
debug UART, use the daemon log path so logs shown up in ADB (only after
the daemon has connected to AoC).

Bug: 152442881
Test: Load onto device w/ capable daemon

Change-Id: If4b1204f5396a9a61eed9dab49df47437a6478dd
diff --git a/core/include/chre/core/host_comms_manager.h b/core/include/chre/core/host_comms_manager.h
index 22956a0..4a2ebcc 100644
--- a/core/include/chre/core/host_comms_manager.h
+++ b/core/include/chre/core/host_comms_manager.h
@@ -175,6 +175,14 @@
    */
   void onMessageToHostComplete(const MessageToHost *msgToHost);
 
+  /**
+   * @return A reference to the platform's host link implementation giving the
+   *         ability to communicate to the host AP.
+   */
+  HostLink &getHostLink() {
+    return mHostLink;
+  }
+
  private:
   //! The maximum number of messages we can have outstanding at any given time
   static constexpr size_t kMaxOutstandingMessages = 32;
diff --git a/platform/aoc/chre_api_re.cc b/platform/aoc/chre_api_re.cc
index 69aa9b1..cc36487 100644
--- a/platform/aoc/chre_api_re.cc
+++ b/platform/aoc/chre_api_re.cc
@@ -19,34 +19,9 @@
 #include "chre/util/macros.h"
 #include "chre_api/chre/re.h"
 
-// The log buffer size is limited by the frame size defined in :
-// {AOC_TOP}/build/{THIS_PLATFORM}/toolchain.mk
-// There is no limit for the linux simulator, 512 bytes for QEMU
-// and 280 bytes for a32, so we pick the maximum common size (i..e. the
-// minimum of the allowable frame sizes), with a small allowance for the
-// size of the function itself
-static constexpr size_t kChreLogBufferSize = 240;
-
 void chreLog(enum chreLogLevel level, const char *formatStr, ...) {
-  char logBuf[kChreLogBufferSize];
   va_list args;
-
   va_start(args, formatStr);
-  vsnprintf(logBuf, sizeof(logBuf), formatStr, args);
+  chre::vaLog(level, formatStr, args);
   va_end(args);
-
-  switch (level) {
-    case CHRE_LOG_ERROR:
-      LOGE("%s", logBuf);
-      break;
-    case CHRE_LOG_WARN:
-      LOGW("%s", logBuf);
-      break;
-    case CHRE_LOG_INFO:
-      LOGI("%s", logBuf);
-      break;
-    case CHRE_LOG_DEBUG:
-    default:
-      LOGD("%s", logBuf);
-  }
 }
diff --git a/platform/aoc/host_link.cc b/platform/aoc/host_link.cc
index 42128dc..232d395 100644
--- a/platform/aoc/host_link.cc
+++ b/platform/aoc/host_link.cc
@@ -296,6 +296,14 @@
   return transportMgr.send(builder.GetBufferPointer(), builder.GetSize());
 }
 
+void HostLink::sendLogMessage(const char *logMessage, size_t logMessageSize) {
+  constexpr size_t kInitialSize = 128;
+  flatbuffers::FlatBufferBuilder builder(logMessageSize + kInitialSize);
+  HostProtocolChre::encodeLogMessages(builder, logMessage, logMessageSize);
+
+  transportMgr.send(builder.GetBufferPointer(), builder.GetSize());
+}
+
 void HostMessageHandlers::handleDebugDumpRequest(uint16_t hostClientId) {
   chre::EventLoopManagerSingleton::get()
       ->getDebugDumpManager()
diff --git a/platform/aoc/include/chre/target_platform/log.h b/platform/aoc/include/chre/target_platform/log.h
index a0d186e..2d5ca0f 100644
--- a/platform/aoc/include/chre/target_platform/log.h
+++ b/platform/aoc/include/chre/target_platform/log.h
@@ -16,6 +16,9 @@
 #ifndef CHRE_PLATFORM_AOC_LOG_H_
 #define CHRE_PLATFORM_AOC_LOG_H_
 
+#include <chre.h>
+#include <stdio.h>
+
 #ifndef __FILENAME__
 #ifdef __BASE_FILE__
 #define __FILENAME__ __BASE_FILE__
@@ -24,16 +27,26 @@
 #endif  // __BASE_FILE__
 #endif  // __FILE_NAME__
 
-#include <stdio.h>
+// TODO(b/149317051): The printf in the below macro is needed until CHRE can log
+// to the AP before the daemon has connected to AoC.
+#define CHRE_AOC_LOG(level, fmt, ...)                                       \
+  do {                                                                      \
+    CHRE_LOG_PREAMBLE                                                       \
+    chre::log(level, fmt, ##__VA_ARGS__);                                   \
+    printf("CHRE:%s:%d\t" fmt "\n", __FILENAME__, __LINE__, ##__VA_ARGS__); \
+    CHRE_LOG_EPILOGUE                                                       \
+  } while (0)
 
-// TODO: b/146164384 - We will need to batch logs rather than send them
-// one at a time to avoid waking the AP.
-#define CHRE_AOC_LOG(level, fmt, ...)                               \
-  printf("CHRE:%s %s:%d\t" fmt "\n", level, __FILENAME__, __LINE__, \
-         ##__VA_ARGS__)
-#define LOGE(fmt, ...) CHRE_AOC_LOG("E", fmt, ##__VA_ARGS__)
-#define LOGW(fmt, ...) CHRE_AOC_LOG("W", fmt, ##__VA_ARGS__)
-#define LOGI(fmt, ...) CHRE_AOC_LOG("I", fmt, ##__VA_ARGS__)
-#define LOGD(fmt, ...) CHRE_AOC_LOG("D", fmt, ##__VA_ARGS__)
+#define LOGE(fmt, ...) CHRE_AOC_LOG(CHRE_LOG_ERROR, fmt, ##__VA_ARGS__)
+#define LOGW(fmt, ...) CHRE_AOC_LOG(CHRE_LOG_WARN, fmt, ##__VA_ARGS__)
+#define LOGI(fmt, ...) CHRE_AOC_LOG(CHRE_LOG_INFO, fmt, ##__VA_ARGS__)
+#define LOGD(fmt, ...) CHRE_AOC_LOG(CHRE_LOG_DEBUG, fmt, ##__VA_ARGS__)
+
+namespace chre {
+
+void log(enum chreLogLevel level, const char *formatStr, ...);
+void vaLog(enum chreLogLevel level, const char *format, va_list args);
+
+}  // namespace chre
 
 #endif  // CHRE_PLATFORM_AOC_LOG_H_
diff --git a/platform/aoc/log.cc b/platform/aoc/log.cc
index ed8aeca..8a6a5e5 100644
--- a/platform/aoc/log.cc
+++ b/platform/aoc/log.cc
@@ -23,10 +23,8 @@
 
 namespace chre {
 namespace {
-
-static constexpr size_t kChreLogBufferSize = CHRE_MESSAGE_TO_HOST_MAX_SIZE;
-static char logBuffer[kChreLogBufferSize];
-
+constexpr size_t kChreLogBufferSize = CHRE_MESSAGE_TO_HOST_MAX_SIZE;
+char logBuffer[kChreLogBufferSize];
 }  // namespace
 
 void log(enum chreLogLevel level, const char *formatStr, ...) {
@@ -39,7 +37,8 @@
 // TODO: b/146164384 - We will need to batch logs rather than send them
 // one at a time to avoid waking the AP.
 void vaLog(enum chreLogLevel level, const char *format, va_list args) {
-  HostLink &hostLink = EventLoopManagerSingleton::get()->getHostLink();
+  HostLink &hostLink =
+      EventLoopManagerSingleton::get()->getHostCommsManager().getHostLink();
 
   // See host_messages.fbs for the log message format.
   size_t logBufIndex = 0;
diff --git a/platform/platform.mk b/platform/platform.mk
index 2b29503..910b682 100644
--- a/platform/platform.mk
+++ b/platform/platform.mk
@@ -166,6 +166,7 @@
 AOC_SRCS += platform/aoc/chre_api_re.cc
 AOC_SRCS += platform/aoc/fatal_error.cc
 AOC_SRCS += platform/aoc/host_link.cc
+AOC_SRCS += platform/aoc/log.cc
 AOC_SRCS += platform/aoc/platform_sensor_type_helpers.cc
 AOC_SRCS += platform/aoc/power_control_manager.cc
 AOC_SRCS += platform/aoc/system_time.cc