diff options
author | Mike Frysinger <vapier@chromium.org> | 2017-08-15 23:49:40 -0400 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2017-08-17 11:31:50 -0700 |
commit | a1a7e930ebd75e82668f7868fa7cb9fdcb2a043d (patch) | |
tree | 4ec9d280ac3cf16034fd45ae8fe7ee5e6005ed9d | |
parent | 9f21d7d47a8af692ccf19714ca59af873003434a (diff) | |
download | platform_external_libbrillo-a1a7e930ebd75e82668f7868fa7cb9fdcb2a043d.tar.gz platform_external_libbrillo-a1a7e930ebd75e82668f7868fa7cb9fdcb2a043d.tar.bz2 platform_external_libbrillo-a1a7e930ebd75e82668f7868fa7cb9fdcb2a043d.zip |
syslog: new option to log in interactive sessions
Developers often want to run programs that log to stderr when they run
them by hand, but use syslog when run as a daemon. Add a new flag that
turns on stderr logging only when stdin is a tty.
BUG=None
TEST=unittests pass
Change-Id: I8086a4613099c873800188415b97cc7b241ab847
Reviewed-on: https://chromium-review.googlesource.com/616449
Commit-Ready: Mike Frysinger <vapier@chromium.org>
Tested-by: Mike Frysinger <vapier@chromium.org>
Reviewed-by: Dan Erat <derat@chromium.org>
-rw-r--r-- | brillo/syslog_logging.cc | 3 | ||||
-rw-r--r-- | brillo/syslog_logging.h | 5 |
2 files changed, 8 insertions, 0 deletions
diff --git a/brillo/syslog_logging.cc b/brillo/syslog_logging.cc index 40b064a..2b35280 100644 --- a/brillo/syslog_logging.cc +++ b/brillo/syslog_logging.cc @@ -5,6 +5,7 @@ #include "brillo/syslog_logging.h" #include <syslog.h> +#include <unistd.h> #include <string> @@ -78,6 +79,8 @@ namespace brillo { void SetLogFlags(int log_flags) { s_log_to_syslog = (log_flags & kLogToSyslog) != 0; s_log_to_stderr = (log_flags & kLogToStderr) != 0; + if ((log_flags & kLogToStderrIfTty) && isatty(0)) + s_log_to_stderr = true; s_log_header = (log_flags & kLogHeader) != 0; } int GetLogFlags() { diff --git a/brillo/syslog_logging.h b/brillo/syslog_logging.h index b054259..04b3253 100644 --- a/brillo/syslog_logging.h +++ b/brillo/syslog_logging.h @@ -12,9 +12,14 @@ namespace brillo { enum InitFlags { + // Always log to syslog. kLogToSyslog = 1, + // Always log to stderr. kLogToStderr = 2, + // Include message header in log lines. kLogHeader = 4, + // Log to stderr if stdin is a tty (e.g. command line). + kLogToStderrIfTty = 8, }; // Initialize logging subsystem. |init_flags| is a bitfield, with bits defined |