ART: Rework monitor_android to use liblog helpers

Remove our custom writing. In preparation for different logging.

Bug: 62241642
Test: m
Test: m test-art-host
Test: Device boots
Test: Manual inspection of reports
Change-Id: I033767659da172b2c1aa8584d9a541fe43650f52
diff --git a/runtime/monitor_android.cc b/runtime/monitor_android.cc
index a597852..7ba3652 100644
--- a/runtime/monitor_android.cc
+++ b/runtime/monitor_android.cc
@@ -22,96 +22,79 @@
 #include <sys/types.h>
 
 #include <log/log.h>
+#include <log/log_event_list.h>
 
 #define EVENT_LOG_TAG_dvm_lock_sample 20003
 
 namespace art {
 
-static void Set4LE(uint8_t* buf, uint32_t val) {
-  *buf++ = (uint8_t)(val);
-  *buf++ = (uint8_t)(val >> 8);
-  *buf++ = (uint8_t)(val >> 16);
-  *buf = (uint8_t)(val >> 24);
-}
-
-static char* EventLogWriteInt(char* dst, int value) {
-  *dst++ = EVENT_TYPE_INT;
-  Set4LE(reinterpret_cast<uint8_t*>(dst), value);
-  return dst + 4;
-}
-
-static char* EventLogWriteString(char* dst, const char* value, size_t len) {
-  *dst++ = EVENT_TYPE_STRING;
-  len = len < 32 ? len : 32;
-  Set4LE(reinterpret_cast<uint8_t*>(dst), len);
-  dst += 4;
-  memcpy(dst, value, len);
-  return dst + len;
-}
-
 void Monitor::LogContentionEvent(Thread* self,
                                  uint32_t wait_ms,
                                  uint32_t sample_percent,
                                  ArtMethod* owner_method,
                                  uint32_t owner_dex_pc) {
+  android_log_event_list ctx(EVENT_LOG_TAG_dvm_lock_sample);
+
   const char* owner_filename;
   int32_t owner_line_number;
   TranslateLocation(owner_method, owner_dex_pc, &owner_filename, &owner_line_number);
 
-  // Emit the event list length, 1 byte.
-  char eventBuffer[174];
-  char* cp = eventBuffer;
-  *cp++ = 9;
-
   // Emit the process name, <= 37 bytes.
-  int fd = open("/proc/self/cmdline", O_RDONLY);
-  char procName[33];
-  memset(procName, 0, sizeof(procName));
-  read(fd, procName, sizeof(procName) - 1);
-  close(fd);
-  size_t len = strlen(procName);
-  cp = EventLogWriteString(cp, procName, len);
+  {
+    int fd = open("/proc/self/cmdline", O_RDONLY);
+    char procName[33];
+    memset(procName, 0, sizeof(procName));
+    read(fd, procName, sizeof(procName) - 1);
+    close(fd);
+    ctx << procName;
+  }
 
-  // Emit the sensitive thread ("main thread") status, 5 bytes.
-  cp = EventLogWriteInt(cp, Thread::IsSensitiveThread());
+  // Emit the sensitive thread ("main thread") status. We follow tradition that this corresponds
+  // to a C++ bool's value, but be explicit.
+  constexpr uint32_t kIsSensitive = 1u;
+  constexpr uint32_t kIsNotSensitive = 0u;
+  ctx << (Thread::IsSensitiveThread() ? kIsSensitive : kIsNotSensitive);
 
-  // Emit self thread name string, <= 37 bytes.
-  std::string thread_name;
-  self->GetThreadName(thread_name);
-  cp = EventLogWriteString(cp, thread_name.c_str(), thread_name.size());
+  // Emit self thread name string.
+  {
+    std::string thread_name;
+    self->GetThreadName(thread_name);
+    ctx << thread_name;
+  }
 
-  // Emit the wait time, 5 bytes.
-  cp = EventLogWriteInt(cp, wait_ms);
+  // Emit the wait time.
+  ctx << wait_ms;
 
-  // Emit the source code file name, <= 37 bytes.
-  uint32_t pc;
-  ArtMethod* m = self->GetCurrentMethod(&pc);
-  const char* filename;
-  int32_t line_number;
-  TranslateLocation(m, pc, &filename, &line_number);
-  cp = EventLogWriteString(cp, filename, strlen(filename));
+  const char* filename = nullptr;
+  {
+    uint32_t pc;
+    ArtMethod* m = self->GetCurrentMethod(&pc);
+    int32_t line_number;
+    TranslateLocation(m, pc, &filename, &line_number);
 
-  // Emit the source code line number, 5 bytes.
-  cp = EventLogWriteInt(cp, line_number);
+    // Emit the source code file name.
+    ctx << filename;
 
-  // Emit the lock owner source code file name, <= 37 bytes.
+    // Emit the source code line number.
+    ctx << line_number;
+  }
+
+  // Emit the lock owner source code file name.
   if (owner_filename == nullptr) {
     owner_filename = "";
   } else if (strcmp(filename, owner_filename) == 0) {
     // Common case, so save on log space.
     owner_filename = "-";
   }
-  cp = EventLogWriteString(cp, owner_filename, strlen(owner_filename));
+  ctx << owner_filename;
 
-  // Emit the source code line number, 5 bytes.
-  cp = EventLogWriteInt(cp, owner_line_number);
+  // Emit the source code line number.
+  ctx << owner_line_number;
 
-  // Emit the sample percentage, 5 bytes.
-  cp = EventLogWriteInt(cp, sample_percent);
+  // Emit the sample percentage.
+  ctx << sample_percent;
 
-  CHECK_LE((size_t)(cp - eventBuffer), sizeof(eventBuffer));
-  android_btWriteLog(EVENT_LOG_TAG_dvm_lock_sample, EVENT_TYPE_LIST, eventBuffer,
-                     (size_t)(cp - eventBuffer));
+  ctx << LOG_ID_EVENTS;
 }
 
 }  // namespace art