Merge "vdc: use libbase logging and log directly to kmsg on boot" am: 4f6c1ee19f am: e3a8a60024
am: 83f1551ecb

Change-Id: Ib6c575bbfd010e71cac509bee343a7be563e730a
diff --git a/vdc.cpp b/vdc.cpp
index 4eb26cd..7ab5fcc 100644
--- a/vdc.cpp
+++ b/vdc.cpp
@@ -30,6 +30,7 @@
 #include <sys/types.h>
 #include <sys/un.h>
 
+#include <android-base/logging.h>
 #include <android-base/stringprintf.h>
 
 #include <cutils/sockets.h>
@@ -48,6 +49,13 @@
 
     progname = argv[0];
 
+    if (getppid() == 1) {
+        // If init is calling us then it's during boot and we should log to kmsg
+        android::base::InitLogging(argv, &android::base::KernelLogger);
+    } else {
+        android::base::InitLogging(argv, &android::base::StderrLogger);
+    }
+
     wait_for_socket = argc > 1 && strcmp(argv[1], "--wait") == 0;
     if (wait_for_socket) {
         argv++;
@@ -68,7 +76,7 @@
                                  ANDROID_SOCKET_NAMESPACE_RESERVED,
                                  SOCK_STREAM)) < 0) {
         if (!wait_for_socket) {
-            fprintf(stdout, "Error connecting to %s: %s\n", sockname, strerror(errno));
+            PLOG(ERROR) << "Error connecting to " << sockname;
             exit(4);
         } else {
             usleep(10000);
@@ -101,7 +109,7 @@
     }
 
     if (TEMP_FAILURE_RETRY(write(sock, cmd.c_str(), cmd.length() + 1)) < 0) {
-        fprintf(stderr, "Failed to write command: %s\n", strerror(errno));
+        PLOG(ERROR) << "Failed to write command";
         return errno;
     }
 
@@ -113,7 +121,7 @@
     int timeout = kCommandTimeoutMs;
 
     if (stop_after_seq == 0) {
-        fprintf(stderr, "Connected to vold\n");
+        LOG(INFO) << "Connected to vold";
         timeout = -1;
     }
 
@@ -121,25 +129,25 @@
         struct pollfd poll_sock = { sock, POLLIN, 0 };
         int rc = TEMP_FAILURE_RETRY(poll(&poll_sock, 1, timeout));
         if (rc == 0) {
-            fprintf(stderr, "Timeout waiting for %d\n", stop_after_seq);
+            LOG(ERROR) << "Timeout waiting for " << stop_after_seq;
             return ETIMEDOUT;
         } else if (rc < 0) {
-            fprintf(stderr, "Failed during poll: %s\n", strerror(errno));
+            PLOG(ERROR) << "Failed during poll";
             return errno;
         }
 
         if (!(poll_sock.revents & POLLIN)) {
-            fprintf(stderr, "No data; trying again\n");
+            LOG(INFO) << "No data; trying again";
             continue;
         }
 
         memset(buffer, 0, sizeof(buffer));
         rc = TEMP_FAILURE_RETRY(read(sock, buffer, sizeof(buffer)));
         if (rc == 0) {
-            fprintf(stderr, "Lost connection, did vold crash?\n");
+            LOG(ERROR) << "Lost connection, did vold crash?";
             return ECONNRESET;
         } else if (rc < 0) {
-            fprintf(stderr, "Error reading data: %s\n", strerror(errno));
+            PLOG(ERROR) << "Error reading data";
             return errno;
         }
 
@@ -169,6 +177,5 @@
 }
 
 static void usage(char *progname) {
-    fprintf(stderr,
-            "Usage: %s [--wait] <monitor>|<cmd> [arg1] [arg2...]\n", progname);
+    LOG(INFO) << "Usage: " << progname << " [--wait] <monitor>|<cmd> [arg1] [arg2...]";
 }