OSDN Git Service

vdc: use libbase logging and log directly to kmsg on boot
authorTom Cherry <tomcherry@google.com>
Wed, 29 Mar 2017 23:50:28 +0000 (16:50 -0700)
committerTom Cherry <tomcherry@google.com>
Thu, 30 Mar 2017 00:06:31 +0000 (17:06 -0700)
Init is no longer calling vdc with logwrapper, so it must take care of
logging to kmsg directly.

Test: observe logging in kmsg on boot and stderr on normal usage
Change-Id: Ie3e59da433bd154f121ce103dea0c59eb0bab069

vdc.cpp

diff --git a/vdc.cpp b/vdc.cpp
index 4eb26cd..7ab5fcc 100644 (file)
--- 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 @@ int main(int argc, char **argv) {
 
     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 @@ int main(int argc, char **argv) {
                                  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 @@ static int do_cmd(int sock, int argc, char **argv) {
     }
 
     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 @@ static int do_monitor(int sock, int stop_after_seq) {
     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 @@ static int do_monitor(int sock, int stop_after_seq) {
         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 int do_monitor(int sock, int stop_after_seq) {
 }
 
 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...]";
 }