diff options
author | Colin Walters <walters@verbum.org> | 2013-01-22 14:20:08 -0500 |
---|---|---|
committer | Ray Strode <rstrode@redhat.com> | 2013-02-04 23:00:50 -0500 |
commit | 9fbd02da3c1fa2e2d3b64b9546926ac22f912ca2 (patch) | |
tree | e07659ed87323b91caa6cd5206dc63045d2a8b08 /common | |
parent | 979753ad717dc7480472845f570752ff8ded91c2 (diff) | |
download | gdm-9fbd02da3c1fa2e2d3b64b9546926ac22f912ca2.tar.gz |
log: When using systemd, just log to stdout/stderr
1) This avoids duplicated output since we were using LOG_PERROR
which writes to both syslog and stderr.
2) syslog() is basically deprecated in the systemd world, so let's
avoid using it.
In this approach, we also output a level prefix to stderr.
Some adjustments from Simon McVittie
https://bugzilla.gnome.org/show_bug.cgi?id=692308
Diffstat (limited to 'common')
-rw-r--r-- | common/Makefile.am | 2 | ||||
-rw-r--r-- | common/gdm-log.c | 180 | ||||
-rw-r--r-- | common/gdm-log.h | 4 |
3 files changed, 74 insertions, 112 deletions
diff --git a/common/Makefile.am b/common/Makefile.am index c28d9b28..9717db82 100644 --- a/common/Makefile.am +++ b/common/Makefile.am @@ -85,9 +85,11 @@ libgdmcommon_la_CPPFLAGS = \ libgdmcommon_la_CFLAGS = \ $(COMMON_CFLAGS) \ + $(SYSTEMD_CFLAGS) \ $(NULL) libgdmcommon_la_LIBADD = \ + $(SYSTEMD_LIBS) \ $(NULL) libgdmcommon_la_LDFLAGS = \ diff --git a/common/gdm-log.c b/common/gdm-log.c index e9bf4715..22d8c7d6 100644 --- a/common/gdm-log.c +++ b/common/gdm-log.c @@ -30,6 +30,9 @@ #include <unistd.h> #include <syslog.h> +#ifdef WITH_SYSTEMD +#include <systemd/sd-daemon.h> +#endif #include <glib.h> #include <glib/gstdio.h> @@ -37,143 +40,87 @@ #include "gdm-log.h" static gboolean initialized = FALSE; -static int syslog_levels = G_LOG_LEVEL_MASK & ~G_LOG_LEVEL_DEBUG; +static gboolean is_sd_booted = FALSE; +static gboolean debug_enabled = FALSE; -static void -log_level_to_priority_and_prefix (GLogLevelFlags log_level, - int *priorityp, - const char **prefixp) +static gint +get_syslog_priority_from_log_level (GLogLevelFlags log_level) { - int priority; - const char *prefix; - - /* Process the message prefix and priority */ switch (log_level & G_LOG_LEVEL_MASK) { case G_LOG_FLAG_FATAL: - priority = LOG_EMERG; - prefix = "FATAL"; - break; + return LOG_EMERG; case G_LOG_LEVEL_ERROR: - priority = LOG_ERR; - prefix = "ERROR"; - break; + /* fatal error - a critical error, in the syslog world */ + return LOG_CRIT; case G_LOG_LEVEL_CRITICAL: - priority = LOG_CRIT; - prefix = "CRITICAL"; - break; + /* critical warning - an error, in the syslog world */ + return LOG_ERR; case G_LOG_LEVEL_WARNING: - priority = LOG_WARNING; - prefix = "WARNING"; - break; case G_LOG_LEVEL_MESSAGE: - priority = LOG_NOTICE; - prefix = "MESSAGE"; - break; + return LOG_NOTICE; case G_LOG_LEVEL_INFO: - priority = LOG_INFO; - prefix = "INFO"; - break; + return LOG_INFO; case G_LOG_LEVEL_DEBUG: - /* if debug was requested then bump this up to ERROR - * to ensure it is seen in a log */ - if (syslog_levels & G_LOG_LEVEL_DEBUG) { - priority = LOG_WARNING; - prefix = "DEBUG(+)"; - } else { - priority = LOG_DEBUG; - prefix = "DEBUG"; - } - break; default: - priority = LOG_DEBUG; - prefix = "UNKNOWN"; - break; - } - - if (priorityp != NULL) { - *priorityp = priority; - } - if (prefixp != NULL) { - *prefixp = prefix; + return LOG_DEBUG; } } -void -gdm_log_default_handler (const gchar *log_domain, - GLogLevelFlags log_level, - const gchar *message, - gpointer unused_data) +static void +gdm_log_default_handler (const gchar *log_domain, + GLogLevelFlags log_level, + const gchar *message, + gpointer unused_data) { - GString *gstring; - int priority; - const char *level_prefix; - char *string; - gboolean do_log; - gboolean is_fatal; + int priority; - is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0; + gdm_log_init (); - do_log = (log_level & syslog_levels); - if (! do_log) { + if ((log_level & G_LOG_LEVEL_MASK) == G_LOG_LEVEL_DEBUG && + !debug_enabled) { return; } - if (! initialized) { - gdm_log_init (); - } - - log_level_to_priority_and_prefix (log_level, - &priority, - &level_prefix); - - gstring = g_string_new (NULL); - - if (log_domain != NULL) { - g_string_append (gstring, log_domain); - g_string_append_c (gstring, '-'); - } - g_string_append (gstring, level_prefix); - - g_string_append (gstring, ": "); - if (message == NULL) { - g_string_append (gstring, "(NULL) message"); - } else { - g_string_append (gstring, message); - } - if (is_fatal) { - g_string_append (gstring, "\naborting...\n"); + /* Process the message prefix and priority */ + priority = get_syslog_priority_from_log_level (log_level); + + if (is_sd_booted) { + fprintf (stderr, + "<%d>%s%s%s\n", + priority, + log_domain != NULL? log_domain : "", + log_domain != NULL? ": " : "", + message); + fflush (stderr); } else { - g_string_append (gstring, "\n"); + syslog (priority, + "%s%s%s\n", + log_domain != NULL? log_domain : "", + log_domain != NULL? ": " : "", + message); } - - string = g_string_free (gstring, FALSE); - - syslog (priority, "%s", string); - - g_free (string); } void gdm_log_toggle_debug (void) { - if (syslog_levels & G_LOG_LEVEL_DEBUG) { - g_debug ("Debugging disabled"); - syslog_levels &= ~G_LOG_LEVEL_DEBUG; - } else { - syslog_levels |= G_LOG_LEVEL_DEBUG; - g_debug ("Debugging enabled"); - } + gdm_log_set_debug (!debug_enabled); } void gdm_log_set_debug (gboolean debug) { + g_assert (initialized); + if (debug_enabled == debug) { + return; + } + if (debug) { - syslog_levels |= G_LOG_LEVEL_DEBUG; + debug_enabled = debug; g_debug ("Enabling debugging"); } else { g_debug ("Disabling debugging"); - syslog_levels &= ~G_LOG_LEVEL_DEBUG; + debug_enabled = debug; } } @@ -183,24 +130,41 @@ gdm_log_init (void) const char *prg_name; int options; + if (initialized) + return; + + initialized = TRUE; + +#ifdef WITH_SYSTEMD + is_sd_booted = sd_booted () > 0; +#endif + g_log_set_default_handler (gdm_log_default_handler, NULL); - prg_name = g_get_prgname (); + /* Only set up syslog if !systemd, otherwise with systemd + * enabled, we keep the default GLib log handler which goes to + * stderr, which is routed to the appropriate place in the + * systemd service file. + */ + if (!is_sd_booted) { + prg_name = g_get_prgname (); - options = LOG_PID; + options = LOG_PID; #ifdef LOG_PERROR - options |= LOG_PERROR; + options |= LOG_PERROR; #endif - openlog (prg_name, options, LOG_DAEMON); - - initialized = TRUE; + openlog (prg_name, options, LOG_DAEMON); + } } void gdm_log_shutdown (void) { - closelog (); + if (!initialized) + return; + if (!is_sd_booted) + closelog (); initialized = FALSE; } diff --git a/common/gdm-log.h b/common/gdm-log.h index c7f39769..db6ed873 100644 --- a/common/gdm-log.h +++ b/common/gdm-log.h @@ -28,10 +28,6 @@ G_BEGIN_DECLS -void gdm_log_default_handler (const gchar *log_domain, - GLogLevelFlags log_level, - const gchar *message, - gpointer unused_data); void gdm_log_set_debug (gboolean debug); void gdm_log_toggle_debug (void); void gdm_log_init (void); |