Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0a1119f0 authored by Bertrand SIMONNET's avatar Bertrand SIMONNET
Browse files

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
parent 3c540aec
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
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
+19 −4
Original line number Diff line number Diff line
@@ -53,7 +53,7 @@ const std::string MetricsMainDiskStatsPath() {
}

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 @@ int main(int argc, char** argv) {
                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");

  int logging_location = (FLAGS_foreground ? brillo::kLogToStderr
                          : brillo::kLogToSyslog);
  if (FLAGS_logtosyslog)
    logging_location = brillo::kLogToSyslog;

  if (FLAGS_logtostderr)
    logging_location = brillo::kLogToStderr;

  // Also log to stderr when not running as daemon.
  brillo::InitLog(brillo::kLogToSyslog | brillo::kLogHeader |
                  (FLAGS_daemon ? 0 : brillo::kLogToStderr));
  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_daemon && daemon(0, 0) != 0) {
  if (!FLAGS_foreground && daemon(0, 0) != 0) {
    return errno;
  }