From cb91d8b512234ba8866738f8b616359e272f203b Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Fri, 12 Apr 2013 11:02:50 +0200 Subject: [PATCH] Send internal warnings/criticals/messages to syslog https://bugs.freedesktop.org/show_bug.cgi?id=58084 --- service/realm-daemon.c | 108 ++++++++++++++++++++++++++++++++++++++++++++ service/realm-daemon.h | 5 ++ service/realm-diagnostics.c | 83 +++------------------------------- 3 files changed, 120 insertions(+), 76 deletions(-) diff --git a/service/realm-daemon.c b/service/realm-daemon.c index 9b0230a..1ba63dc 100644 --- a/service/realm-daemon.c +++ b/service/realm-daemon.c @@ -36,6 +36,12 @@ #include #include +#ifdef WITH_JOURNAL +#include +#else +#include +#endif + #define TIMEOUT 60 /* seconds */ #define HOLD_INTERNAL (GUINT_TO_POINTER (~0)) @@ -324,6 +330,107 @@ on_realm_log_debug (const gchar *log_domain, g_string_free (string, TRUE); } +static void +on_realm_log_message (const gchar *log_domain, + GLogLevelFlags log_level, + const gchar *message, + gpointer user_data) +{ + int level; + + /* Note that crit and err are the other way around in syslog */ + + switch (G_LOG_LEVEL_MASK & log_level) { + case G_LOG_LEVEL_ERROR: + level = LOG_CRIT; + break; + case G_LOG_LEVEL_CRITICAL: + level = LOG_ERR; + break; + case G_LOG_LEVEL_WARNING: + level = LOG_WARNING; + break; + case G_LOG_LEVEL_MESSAGE: + level = LOG_NOTICE; + break; + case G_LOG_LEVEL_INFO: + level = LOG_INFO; + break; + case G_LOG_LEVEL_DEBUG: + level = LOG_DEBUG; + break; + default: + level = LOG_ERR; + break; + } + + /* Log to syslog first */ + if (log_domain) + realm_daemon_syslog (NULL, level, "%s: %s", log_domain, message); + else + realm_daemon_syslog (NULL, level, "%s", message); + + /* And then to default handler for aborting and stuff like that */ + g_log_default_handler (log_domain, log_level, message, user_data); +} + +static void +prepare_syslog () +{ + GLogLevelFlags flags = G_LOG_FLAG_FATAL | G_LOG_LEVEL_ERROR | + G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | + G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO; + +#ifndef WITH_JOURNAL + openlog ("realmd", 0, LOG_AUTH); +#endif + + g_log_set_handler (NULL, flags, on_realm_log_message, NULL); + g_log_set_handler ("Glib", flags, on_realm_log_message, NULL); + g_log_set_default_handler (on_realm_log_message, NULL); +} + +#ifdef WITH_JOURNAL + +void +realm_daemon_syslog (const gchar *operation, + int prio, + const gchar *format, + ...) +{ + va_list ap; + gchar *message; + + va_start (ap, format); + message = g_strdup_vprintf (format, ap); + va_end (ap); + + sd_journal_send ("MESSAGE=%s", message, + "REALMD_OPERATION=%s", operation, + "PRIORITY=%i", prio, + "SYSLOG_FACILITY=%i", LOG_FAC (LOG_AUTH), + "SYSLOG_IDENTIFIER=realmd", + NULL); + + g_free (message); +} + +#else /* !WITH_JOURNAL */ + +void +realm_daemon_syslog (const gchar *operation, + int prio, + const gchar *format, + ...) +{ + va_list ap; + va_start (ap, format); + vsyslog (prio, format, ap); + va_end (ap); +} + +#endif /* !WITH_JOURNAL */ + static gboolean on_signal_quit (gpointer data) { @@ -362,6 +469,7 @@ main (int argc, textdomain (GETTEXT_PACKAGE); #endif + prepare_syslog (); g_type_init (); /* diff --git a/service/realm-daemon.h b/service/realm-daemon.h index 3c59d63..1071205 100644 --- a/service/realm-daemon.h +++ b/service/realm-daemon.h @@ -35,6 +35,11 @@ void realm_daemon_poke (void); void realm_daemon_export_object (GDBusObjectSkeleton *object); +void realm_daemon_syslog (const gchar *operation, + int prio, + const gchar *format, + ...) G_GNUC_PRINTF(3, 4); + G_END_DECLS #endif /* __REALM_DAEMON_H__ */ diff --git a/service/realm-diagnostics.c b/service/realm-diagnostics.c index 2468ac6..9f6b161 100644 --- a/service/realm-diagnostics.c +++ b/service/realm-diagnostics.c @@ -14,80 +14,14 @@ #include "config.h" +#include "realm-daemon.h" #include "realm-dbus-constants.h" #include "realm-diagnostics.h" #include "realm-invocation.h" #include - -static void system_openlog (void); -static void system_log_for_invocation (GDBusMethodInvocation *invocation, - gint prio, - const gchar *fmt, - ...) G_GNUC_PRINTF (3, 4); - -#ifdef WITH_JOURNAL - -#include - -static void -system_openlog (void) -{ - -} - -static void -system_log_for_invocation (GDBusMethodInvocation *invocation, - gint prio, - const gchar *fmt, - ...) -{ - va_list ap; - gchar *message; - const gchar *operation; - - va_start (ap, fmt); - message = g_strdup_vprintf (fmt, ap); - va_end (ap); - - operation = realm_invocation_get_operation (invocation); - if (operation == NULL) - operation = ""; - - sd_journal_send ("MESSAGE=%s", message, - "REALMD_OPERATION=%s", operation, - "PRIORITY=%i", prio, - "SYSLOG_FACILITY=%i", LOG_FAC (LOG_AUTH), - "SYSLOG_IDENTIFIER=realmd", - NULL); - - g_free (message); -} - -#else /* !WITH_JOURNAL */ - #include -static void -system_openlog (void) -{ - openlog ("realmd", 0, LOG_AUTH); -} - -static void -system_log_for_invocation (GDBusMethodInvocation *invocation, - gint prio, - const gchar *fmt, - ...) -{ - va_list ap; - va_start (ap, fmt); - vsyslog (prio, fmt, ap); - va_end (ap); -} - -#endif /* WITH_JOURNAL */ - static GDBusConnection *the_connection = NULL; static GString *line_buffer = NULL; @@ -110,18 +44,22 @@ log_syslog_and_debug (GDBusMethodInvocation *invocation, gchar *string, gsize length) { + const gchar *operation = NULL; gchar *at = string; gchar *ptr; + if (invocation) + operation = realm_invocation_get_operation (invocation); + /* Print all stderr lines as messages */ while ((ptr = memchr (at, '\n', length)) != NULL) { *ptr = '\0'; if (line_buffer && line_buffer->len > 0) { - system_log_for_invocation (invocation, log_level, "%s%s", line_buffer->str, at); + realm_daemon_syslog (operation, log_level, "%s%s", line_buffer->str, at); g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "%s%s", line_buffer->str, at); g_string_set_size (line_buffer, 0); } else { - system_log_for_invocation (invocation, log_level, "%s", at); + realm_daemon_syslog (operation, log_level, "%s", at); g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "%s", at); } @@ -144,13 +82,6 @@ log_take_diagnostic (GDBusMethodInvocation *invocation, int log_level, gchar *string) { - static gboolean syslog_initialized = FALSE; - - if (!syslog_initialized) { - system_openlog (); - syslog_initialized = TRUE; - } - log_syslog_and_debug (invocation, log_level, string, strlen (string)); realm_diagnostics_signal (invocation, string); -- 1.8.1.4