entd: Factor out syslog_logger code and start using it

Change-Id: I4a136cbe89605460e4c355c7a2009c3cef704af8

BUG=none
TEST=Fire up entd and verify it logs to /var/log/messages

Review URL: http://codereview.chromium.org/6594001
diff --git a/main.cc b/main.cc
index 74dbd96..14c70a9 100644
--- a/main.cc
+++ b/main.cc
@@ -3,29 +3,12 @@
 // found in the LICENSE file.
 
 #include <stdio.h>
-#include <syslog.h>
-
-// syslog.h and base/logging.h both try to #define LOG_INFO and LOG_WARNING.
-// We need to #undef at least these two before including base/logging.h.  The
-// others are included to be consistent.
-namespace {
-const int kSyslogInfo     = LOG_INFO;
-const int kSyslogWarning  = LOG_WARNING;
-const int kSyslogError    = LOG_ERR;
-const int kSyslogCritical = LOG_CRIT;
-
-#undef LOG_INFO
-#undef LOG_WARNING
-#undef LOG_ERR
-#undef LOG_INFO
-}  // namespace
-
 #include <iostream>
 #include <string>
 
 #include <base/command_line.h>
 #include <base/file_util.h>
-#include <base/logging.h>
+#include <chromeos/syslog_logging.h>
 
 #include "entd/entd.h"
 #include "entd/extensions.h"
@@ -75,41 +58,6 @@
 
 }  // namespace switches
 
-bool handle_message(int severity, const std::string &message) {
-  switch (severity) {
-    case logging::LOG_INFO:
-      severity = kSyslogInfo;
-      break;
-
-    case logging::LOG_WARNING:
-      severity = kSyslogWarning;
-      break;
-
-    case logging::LOG_ERROR:
-    case logging::LOG_ERROR_REPORT:
-      severity = kSyslogError;
-      break;
-
-    case logging::LOG_FATAL:
-      severity = kSyslogCritical;
-      break;
-  }
-
-  // The first "] " should be the end of the header added by the logging
-  // code.  The meat of the message is two characters after that.
-  size_t pos = message.find("] ");
-  if (pos != std::string::npos && message.length() > pos + 2) {
-    pos += 2;
-  } else {
-    pos = 0;
-  }
-
-  const char* str = message.c_str() + pos;
-
-  syslog(severity, "%s", str);
-  return false;
-}
-
 // Return values:
 //   0: Entd completed successfully and should not be restarted.
 //   1: Entd encountered a failure, but will probably fail again if restarted,
@@ -124,18 +72,17 @@
 
   CommandLine::Init(argc, argv);
   CommandLine* cl = CommandLine::ForCurrentProcess();
-
-  logging::InitLogging("/dev/null", logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG,
-                       logging::DONT_LOCK_LOG_FILE,
-                       logging::APPEND_TO_OLD_LOG_FILE);
+  int log_flags = chromeos::kLogToStderr;
 
   if (cl->HasSwitch(switches::kEnableSyslog) ||
       (!isatty(STDOUT_FILENO) && !cl->HasSwitch(switches::kDisableSyslog))) {
     // If syslog was explicitly enabled, or stdout is not a tty and syslog
     // was not explicitly disabled, then send all LOG(...) messages to syslog.
-    logging::SetLogMessageHandler(handle_message);
+    log_flags |= chromeos::kLogToSyslog;
   }
 
+  chromeos::InitLog(log_flags);
+
   LOG(INFO) << "Starting entd";
 
   std::string base_extension_path =