diff options
author | David Zeuthen <davidz@redhat.com> | 2011-05-17 16:31:09 -0400 |
---|---|---|
committer | David Zeuthen <davidz@redhat.com> | 2011-05-17 16:31:09 -0400 |
commit | d289339ff9979a8a73e56b645a6779cdf914d94e (patch) | |
tree | c296e1c2c987cfcb702bfcb9dc7841d6efb85432 | |
parent | 9698a2a8f6a3e9a9a77dda192872e6a7a9d176b4 (diff) |
Add logging routines
Signed-off-by: David Zeuthen <davidz@redhat.com>
-rw-r--r-- | doc/goa-docs.xml | 1 | ||||
-rw-r--r-- | doc/goa-sections.txt | 13 | ||||
-rw-r--r-- | src/daemon/Makefile.am | 4 | ||||
-rw-r--r-- | src/daemon/goadaemon.c | 8 | ||||
-rw-r--r-- | src/daemon/main.c | 19 | ||||
-rw-r--r-- | src/goa/Makefile.am | 4 | ||||
-rw-r--r-- | src/goabackend/Makefile.am | 6 | ||||
-rw-r--r-- | src/goabackend/goabackend.h | 1 | ||||
-rw-r--r-- | src/goabackend/goabackendenums.h | 19 | ||||
-rw-r--r-- | src/goabackend/goalogging.c | 219 | ||||
-rw-r--r-- | src/goabackend/goalogging.h | 83 | ||||
-rw-r--r-- | src/goabackend/goaoauth2provider.c | 7 | ||||
-rw-r--r-- | src/goabackend/goaoauthprovider.c | 7 |
13 files changed, 373 insertions, 18 deletions
diff --git a/doc/goa-docs.xml b/doc/goa-docs.xml index 05eee4b..3c89c8c 100644 --- a/doc/goa-docs.xml +++ b/doc/goa-docs.xml @@ -131,6 +131,7 @@ <part id="ref-backend-library"> <title>Backend Library API Reference</title> + <xi:include href="xml/goalog.xml"/> <xi:include href="xml/goaprovider.xml"/> <xi:include href="xml/goaoauthprovider.xml"/> <xi:include href="xml/goaoauth2provider.xml"/> diff --git a/doc/goa-sections.txt b/doc/goa-sections.txt index a3f4b83..09140dc 100644 --- a/doc/goa-sections.txt +++ b/doc/goa-sections.txt @@ -750,3 +750,16 @@ GOA_IS_IMAP_AUTH_OAUTH GOA_TYPE_IMAP_AUTH_OAUTH goa_imap_auth_oauth_get_type </SECTION> + +<SECTION> +<FILE>goalog</FILE> +GoaLogLevel +goa_log +<SUBSECTION> +goa_debug +goa_info +goa_notice +goa_warning +goa_error +<SUBSECTION Standard> +</SECTION> diff --git a/src/daemon/Makefile.am b/src/daemon/Makefile.am index 42e0dcd..c627cd1 100644 --- a/src/daemon/Makefile.am +++ b/src/daemon/Makefile.am @@ -25,6 +25,10 @@ goa_daemon_SOURCES = \ gposixsignal.h gposixsignal.c \ $(NULL) +goa_daemon_CPPFLAGS = \ + -DG_LOG_DOMAIN=\"goa-daemon\" \ + $(NULL) + goa_daemon_CFLAGS = \ $(GLIB_CFLAGS) \ $(GTK_CFLAGS) \ diff --git a/src/daemon/goadaemon.c b/src/daemon/goadaemon.c index 721bccd..c3ff84b 100644 --- a/src/daemon/goadaemon.c +++ b/src/daemon/goadaemon.c @@ -384,7 +384,7 @@ update_account_object (GoaDaemon *daemon, type = NULL; account = NULL; - g_debug ("updating %s %d", g_dbus_object_get_object_path (G_DBUS_OBJECT (object)), just_added); + goa_debug ("updating %s %d", g_dbus_object_get_object_path (G_DBUS_OBJECT (object)), just_added); type = g_key_file_get_string (key_file, group, "Type", NULL); name = g_key_file_get_string (key_file, group, "Name", NULL); @@ -511,7 +511,7 @@ process_config_entries (GoaDaemon *daemon, g_signal_handlers_disconnect_by_func (goa_object_peek_account (object), G_CALLBACK (on_account_handle_remove), daemon); - g_debug ("removing %s", object_path); + goa_debug ("removing %s", object_path); g_warn_if_fail (g_dbus_object_manager_server_unexport (daemon->object_manager, object_path)); } for (l = added; l != NULL; l = l->next) @@ -521,7 +521,7 @@ process_config_entries (GoaDaemon *daemon, gchar *group; GKeyFile *key_file; - g_debug ("adding %s", object_path); + goa_debug ("adding %s", object_path); group = object_path_to_group (object_path); key_file = g_hash_table_lookup (group_name_to_key_file, group); @@ -558,7 +558,7 @@ process_config_entries (GoaDaemon *daemon, gchar *group; GKeyFile *key_file; - g_debug ("unchanged %s", object_path); + goa_debug ("unchanged %s", object_path); group = object_path_to_group (object_path); key_file = g_hash_table_lookup (group_name_to_key_file, group); diff --git a/src/daemon/main.c b/src/daemon/main.c index 4439b45..78841a5 100644 --- a/src/daemon/main.c +++ b/src/daemon/main.c @@ -27,9 +27,12 @@ #include <signal.h> #include <gio/gio.h> +#include <goabackend/goabackend.h> + #include "gposixsignal.h" #include "goadaemon.h" + /* ---------------------------------------------------------------------------------------------------- */ static GMainLoop *loop = NULL; @@ -50,7 +53,7 @@ on_bus_acquired (GDBusConnection *connection, { if (connection != NULL) the_daemon = goa_daemon_new (); - g_print ("Connected to the session bus\n"); + goa_info ("Connected to the session bus"); } static void @@ -58,7 +61,7 @@ on_name_lost (GDBusConnection *connection, const gchar *name, gpointer user_data) { - g_print ("Lost (or failed to acquire) the name %s on the session message bus\n", name); + goa_info ("Lost (or failed to acquire) the name %s on the session message bus", name); g_main_loop_quit (loop); } @@ -67,13 +70,13 @@ on_name_acquired (GDBusConnection *connection, const gchar *name, gpointer user_data) { - g_print ("Acquired the name %s on the session message bus\n", name); + goa_info ("Acquired the name %s on the session message bus", name); } static gboolean on_sigint (gpointer user_data) { - g_print ("Caught SIGINT. Initiating shutdown.\n"); + goa_info ("Caught SIGINT. Initiating shutdown."); g_main_loop_quit (loop); return FALSE; } @@ -101,12 +104,12 @@ main (int argc, error = NULL; if (!g_option_context_parse (opt_context, &argc, &argv, &error)) { - g_printerr ("Error parsing options: %s\n", error->message); + goa_error ("Error parsing options: %s", error->message); g_error_free (error); goto out; } - g_print ("goa-daemon version %s starting\n", PACKAGE_VERSION); + goa_notice ("goa-daemon version %s starting", PACKAGE_VERSION); loop = g_main_loop_new (NULL, FALSE); @@ -130,7 +133,7 @@ main (int argc, NULL, NULL); - g_print ("Entering main event loop\n"); + goa_info ("Entering main event loop"); g_main_loop_run (loop); @@ -148,7 +151,7 @@ main (int argc, if (opt_context != NULL) g_option_context_free (opt_context); - g_print ("goa-daemon version %s exiting\n", PACKAGE_VERSION); + goa_notice ("goa-daemon version %s exiting", PACKAGE_VERSION); return ret; } diff --git a/src/goa/Makefile.am b/src/goa/Makefile.am index 0e3cf87..cc92b9b 100644 --- a/src/goa/Makefile.am +++ b/src/goa/Makefile.am @@ -72,6 +72,10 @@ libgoa_1_0_la_SOURCES = \ goaenumtypes.h goaenumtypes.c \ $(NULL) +libgoa_1_0_la_CPPFLAGS = \ + -DG_LOG_DOMAIN=\"Goa\" \ + $(NULL) + libgoa_1_0_la_CFLAGS = \ -DGOA_COMPILATION \ $(GLIB_CFLAGS) \ diff --git a/src/goabackend/Makefile.am b/src/goabackend/Makefile.am index 2f9e5e9..c32c67f 100644 --- a/src/goabackend/Makefile.am +++ b/src/goabackend/Makefile.am @@ -43,6 +43,7 @@ libgoa_backend_1_0_la_HEADERS = \ goabackendtypes.h \ goabackendenums.h \ goabackendenumtypes.h \ + goalogging.h \ goaprovider.h \ goaoauthprovider.h \ goaoauth2provider.h \ @@ -62,6 +63,7 @@ libgoa_backend_1_0_la_SOURCES = \ goabackendenums.h \ goabackendenumtypes.h goabackendenumtypes.c \ goaprovider.h goaprovider.c \ + goalogging.h goalogging.c \ goaoauthprovider.h goaoauthprovider.c \ goaoauth2provider.h goaoauth2provider.c \ goagoogleprovider.h goagoogleprovider.c \ @@ -74,6 +76,10 @@ libgoa_backend_1_0_la_SOURCES = \ goaimapmail.h goaimapmail.c \ $(NULL) +libgoa_backend_1_0_la_CPPFLAGS = \ + -DG_LOG_DOMAIN=\"GoaBackend\" \ + $(NULL) + libgoa_backend_1_0_la_CFLAGS = \ -DGOA_BACKEND_COMPILATION \ -DGOA_API_IS_SUBJECT_TO_CHANGE \ diff --git a/src/goabackend/goabackend.h b/src/goabackend/goabackend.h index 536f8b3..6e96c8f 100644 --- a/src/goabackend/goabackend.h +++ b/src/goabackend/goabackend.h @@ -29,6 +29,7 @@ #define __GOA_BACKEND_INSIDE_GOA_BACKEND_H__ #include <goabackend/goabackendtypes.h> +#include <goabackend/goalogging.h> #include <goabackend/goaprovider.h> #include <goabackend/goaoauthprovider.h> #include <goabackend/goaoauth2provider.h> diff --git a/src/goabackend/goabackendenums.h b/src/goabackend/goabackendenums.h index 8cd0899..6ac607d 100644 --- a/src/goabackend/goabackendenums.h +++ b/src/goabackend/goabackendenums.h @@ -31,6 +31,25 @@ G_BEGIN_DECLS +/** + * GoaLogLevel: + * @GOA_LOG_LEVEL_DEBUG: Debug messages. + * @GOA_LOG_LEVEL_INFO: Informational messages. + * @GOA_LOG_LEVEL_NOTICE: Messages that the administrator should take notice of. + * @GOA_LOG_LEVEL_WARNING: Warning messages. + * @GOA_LOG_LEVEL_ERROR: Error messages. + * + * Logging levels. + */ +typedef enum +{ + GOA_LOG_LEVEL_DEBUG, + GOA_LOG_LEVEL_INFO, + GOA_LOG_LEVEL_NOTICE, + GOA_LOG_LEVEL_WARNING, + GOA_LOG_LEVEL_ERROR +} GoaLogLevel; + G_END_DECLS #endif /* __GOA_BACKEND_ENUMS_H__ */ diff --git a/src/goabackend/goalogging.c b/src/goabackend/goalogging.c new file mode 100644 index 0000000..75c8d18 --- /dev/null +++ b/src/goabackend/goalogging.c @@ -0,0 +1,219 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ +/* + * Copyright (C) 2011 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307, USA. + * + * Author: David Zeuthen <davidz@redhat.com> + */ + +#include "config.h" +#include <glib/gi18n-lib.h> + +#include <syslog.h> + +#include "goalogging.h" + + +/** + * SECTION:goalog + * @title: Logging + * @short_description: Logging Routines + * + * TODO: explain a bit more what these logging routines do - e.g. that + * %GOA_LOG_LEVEL_NOTICE and higher goes to the syslog and so on + * etc. etc. + */ + + +G_LOCK_DEFINE_STATIC (log_lock); + +/* ---------------------------------------------------------------------------------------------------- */ + +typedef enum +{ + _COLOR_RESET, + _COLOR_BOLD_ON, + _COLOR_INVERSE_ON, + _COLOR_BOLD_OFF, + _COLOR_FG_BLACK, + _COLOR_FG_RED, + _COLOR_FG_GREEN, + _COLOR_FG_YELLOW, + _COLOR_FG_BLUE, + _COLOR_FG_MAGENTA, + _COLOR_FG_CYAN, + _COLOR_FG_WHITE, + _COLOR_BG_RED, + _COLOR_BG_GREEN, + _COLOR_BG_YELLOW, + _COLOR_BG_BLUE, + _COLOR_BG_MAGENTA, + _COLOR_BG_CYAN, + _COLOR_BG_WHITE +} _Color; + +static gboolean _color_stdin_is_tty = FALSE; +static gboolean _color_initialized = FALSE; + +static void +_color_init (void) +{ + if (_color_initialized) + return; + _color_initialized = TRUE; + _color_stdin_is_tty = (isatty (STDIN_FILENO) != 0 && isatty (STDOUT_FILENO) != 0); +} + +static const gchar * +_color_get (_Color color) +{ + const gchar *str; + + _color_init (); + + if (!_color_stdin_is_tty) + return ""; + + str = NULL; + switch (color) + { + case _COLOR_RESET: str="\x1b[0m"; break; + case _COLOR_BOLD_ON: str="\x1b[1m"; break; + case _COLOR_INVERSE_ON: str="\x1b[7m"; break; + case _COLOR_BOLD_OFF: str="\x1b[22m"; break; + case _COLOR_FG_BLACK: str="\x1b[30m"; break; + case _COLOR_FG_RED: str="\x1b[31m"; break; + case _COLOR_FG_GREEN: str="\x1b[32m"; break; + case _COLOR_FG_YELLOW: str="\x1b[33m"; break; + case _COLOR_FG_BLUE: str="\x1b[34m"; break; + case _COLOR_FG_MAGENTA: str="\x1b[35m"; break; + case _COLOR_FG_CYAN: str="\x1b[36m"; break; + case _COLOR_FG_WHITE: str="\x1b[37m"; break; + case _COLOR_BG_RED: str="\x1b[41m"; break; + case _COLOR_BG_GREEN: str="\x1b[42m"; break; + case _COLOR_BG_YELLOW: str="\x1b[43m"; break; + case _COLOR_BG_BLUE: str="\x1b[44m"; break; + case _COLOR_BG_MAGENTA: str="\x1b[45m"; break; + case _COLOR_BG_CYAN: str="\x1b[46m"; break; + case _COLOR_BG_WHITE: str="\x1b[47m"; break; + default: + g_assert_not_reached (); + break; + } + return str; +} + +/* ---------------------------------------------------------------------------------------------------- */ + +/** + * goa_log: + * @level: A #GoaLogLevel. + * @function: Pass #G_STRFUNC here. + * @location: Pass #G_STRLOC here. + * @format: printf()-style format. + * @...: Arguments for format. + * + * Low-level logging function used by goa_debug() and other macros. + */ +void +goa_log (GoaLogLevel level, + const gchar *function, + const gchar *location, + const gchar *format, + ...) +{ + va_list var_args; + gchar *message; + GTimeVal now; + time_t now_time; + struct tm *now_tm; + gchar time_buf[128]; + const gchar *level_str; + const gchar *level_color_str; + gint syslog_priority; + static gboolean have_called_openlog = FALSE; + + va_start (var_args, format); + message = g_strdup_vprintf (format, var_args); + va_end (var_args); + + G_LOCK (log_lock); + + if (!have_called_openlog) + { + openlog ("goa", + LOG_CONS|LOG_NDELAY|LOG_PID, + LOG_DAEMON); + have_called_openlog = TRUE; + } + + g_get_current_time (&now); + now_time = (time_t) now.tv_sec; + now_tm = localtime (&now_time); + strftime (time_buf, sizeof time_buf, "%H:%M:%S", now_tm); + + switch (level) + { + case GOA_LOG_LEVEL_DEBUG: + level_str = "[DEBUG]"; + syslog_priority = LOG_DEBUG; + level_color_str = _color_get (_COLOR_FG_BLUE); + break; + + case GOA_LOG_LEVEL_INFO: + level_str = "[INFO]"; + syslog_priority = LOG_INFO; + level_color_str = _color_get (_COLOR_FG_CYAN); + break; + + case GOA_LOG_LEVEL_NOTICE: + level_str = "[NOTICE]"; + syslog_priority = LOG_NOTICE; + level_color_str = _color_get (_COLOR_FG_CYAN); + break; + + case GOA_LOG_LEVEL_WARNING: + level_str = "[WARNING]"; + syslog_priority = LOG_WARNING; + level_color_str = _color_get (_COLOR_FG_YELLOW); + break; + + case GOA_LOG_LEVEL_ERROR: + level_str = "[ERROR]"; + syslog_priority = LOG_ERR; + level_color_str = _color_get (_COLOR_FG_RED); + break; + + default: + g_assert_not_reached (); + break; + } + g_print ("%s%s%s.%03d:%s %s%s%s:%s %s %s[%s, %s()]%s\n", + _color_get (_COLOR_BOLD_ON), _color_get (_COLOR_FG_YELLOW), + time_buf, (gint) now.tv_usec / 1000, + _color_get (_COLOR_RESET), + level_color_str, + _color_get (_COLOR_BOLD_ON), level_str, + _color_get (_COLOR_RESET), + message, + _color_get (_COLOR_FG_BLACK), location, function, _color_get (_COLOR_RESET)); + if (level >= GOA_LOG_LEVEL_NOTICE) + syslog (syslog_priority, "%s [%s, %s()]", message, location, function); + g_free (message); + + G_UNLOCK (log_lock); +} diff --git a/src/goabackend/goalogging.h b/src/goabackend/goalogging.h new file mode 100644 index 0000000..6809313 --- /dev/null +++ b/src/goabackend/goalogging.h @@ -0,0 +1,83 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ +/* + * Copyright (C) 2011 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307, USA. + * + * Author: David Zeuthen <davidz@redhat.com> + */ + +#if !defined (__GOA_BACKEND_INSIDE_GOA_BACKEND_H__) && !defined (GOA_BACKEND_COMPILATION) +#error "Only <goabackend/goabackend.h> can be included directly." +#endif + +#ifndef __GOA_LOGGING_H__ +#define __GOA_LOGGING_H__ + +#include <goabackend/goabackendtypes.h> + +G_BEGIN_DECLS + +void goa_log (GoaLogLevel level, + const gchar *function, + const gchar *location, + const gchar *format, + ...) G_GNUC_PRINTF (4, 5); + +/** + * goa_debug: + * @args: printf()-style format string and arguments + * + * Logging macro for %GOA_LOG_LEVEL_DEBUG. + */ +#define goa_debug(args...) goa_log(GOA_LOG_LEVEL_DEBUG, G_STRFUNC, G_STRLOC, args); + +/** + * goa_info: + * @args: printf()-style format string and arguments + * + * Logging macro for %GOA_LOG_LEVEL_INFO. + */ +#define goa_info(args...) goa_log(GOA_LOG_LEVEL_INFO, G_STRFUNC, G_STRLOC, args); + +/** + * goa_notice: + * @args: printf()-style format string and arguments + * + * Logging macro for %GOA_LOG_LEVEL_NOTICE. + */ +#define goa_notice(args...) goa_log(GOA_LOG_LEVEL_NOTICE, G_STRFUNC, G_STRLOC, args); + +/** + * goa_warning: + * @args: printf()-style format string and arguments + * + * Logging macro for %GOA_LOG_LEVEL_WARNING. + */ +#define goa_warning(args...) goa_log(GOA_LOG_LEVEL_WARNING, G_STRFUNC, G_STRLOC, args); + +/** + * goa_error: + * @args: printf()-style format string and arguments + * + * Logging macro for %GOA_LOG_LEVEL_ERROR. + */ +#define goa_error(args...) goa_log(GOA_LOG_LEVEL_ERROR, G_STRFUNC, G_STRLOC, args); + + +G_END_DECLS + +#endif /* __GOA_LOGGING_H__ */ diff --git a/src/goabackend/goaoauth2provider.c b/src/goabackend/goaoauth2provider.c index 74cc6c1..6dbeb1d 100644 --- a/src/goabackend/goaoauth2provider.c +++ b/src/goabackend/goaoauth2provider.c @@ -28,6 +28,7 @@ #include <webkit/webkit.h> #include <json-glib/json-glib.h> +#include "goalogging.h" #include "goaprovider.h" #include "goaoauth2provider.h" @@ -1193,7 +1194,7 @@ goa_oauth2_provider_get_access_token_sync (GoaOAuth2Provider *provider, /* if we can't refresh the token, just return it no matter what */ if (refresh_token == NULL) { - g_debug ("Returning locally cached credentials that cannot be refreshed"); + goa_debug ("Returning locally cached credentials that cannot be refreshed"); success = TRUE; goto out; } @@ -1204,12 +1205,12 @@ goa_oauth2_provider_get_access_token_sync (GoaOAuth2Provider *provider, */ if (!force_refresh && access_token_expires_in > 10*60) { - g_debug ("Returning locally cached credentials (expires in %d seconds)", access_token_expires_in); + goa_debug ("Returning locally cached credentials (expires in %d seconds)", access_token_expires_in); success = TRUE; goto out; } - g_debug ("Refreshing locally cached credentials (expires in %d seconds, force_refresh=%d)", access_token_expires_in, force_refresh); + goa_debug ("Refreshing locally cached credentials (expires in %d seconds, force_refresh=%d)", access_token_expires_in, force_refresh); /* Otherwise, refresh it */ old_refresh_token = refresh_token; refresh_token = NULL; diff --git a/src/goabackend/goaoauthprovider.c b/src/goabackend/goaoauthprovider.c index 543bd15..2fe8a25 100644 --- a/src/goabackend/goaoauthprovider.c +++ b/src/goabackend/goaoauthprovider.c @@ -28,6 +28,7 @@ #include <webkit/webkit.h> #include <json-glib/json-glib.h> +#include "goalogging.h" #include "goaprovider.h" #include "goaoauthprovider.h" @@ -1295,7 +1296,7 @@ goa_oauth_provider_get_access_token_sync (GoaOAuthProvider *provider, /* if we can't refresh the token, just return it no matter what */ if (session_handle == NULL) { - g_debug ("Returning locally cached credentials that cannot be refreshed"); + goa_debug ("Returning locally cached credentials that cannot be refreshed"); success = TRUE; goto out; } @@ -1306,12 +1307,12 @@ goa_oauth_provider_get_access_token_sync (GoaOAuthProvider *provider, */ if (!force_refresh && access_token_expires_in > 10*60) { - g_debug ("Returning locally cached credentials (expires in %d seconds)", access_token_expires_in); + goa_debug ("Returning locally cached credentials (expires in %d seconds)", access_token_expires_in); success = TRUE; goto out; } - g_debug ("Refreshing locally cached credentials (expires in %d seconds, force_refresh=%d)", access_token_expires_in, force_refresh); + goa_debug ("Refreshing locally cached credentials (expires in %d seconds, force_refresh=%d)", access_token_expires_in, force_refresh); /* Otherwise, refresh it */ access_token_for_refresh = access_token; access_token = NULL; |