chiark / gitweb /
manager: make sure we free the dbus error only if we actually use one
[elogind.git] / src / log.c
index 265a7f10e28e2ee1d624b899c69320a2c768a897..21fc9f4fa091f9c40265d2f37d8d47a4cd0a80df 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -1,4 +1,4 @@
-/*-*- Mode: C; c-basic-offset: 8 -*-*/
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
 
 /***
   This file is part of systemd.
 #include "macro.h"
 
 #define SYSLOG_TIMEOUT_USEC (5*USEC_PER_SEC)
-#define LOG_BUFFER_MAX 1024
 
 static LogTarget log_target = LOG_TARGET_CONSOLE;
-static int log_max_level = LOG_DEBUG;
+static int log_max_level = LOG_INFO;
 
 static int console_fd = STDERR_FILENO;
 static int syslog_fd = -1;
 static int kmsg_fd = -1;
 
+static bool show_color = false;
+static bool show_location = false;
+
 /* Akin to glibc's __abort_msg; which is private and we hance cannot
  * use here. */
 static char *log_abort_msg = NULL;
@@ -70,7 +72,7 @@ static int log_open_console(void) {
                         return console_fd;
                 }
 
-                log_info("Succesfully opened /dev/console for logging.");
+                log_debug("Succesfully opened /dev/console for logging.");
         } else
                 console_fd = STDERR_FILENO;
 
@@ -96,7 +98,7 @@ static int log_open_kmsg(void) {
                 return -errno;
         }
 
-        log_info("Succesfully opened /dev/kmsg for logging.");
+        log_debug("Succesfully opened /dev/kmsg for logging.");
 
         return 0;
 }
@@ -143,7 +145,7 @@ static int log_open_syslog(void) {
                 goto fail;
         }
 
-        log_info("Succesfully opened syslog for logging.");
+        log_debug("Succesfully opened syslog for logging.");
 
         return 0;
 
@@ -218,15 +220,16 @@ static int write_to_console(
         snprintf(location, sizeof(location), "(%s:%u) ", file, line);
         char_array_0(location);
 
-        highlight = LOG_PRI(level) <= LOG_ERR;
+        highlight = LOG_PRI(level) <= LOG_ERR && show_color;
 
         zero(iovec);
-        IOVEC_SET_STRING(iovec[n++], location);
+        if (show_location)
+                IOVEC_SET_STRING(iovec[n++], location);
         if (highlight)
-                IOVEC_SET_STRING(iovec[n++], "\x1B[1;31m");
+                IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_ON);
         IOVEC_SET_STRING(iovec[n++], buffer);
         if (highlight)
-                IOVEC_SET_STRING(iovec[n++], "\x1B[0m");
+                IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_OFF);
         IOVEC_SET_STRING(iovec[n++], "\n");
 
         if (writev(console_fd, iovec, n) < 0)
@@ -261,13 +264,13 @@ static int write_to_syslog(
         if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
                 return -EINVAL;
 
-        snprintf(header_pid, sizeof(header_pid), "[%llu]: ", (unsigned long long) getpid());
+        snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) getpid());
         char_array_0(header_pid);
 
         zero(iovec);
         IOVEC_SET_STRING(iovec[0], header_priority);
         IOVEC_SET_STRING(iovec[1], header_time);
-        IOVEC_SET_STRING(iovec[2], __progname);
+        IOVEC_SET_STRING(iovec[2], program_invocation_short_name);
         IOVEC_SET_STRING(iovec[3], header_pid);
         IOVEC_SET_STRING(iovec[4], buffer);
 
@@ -275,7 +278,7 @@ static int write_to_syslog(
         msghdr.msg_iov = iovec;
         msghdr.msg_iovlen = ELEMENTSOF(iovec);
 
-        if (sendmsg(syslog_fd, &msghdr, 0) < 0)
+        if (sendmsg(syslog_fd, &msghdr, MSG_NOSIGNAL) < 0)
                 return -errno;
 
         return 1;
@@ -297,12 +300,12 @@ static int write_to_kmsg(
         snprintf(header_priority, sizeof(header_priority), "<%i>", LOG_PRI(level));
         char_array_0(header_priority);
 
-        snprintf(header_pid, sizeof(header_pid), "[%llu]: ", (unsigned long long) getpid());
+        snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) getpid());
         char_array_0(header_pid);
 
         zero(iovec);
         IOVEC_SET_STRING(iovec[0], header_priority);
-        IOVEC_SET_STRING(iovec[1], __progname);
+        IOVEC_SET_STRING(iovec[1], program_invocation_short_name);
         IOVEC_SET_STRING(iovec[2], header_pid);
         IOVEC_SET_STRING(iovec[3], buffer);
         IOVEC_SET_STRING(iovec[4], "\n");
@@ -327,7 +330,7 @@ static int log_dispatch(
 
         do {
                 char *e;
-                int k;
+                int k = 0;
 
                 buffer += strspn(buffer, NEWLINE);
 
@@ -340,24 +343,26 @@ static int log_dispatch(
                 if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
                     log_target == LOG_TARGET_SYSLOG) {
 
-                        if ((r = write_to_syslog(level, file, line, func, buffer)) < 0) {
+                        if ((k = write_to_syslog(level, file, line, func, buffer)) < 0) {
                                 log_close_syslog();
                                 log_open_kmsg();
-                        } else if (r > 0)
+                        } else if (k > 0)
                                 r++;
                 }
 
-                if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
-                    log_target == LOG_TARGET_KMSG) {
+                if (k <= 0 &&
+                    (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
+                     log_target == LOG_TARGET_KMSG)) {
 
-                        if ((r = write_to_kmsg(level, file, line, func, buffer)) < 0) {
+                        if ((k = write_to_kmsg(level, file, line, func, buffer)) < 0) {
                                 log_close_kmsg();
                                 log_open_console();
-                        } else if (r > 0)
+                        } else if (k > 0)
                                 r++;
                 }
 
-                if ((k = write_to_console(level, file, line, func, buffer)) < 0)
+                if (k <= 0 &&
+                    (k = write_to_console(level, file, line, func, buffer)) < 0)
                         return k;
 
                 buffer = e;
@@ -394,7 +399,7 @@ int log_meta(
         const char *func,
         const char *format, ...) {
 
-        char buffer[LOG_BUFFER_MAX];
+        char buffer[LINE_MAX];
         int saved_errno, r;
         va_list ap;
 
@@ -421,7 +426,7 @@ void log_assert(
         const char *func,
         const char *format, ...) {
 
-        static char buffer[LOG_BUFFER_MAX];
+        static char buffer[LINE_MAX];
         int saved_errno = errno;
         va_list ap;
 
@@ -469,6 +474,15 @@ void log_parse_environment(void) {
         if ((e = getenv("SYSTEMD_LOG_LEVEL")))
                 if (log_set_max_level_from_string(e) < 0)
                         log_warning("Failed to parse log level %s. Ignoring.", e);
+
+        if ((e = getenv("SYSTEMD_LOG_COLOR")))
+                if (log_show_color_from_string(e) < 0)
+                        log_warning("Failed to parse bool %s. Ignoring.", e);
+
+        if ((e = getenv("SYSTEMD_LOG_LOCATION"))) {
+                if (log_show_location_from_string(e) < 0)
+                        log_warning("Failed to parse bool %s. Ignoring.", e);
+        }
 }
 
 LogTarget log_get_target(void) {
@@ -479,6 +493,34 @@ int log_get_max_level(void) {
         return log_max_level;
 }
 
+void log_show_color(bool b) {
+        show_color = b;
+}
+
+void log_show_location(bool b) {
+        show_location = b;
+}
+
+int log_show_color_from_string(const char *e) {
+        int t;
+
+        if ((t = parse_boolean(e)) < 0)
+                return -EINVAL;
+
+        log_show_color(t);
+        return 0;
+}
+
+int log_show_location_from_string(const char *e) {
+        int t;
+
+        if ((t = parse_boolean(e)) < 0)
+                return -EINVAL;
+
+        log_show_location(t);
+        return 0;
+}
+
 static const char *const log_target_table[] = {
         [LOG_TARGET_CONSOLE] = "console",
         [LOG_TARGET_SYSLOG] = "syslog",