diff options
Diffstat (limited to 'common/gdm-log.c')
-rw-r--r-- | common/gdm-log.c | 180 |
1 files changed, 72 insertions, 108 deletions
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; } |