Add functions for testability to the EventLog APIs

Test: bit statsd_test && adb push out/target/product/sailfish/testcases/statsd_test/arm/statsd_test /data/statsd_test && adb shell /data/statsd_test
Change-Id: I34ef25b174e9660ee328d5eddbac631c5c7caf62
diff --git a/liblog/include/log/log_event_list.h b/liblog/include/log/log_event_list.h
index bb1ce34..4d24c68 100644
--- a/liblog/include/log/log_event_list.h
+++ b/liblog/include/log/log_event_list.h
@@ -108,6 +108,12 @@
 android_log_list_element android_log_read_next(android_log_context ctx);
 android_log_list_element android_log_peek_next(android_log_context ctx);
 
+/**
+ * Convert a writer context to a reader context. Useful for testing.
+ * Returns an error if ctx is already a reader.
+ */
+int android_log_writer_to_reader(android_log_context ctx);
+
 /* Finished with reader or writer context */
 int android_log_destroy(android_log_context* ctx);
 
@@ -122,6 +128,7 @@
  private:
   android_log_context ctx;
   int ret;
+  int tag_;
 
   android_log_event_list(const android_log_event_list&) = delete;
   void operator=(const android_log_event_list&) = delete;
@@ -129,11 +136,16 @@
  public:
   explicit android_log_event_list(int tag) : ret(0) {
     ctx = create_android_logger(static_cast<uint32_t>(tag));
+    tag_ = tag;
   }
+
   explicit android_log_event_list(log_msg& log_msg) : ret(0) {
-    ctx = create_android_log_parser(log_msg.msg() + sizeof(uint32_t),
+    const char* buf = log_msg.msg();
+    ctx = create_android_log_parser(buf + sizeof(uint32_t),
                                     log_msg.entry.len - sizeof(uint32_t));
+    tag_ = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
   }
+
   ~android_log_event_list() {
     android_log_destroy(&ctx);
   }
@@ -149,6 +161,10 @@
     return ctx;
   }
 
+  android_log_context context() const {
+    return ctx;
+  }
+
   /* return errors or transmit status */
   int status() const {
     return ret;
@@ -159,12 +175,17 @@
     if (retval < 0) ret = retval;
     return ret;
   }
+
   int end() {
     int retval = android_log_write_list_end(ctx);
     if (retval < 0) ret = retval;
     return ret;
   }
 
+  uint32_t tag() {
+    return tag_;
+  }
+
   android_log_event_list& operator<<(int32_t value) {
     int retval = android_log_write_int32(ctx, value);
     if (retval < 0) ret = retval;
@@ -296,6 +317,10 @@
     return ret >= 0;
   }
 
+  int convert_to_reader() {
+    return android_log_writer_to_reader(ctx);
+  }
+
   android_log_list_element read() {
     return android_log_read_next(ctx);
   }
diff --git a/liblog/log_event_list.c b/liblog/log_event_list.c
index a59cb87..f6e13db 100644
--- a/liblog/log_event_list.c
+++ b/liblog/log_event_list.c
@@ -565,3 +565,26 @@
 android_log_peek_next(android_log_context ctx) {
   return android_log_read_next_internal(ctx, 1);
 }
+
+LIBLOG_ABI_PUBLIC int android_log_writer_to_reader(android_log_context ctx) {
+  android_log_context_internal* context;
+
+  context = (android_log_context_internal*)ctx;
+
+  if (!context || context->read_write_flag != kAndroidLoggerWrite) {
+    return -EBADF;
+  }
+
+  context->len = context->pos;
+  context->storage[1] =
+      context
+          ->count[0];  // What does this do?!?! It's copied from the write func
+  context->pos = 0;
+  memset(context->count, 0, sizeof(context->count));
+  memset(context->list, 0, sizeof(context->list));
+  context->list_nest_depth = 0;
+  context->read_write_flag = kAndroidLoggerRead;
+  context->list_stop = false;
+
+  return 0;
+}