android/log: allow selecting log buffer with '-b'

Instead of unconditionally logging on the main buffer, allow choosing
the buffer where the log should be sent. The default Android buffers
are supported.
diff --git a/lib/portability.h b/lib/portability.h
index 324ddc8..55e3ab2 100644
--- a/lib/portability.h
+++ b/lib/portability.h
@@ -302,15 +302,26 @@
   ANDROID_LOG_FATAL,
   ANDROID_LOG_SILENT,
 } android_LogPriority;
+typedef enum log_id {
+  LOG_ID_MAIN = 0,
+  LOG_ID_RADIO = 1,
+  LOG_ID_EVENTS = 2,
+  LOG_ID_SYSTEM = 3,
+  LOG_ID_CRASH = 4,
+  LOG_ID_STATS = 5,
+  LOG_ID_SECURITY = 6,
+  LOG_ID_KERNEL = 7,
+} log_id_t;
 #endif
 #if !defined(__BIONIC__) || defined(__ANDROID_NDK__)
 // Android NDKv18 has liblog.so but not liblog.a for static builds.
-static inline int stub_out_log_write(int pri, const char *tag, const char *msg)
+static inline int stub_out_log_buf_write(int log_id, int pri, const char *tag,
+  const char *msg)
 {
   return -1;
 }
 #ifdef __ANDROID_NDK__
-#define __android_log_write(a, b, c) stub_out_log_write(a, b, c)
+#define __android_log_buf_write(a, b, c, d) stub_out_log_buf_write(a, b, c, d)
 #endif
 
 #endif
diff --git a/toys/android/log.c b/toys/android/log.c
index 005f714..5c4dcd9 100644
--- a/toys/android/log.c
+++ b/toys/android/log.c
@@ -2,17 +2,19 @@
  *
  * Copyright 2016 The Android Open Source Project
 
-USE_LOG(NEWTOY(log, "p:t:", TOYFLAG_USR|TOYFLAG_SBIN))
+USE_LOG(NEWTOY(log, "b:p:t:", TOYFLAG_USR|TOYFLAG_SBIN))
 
 config LOG
   bool "log"
   depends on TOYBOX_ON_ANDROID
   default y
   help
-    usage: log [-p PRI] [-t TAG] [MESSAGE...]
+    usage: log [-b BUFFER] [-p PRI] [-t TAG] [MESSAGE...]
 
     Logs message (or stdin) to logcat.
 
+    -b	Use the given log buffer instead of "main":
+    	"radio", "events", "system", "crash", "stats", "security", "kernel"
     -p	Use the given priority instead of INFO:
     	d: DEBUG  e: ERROR  f: FATAL  i: INFO  v: VERBOSE  w: WARN  s: SILENT
     -t	Use the given tag instead of "log"
@@ -22,15 +24,15 @@
 #include "toys.h"
 
 GLOBALS(
-  char *t, *p;
+  char *t, *p, *b;
 
-  int pri;
+  int pri, buf;
 )
 
 static void log_line(char **pline, long len)
 {
   if (!pline) return;
-  __android_log_write(TT.pri, TT.t, *pline);
+  __android_log_buf_write(TT.buf, TT.pri, TT.t, *pline);
 }
 
 void log_main(void)
@@ -47,6 +49,15 @@
       ANDROID_LOG_VERBOSE, ANDROID_LOG_WARN}[i];
   }
   if (!TT.t) TT.t = "log";
+  if (!TT.b || !strcmp(TT.b, "main")) TT.buf = LOG_ID_MAIN;
+  else if (!strcmp(TT.b, "radio")) TT.buf = LOG_ID_RADIO;
+  else if (!strcmp(TT.b, "events")) TT.buf = LOG_ID_EVENTS;
+  else if (!strcmp(TT.b, "system")) TT.buf = LOG_ID_SYSTEM;
+  else if (!strcmp(TT.b, "crash")) TT.buf = LOG_ID_CRASH;
+  else if (!strcmp(TT.b, "stats")) TT.buf = LOG_ID_STATS;
+  else if (!strcmp(TT.b, "security")) TT.buf = LOG_ID_SECURITY;
+  else if (!strcmp(TT.b, "kernel")) TT.buf = LOG_ID_KERNEL;
+  else error_exit("unknown log buffer: %s", TT.b);
 
   if (toys.optc) {
     for (i = 0; toys.optargs[i]; i++) {
@@ -61,5 +72,5 @@
     }
   } else do_lines(0, '\n', log_line);
 
-  __android_log_write(TT.pri, TT.t, toybuf);
+  __android_log_buf_write(TT.buf, TT.pri, TT.t, toybuf);
 }