metricsd: Fix logging initialization.
metricsd should only log to a single location (stderr or syslog):
* standard error if --logtostderr is set or metricsd runs in the
foreground.
* syslog if --logtosyslog is set or metricsd is daemonized.
Bug: 25472752
Change-Id: Idb918714e1b6975d79682f22495a7ca708d4d97d
diff --git a/metricsd/metrics_daemon.rc b/metricsd/metrics_daemon.rc
index 0ee577e..8b24749 100644
--- a/metricsd/metrics_daemon.rc
+++ b/metricsd/metrics_daemon.rc
@@ -1,7 +1,7 @@
on post-fs-data
mkdir /data/misc/metrics 0770 system system
-service metrics_daemon /system/bin/metrics_daemon --uploader -nodaemon
+service metrics_daemon /system/bin/metrics_daemon --uploader --foreground --logtosyslog
class late_start
user system
group system dbus inet
diff --git a/metricsd/metrics_daemon_main.cc b/metricsd/metrics_daemon_main.cc
index 50c279d..8573f68 100644
--- a/metricsd/metrics_daemon_main.cc
+++ b/metricsd/metrics_daemon_main.cc
@@ -53,7 +53,7 @@
}
int main(int argc, char** argv) {
- DEFINE_bool(daemon, true, "run as daemon (use -nodaemon for debugging)");
+ DEFINE_bool(foreground, false, "Don't daemonize");
// The uploader is disabled by default on ChromeOS as Chrome is responsible
// for sending the metrics.
@@ -79,13 +79,28 @@
metrics::kMetricsDirectory,
"Root of the configuration files (testing only)");
+ DEFINE_bool(logtostderr, false, "Log to standard error");
+ DEFINE_bool(logtosyslog, false, "Log to syslog");
+
brillo::FlagHelper::Init(argc, argv, "Chromium OS Metrics Daemon");
- // Also log to stderr when not running as daemon.
- brillo::InitLog(brillo::kLogToSyslog | brillo::kLogHeader |
- (FLAGS_daemon ? 0 : brillo::kLogToStderr));
+ int logging_location = (FLAGS_foreground ? brillo::kLogToStderr
+ : brillo::kLogToSyslog);
+ if (FLAGS_logtosyslog)
+ logging_location = brillo::kLogToSyslog;
- if (FLAGS_daemon && daemon(0, 0) != 0) {
+ if (FLAGS_logtostderr)
+ logging_location = brillo::kLogToStderr;
+
+ // Also log to stderr when not running as daemon.
+ brillo::InitLog(logging_location | brillo::kLogHeader);
+
+ if (FLAGS_logtostderr && FLAGS_logtosyslog) {
+ LOG(ERROR) << "only one of --logtosyslog and --logtostderr can be set";
+ return 1;
+ }
+
+ if (!FLAGS_foreground && daemon(0, 0) != 0) {
return errno;
}