]> err.no Git - systemd/commitdiff
log: add automatic log target
authorLennart Poettering <lennart@poettering.net>
Fri, 12 Nov 2010 00:01:04 +0000 (01:01 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 12 Nov 2010 00:01:04 +0000 (01:01 +0100)
src/cryptsetup.c
src/log.c
src/log.h

index 7d5d3db49b9bb766f487c7ec6e87c3321325c655..d9b9ebe69765697be205bf0396eb6aa879f7bd5c 100644 (file)
@@ -154,7 +154,7 @@ int main(int argc, char *argv[]) {
                 return EXIT_FAILURE;
         }
 
-        log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
+        log_set_target(LOG_TARGET_AUTO);
         log_parse_environment();
         log_open();
 
index cc636a2dfac16e7fab8b1206717d3c1eb00e1a14..3e7b8e93b7ec4ad32a8934f09d2087fc9e9e895b 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -196,20 +196,26 @@ int log_open(void) {
                 return 0;
         }
 
-        if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
-            log_target == LOG_TARGET_SYSLOG)
-                if ((r = log_open_syslog()) >= 0) {
-                        log_close_console();
-                        return r;
-                }
-
-        if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
-            log_target == LOG_TARGET_KMSG)
-                if ((r = log_open_kmsg()) >= 0) {
-                        log_close_syslog();
-                        log_close_console();
-                        return r;
-                }
+        if (log_target != LOG_TARGET_AUTO ||
+            getpid() == 1 ||
+            isatty(STDERR_FILENO) <= 0)) {
+
+                if (log_target == LOG_TARGET_AUTO ||
+                    log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
+                    log_target == LOG_TARGET_SYSLOG)
+                        if ((r = log_open_syslog()) >= 0) {
+                                log_close_console();
+                                return r;
+                        }
+                if (log_target == LOG_TARGET_AUTO ||
+                    log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
+                    log_target == LOG_TARGET_KMSG)
+                        if ((r = log_open_kmsg()) >= 0) {
+                                log_close_syslog();
+                                log_close_console();
+                                return r;
+                        }
+        }
 
         log_close_syslog();
 
@@ -383,7 +389,8 @@ static int log_dispatch(
                 if ((e = strpbrk(buffer, NEWLINE)))
                         *(e++) = 0;
 
-                if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
+                if (log_target == LOG_TARGET_AUTO ||
+                    log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
                     log_target == LOG_TARGET_SYSLOG) {
 
                         if ((k = write_to_syslog(level, file, line, func, buffer)) < 0) {
@@ -394,7 +401,8 @@ static int log_dispatch(
                 }
 
                 if (k <= 0 &&
-                    (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
+                    (log_target == LOG_TARGET_AUTO ||
+                     log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
                      log_target == LOG_TARGET_KMSG)) {
 
                         if ((k = write_to_kmsg(level, file, line, func, buffer)) < 0) {
@@ -568,7 +576,8 @@ static const char *const log_target_table[] = {
         [LOG_TARGET_SYSLOG] = "syslog",
         [LOG_TARGET_KMSG] = "kmsg",
         [LOG_TARGET_SYSLOG_OR_KMSG] = "syslog-or-kmsg",
-        [LOG_TARGET_NULL] = "null"
+        [LOG_TARGET_NULL] = "null",
+        [LOG_TARGET_AUTO] = "auto"
 };
 
 DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget);
index b052bc3c5101005cbd2f77c1a2134c0389494d69..303b0d66a35a6dfa437e5290abcb6fdd6e0adae2 100644 (file)
--- a/src/log.h
+++ b/src/log.h
@@ -34,6 +34,7 @@ typedef enum LogTarget{
         LOG_TARGET_KMSG,
         LOG_TARGET_SYSLOG,
         LOG_TARGET_SYSLOG_OR_KMSG,
+        LOG_TARGET_AUTO, /* console if stderr is tty, SYSLOG_OR_KMSG otherwise */
         LOG_TARGET_NULL,
         _LOG_TARGET_MAX,
         _LOG_TARGET_INVALID = -1