diff options
author | Rob Staudinger <robsta@linux.intel.com> | 2011-10-28 11:26:22 +0200 |
---|---|---|
committer | Rob Staudinger <robsta@linux.intel.com> | 2011-10-28 11:26:22 +0200 |
commit | e20c47605db3308ee67fb60a2968980be7657dbd (patch) | |
tree | e0b14cfa18cc7ac1075cdffaaed274bc986cebef | |
parent | 5d8d0f885444ad61b5cee3073bb0976fa4b7d434 (diff) |
logging: improve logging facilities
Include G_STRLOC in the log lines for better traceabilty of what's from
where.
-rw-r--r-- | ytstenut/empathy-tp-file.c | 2 | ||||
-rw-r--r-- | ytstenut/yts-client.c | 36 | ||||
-rw-r--r-- | ytstenut/yts-contact.c | 2 | ||||
-rw-r--r-- | ytstenut/yts-proxy-service.c | 2 | ||||
-rw-r--r-- | ytstenut/yts-proxy.c | 2 | ||||
-rw-r--r-- | ytstenut/yts-roster.c | 2 | ||||
-rw-r--r-- | ytstenut/yts-service-adapter.c | 2 | ||||
-rw-r--r-- | ytstenut/yts-service.c | 2 | ||||
-rw-r--r-- | ytstenut/ytstenut-internal.h | 23 | ||||
-rw-r--r-- | ytstenut/ytstenut.c | 126 |
10 files changed, 131 insertions, 68 deletions
diff --git a/ytstenut/empathy-tp-file.c b/ytstenut/empathy-tp-file.c index c32452d..2a87f6d 100644 --- a/ytstenut/empathy-tp-file.c +++ b/ytstenut/empathy-tp-file.c @@ -57,7 +57,7 @@ #include "yts-marshal.h" #undef G_LOG_DOMAIN -#define G_LOG_DOMAIN PACKAGE"\0file-transfer" +#define G_LOG_DOMAIN PACKAGE"\0file-transfer\0"G_STRLOC #if 0 #include "empathy-time.h" diff --git a/ytstenut/yts-client.c b/ytstenut/yts-client.c index 25700cb..e6de2c2 100644 --- a/ytstenut/yts-client.c +++ b/ytstenut/yts-client.c @@ -67,7 +67,7 @@ G_DEFINE_TYPE (YtsClient, yts_client, G_TYPE_OBJECT) (G_TYPE_INSTANCE_GET_PRIVATE ((o), YTS_TYPE_CLIENT, YtsClientPrivate)) #undef G_LOG_DOMAIN -#define G_LOG_DOMAIN PACKAGE"\0client" +#define G_LOG_DOMAIN PACKAGE"\0client\0"G_STRLOC /** * SECTION: yts-client @@ -944,25 +944,31 @@ yts_client_debug_msg_cb (TpProxy *proxy, gpointer data, GObject *weak_object) { - switch (level) - { + char *log_domain; + GLogLevelFlags log_level; + + log_domain = g_strdup_printf ("%s%c%s%c%s", + PACKAGE, '\0', "telepathy", '\0', domain); + + switch (level) { case 0: - g_error ("%s: %s", domain, msg); + log_level = G_LOG_LEVEL_ERROR; break; case 1: - g_critical ("%s: %s", domain, msg); + log_level = G_LOG_LEVEL_CRITICAL; break; case 2: - g_warning ("%s: %s", domain, msg); + log_level = G_LOG_LEVEL_WARNING; break; - default: case 3: - case 4: - g_message ("%s: %s", domain, msg); + log_level = G_LOG_LEVEL_MESSAGE; break; - case 5: - g_message ("%s: %s", domain, msg); - } + default: + log_level = G_LOG_LEVEL_INFO; + } + + g_log (log_domain, log_level, "%s", msg); + g_free (log_domain); } /* @@ -1187,11 +1193,9 @@ yts_client_account_cb (GObject *object, GAsyncResult *res, gpointer self) g_message ("Got account"); - if (YTS_DEBUG_TELEPATHY & ytstenut_get_debug_flags ()) - tp_debug_set_flags ("all"); - - if (YTS_DEBUG_CLIENT & ytstenut_get_debug_flags ()) + if (YTS_DEBUG_TELEPATHY & ytstenut_get_debug_flags ()) { yts_client_setup_debug (self); + } tp_account_prepare_async (priv->account, &features[0], diff --git a/ytstenut/yts-contact.c b/ytstenut/yts-contact.c index 5582599..f4e208d 100644 --- a/ytstenut/yts-contact.c +++ b/ytstenut/yts-contact.c @@ -42,7 +42,7 @@ G_DEFINE_TYPE (YtsContact, yts_contact, G_TYPE_OBJECT); (G_TYPE_INSTANCE_GET_PRIVATE ((o), YTS_TYPE_CONTACT, YtsContactPrivate)) #undef G_LOG_DOMAIN -#define G_LOG_DOMAIN PACKAGE"\0contact" +#define G_LOG_DOMAIN PACKAGE"\0contact\0"G_STRLOC /** * SECTION: yts-contact diff --git a/ytstenut/yts-proxy-service.c b/ytstenut/yts-proxy-service.c index 4d0f9cd..0c745a8 100644 --- a/ytstenut/yts-proxy-service.c +++ b/ytstenut/yts-proxy-service.c @@ -38,7 +38,7 @@ G_DEFINE_TYPE (YtsProxyService, yts_proxy_service, YTS_TYPE_SERVICE) (G_TYPE_INSTANCE_GET_PRIVATE ((o), YTS_TYPE_PROXY_SERVICE, YtsProxyServicePrivate)) #undef G_LOG_DOMAIN -#define G_LOG_DOMAIN PACKAGE"\0proxy-service" +#define G_LOG_DOMAIN PACKAGE"\0proxy-service\0"G_STRLOC /** * SECTION: yts-proxy-service diff --git a/ytstenut/yts-proxy.c b/ytstenut/yts-proxy.c index ca91f34..db7fca0 100644 --- a/ytstenut/yts-proxy.c +++ b/ytstenut/yts-proxy.c @@ -35,7 +35,7 @@ G_DEFINE_ABSTRACT_TYPE_WITH_CODE (YtsProxy, _capability_interface_init)) #undef G_LOG_DOMAIN -#define G_LOG_DOMAIN PACKAGE"\0proxy" +#define G_LOG_DOMAIN PACKAGE"\0proxy\0"G_STRLOC /** * SECTION: yts-proxy diff --git a/ytstenut/yts-roster.c b/ytstenut/yts-roster.c index 27c234a..284fc31 100644 --- a/ytstenut/yts-roster.c +++ b/ytstenut/yts-roster.c @@ -34,7 +34,7 @@ G_DEFINE_TYPE (YtsRoster, yts_roster, G_TYPE_OBJECT); (G_TYPE_INSTANCE_GET_PRIVATE ((o), YTS_TYPE_ROSTER, YtsRosterPrivate)) #undef G_LOG_DOMAIN -#define G_LOG_DOMAIN PACKAGE"\0roster" +#define G_LOG_DOMAIN PACKAGE"\0roster\0"G_STRLOC /** * SECTION: yts-roster diff --git a/ytstenut/yts-service-adapter.c b/ytstenut/yts-service-adapter.c index db463b4..eeaa88f 100644 --- a/ytstenut/yts-service-adapter.c +++ b/ytstenut/yts-service-adapter.c @@ -26,7 +26,7 @@ G_DEFINE_TYPE (YtsServiceAdapter, yts_service_adapter, G_TYPE_OBJECT) #undef G_LOG_DOMAIN -#define G_LOG_DOMAIN PACKAGE"\0service-adapter" +#define G_LOG_DOMAIN PACKAGE"\0service-adapter\0"G_STRLOC enum { PROP_0, diff --git a/ytstenut/yts-service.c b/ytstenut/yts-service.c index a01d480..8ef84ea 100644 --- a/ytstenut/yts-service.c +++ b/ytstenut/yts-service.c @@ -47,7 +47,7 @@ G_DEFINE_TYPE_WITH_CODE (YtsService, (G_TYPE_INSTANCE_GET_PRIVATE ((o), YTS_TYPE_SERVICE, YtsServicePrivate)) #undef G_LOG_DOMAIN -#define G_LOG_DOMAIN PACKAGE"\0service" +#define G_LOG_DOMAIN PACKAGE"\0service\0"G_STRLOC enum { PROP_0, diff --git a/ytstenut/ytstenut-internal.h b/ytstenut/ytstenut-internal.h index 6d2080a..d7f518d 100644 --- a/ytstenut/ytstenut-internal.h +++ b/ytstenut/ytstenut-internal.h @@ -24,17 +24,18 @@ #include <stdbool.h> typedef enum { - YTS_DEBUG_CLIENT = 1 << 0, - YTS_DEBUG_CONTACT = 1 << 1, - YTS_DEBUG_FILE_TRANSFER = 1 << 2, - YTS_DEBUG_MAIN = 1 << 3, - YTS_DEBUG_PROXY = 1 << 4, - YTS_DEBUG_PROXY_SERVICE = 1 << 5, - YTS_DEBUG_ROSTER = 1 << 6, - YTS_DEBUG_SERVICE_ADAPTER = 1 << 7, - YTS_DEBUG_SERVICE = 1 << 8, - YTS_DEBUG_TELEPATHY = 1 << 9, - YTS_DEBUG_UNSPECIFIED = 1 << 10 + YTS_DEBUG_BRIEF = 1 << 0, /* Brief debug output. */ + YTS_DEBUG_CLIENT = 1 << 1, + YTS_DEBUG_CONTACT = 1 << 2, + YTS_DEBUG_FILE_TRANSFER = 1 << 3, + YTS_DEBUG_MAIN = 1 << 4, + YTS_DEBUG_PROXY = 1 << 5, + YTS_DEBUG_PROXY_SERVICE = 1 << 6, + YTS_DEBUG_ROSTER = 1 << 7, + YTS_DEBUG_SERVICE_ADAPTER = 1 << 8, + YTS_DEBUG_SERVICE = 1 << 9, + YTS_DEBUG_TELEPATHY = 1 << 10, + YTS_DEBUG_UNSPECIFIED = 1 << 11 } YtsDebugFlags; bool diff --git a/ytstenut/ytstenut.c b/ytstenut/ytstenut.c index 143cba8..d5ebbd4 100644 --- a/ytstenut/ytstenut.c +++ b/ytstenut/ytstenut.c @@ -18,6 +18,7 @@ * Authored by: Rob Staudinger <robsta@linux.intel.com> */ +#include <stdio.h> #include <string.h> #include <glib.h> @@ -25,9 +26,10 @@ #include "config.h" #undef G_LOG_DOMAIN -#define G_LOG_DOMAIN PACKAGE"\0main" +#define G_LOG_DOMAIN PACKAGE"\0main\0"G_STRLOC static const GDebugKey _debug_keys[] = { + { "brief", YTS_DEBUG_BRIEF }, /* Brief debug output. */ { "client", YTS_DEBUG_CLIENT }, { "contact", YTS_DEBUG_CONTACT }, { "file-transfer", YTS_DEBUG_FILE_TRANSFER }, @@ -44,22 +46,75 @@ static const GDebugKey _debug_keys[] = { static YtsDebugFlags _debug_flags = 0; static void -_log (char const *log_domain, - GLogLevelFlags log_level, - char const *message, - gpointer test_topic) +log_brief (GLogLevelFlags log_level, + char const *context, + char const *message) +{ + char const *level; + + if (G_LOG_LEVEL_ERROR & log_level) + level = "error"; + else if (G_LOG_LEVEL_CRITICAL & log_level) + level = "critical"; + else if (G_LOG_LEVEL_WARNING & log_level) + level = "warning"; + else if (G_LOG_LEVEL_MESSAGE & log_level) + level = "message"; + else if (G_LOG_LEVEL_INFO & log_level) + level = "info"; + else if (G_LOG_LEVEL_DEBUG & log_level) + level = "debug"; + else + level = "unknown"; + + fprintf (stderr, "%s: %s: %s\n", context, level, message); +} + +static void +log_default (char const *log_domain, + GLogLevelFlags log_level, + char const *topic, + char const *location, + char const *message_) +{ + char *message; + + /* Prepend context to message. */ + if (location) { + message = g_strdup_printf ("%s: %s", location, message_); + } else { + message = g_strdup_printf ("%s: %s", topic, message_); + } + + g_log_default_handler (log_domain, log_level, message, NULL); + g_free (message); +} + +static void +_log_handler (char const *log_domain, + GLogLevelFlags log_level, + char const *message, + gpointer user_data) { char const *topic; + char const *location = NULL; unsigned topic_flag = 0; /* A bit of a hack, the domain has the format "ytstenut\0topic", * so look what's past the \0 for the topic. */ topic = &log_domain[strlen (log_domain) + 1]; + if (0 != g_strcmp0 ("unspecified", topic)) { + location = &topic[strlen (topic) + 1]; + if (location[0] == '.' && location[1] == '/') + location += 2; + } + if (_debug_flags) { - /* We are in debug mode. */ - for (unsigned i = 0; i < G_N_ELEMENTS (_debug_keys); i++) { + /* == We are in debug mode. == */ + + for (unsigned i = 1; i < G_N_ELEMENTS (_debug_keys); i++) { if (0 == g_strcmp0 (topic, _debug_keys[i].key)) { topic_flag = _debug_keys[i].value; break; @@ -68,45 +123,30 @@ _log (char const *log_domain, if (_debug_flags & topic_flag) { - char *msg = NULL; + if (YTS_DEBUG_BRIEF & _debug_flags) { - /* Prepend topic on the message, if we have one. */ - if (0 != g_strcmp0 ("unspecified", topic)) { - msg = g_strdup_printf ("%s: %s", topic, message); - } + log_brief (log_level, + location ? location : topic, + message); - g_log_default_handler (log_domain, log_level, - msg ? msg : message, - NULL); + } else { - if (msg) - g_free (msg); + log_default (log_domain, log_level, topic, location, message); + } } } else { - /* Not in debug mode, only print debug messages - * (there should not be any in a tarball release), and exceptions. */ + /* == Not in debug mode == + * Only print debug messages (there should not be any in a tarball release), + * and exceptions. */ GLogLevelFlags log_mask = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_DEBUG; if (log_level & log_mask) { - - char *msg = NULL; - - /* Prepend topic on the message, if we have one. */ - if (0 != g_strcmp0 ("unspecified", topic)) { - msg = g_strdup_printf ("%s: %s", topic, message); - } - - g_log_default_handler (log_domain, log_level, - msg ? msg : message, - NULL); - - if (msg) - g_free (msg); + log_default (log_domain, log_level, topic, location, message); } } } @@ -130,7 +170,7 @@ ytstenut_init (void) G_N_ELEMENTS (_debug_keys)); } - g_log_set_handler (PACKAGE, G_LOG_LEVEL_MASK, _log, NULL); + g_log_set_handler (PACKAGE, G_LOG_LEVEL_MASK, _log_handler, NULL); is_initialized = true; return true; @@ -142,3 +182,21 @@ ytstenut_get_debug_flags (void) return _debug_flags; } +#if 0 + +/* This is just for testing the above code standalone. */ + +int +main (int argc, + char **argv) +{ + ytstenut_init (); + + g_warning ("foo"); + + printf ("=== %s\n", FOO_LOG_DOMAIN); + + return 0; +} +#endif + |