From b3626701ada6b4b00bf86187ec1768471f12a6e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Cr=C3=AAte?= Date: Wed, 1 Sep 2010 13:37:22 -0400 Subject: [PATCH] Make TpDebugSender's g_log handler thread safe g_log can be called from any thread, but telepathy-glib is not thread safe in any way. So we have to push the incoming messages to the main thread using g_idle_add() before doing anything with them --- telepathy-glib/debug-sender.c | 55 +++++++++++++++++++++++++++++++++++------ 1 files changed, 47 insertions(+), 8 deletions(-) diff --git a/telepathy-glib/debug-sender.c b/telepathy-glib/debug-sender.c index 75aa282..ae3fcb2 100644 --- a/telepathy-glib/debug-sender.c +++ b/telepathy-glib/debug-sender.c @@ -392,6 +392,42 @@ tp_debug_sender_add_message (TpDebugSender *self, } } +static gboolean +add_message_idle (gpointer data) +{ + DebugMessage *new_msg = data; + TpDebugSender *self; + + if (debug_sender == NULL) + goto ignore_message; + + self = TP_DEBUG_SENDER (debug_sender); + + if (g_queue_get_length (self->priv->messages) >= DEBUG_MESSAGE_LIMIT) + { + DebugMessage *old_head = + (DebugMessage *) g_queue_pop_head (self->priv->messages); + + debug_message_free (old_head); + } + + g_queue_push_tail (self->priv->messages, new_msg); + + if (self->priv->enabled) + { + tp_svc_debug_emit_new_debug_message (self, new_msg->timestamp, + new_msg->domain, new_msg->level, new_msg->string); + } + + return FALSE; + + ignore_message: + + debug_message_free (new_msg); + return FALSE; + +} + /** * tp_debug_sender_log_handler: * @log_domain: domain of the message @@ -447,17 +483,20 @@ tp_debug_sender_log_handler (const gchar *log_domain, const gchar *message, gpointer exclude) { + DebugMessage *new_msg; + GTimeVal now; + g_log_default_handler (log_domain, log_level, message, NULL); - if (debug_sender == NULL) + if (exclude && tp_strdiff (log_domain, exclude)) return; - if (exclude == NULL || tp_strdiff (log_domain, exclude)) - { - GTimeVal now; - g_get_current_time (&now); + g_get_current_time (&now); + new_msg = debug_message_new (&now, log_domain, log_level, message); - tp_debug_sender_add_message (debug_sender, &now, log_domain, log_level, - message); - } + /* This could be called from any thread, but since Telepathy-GLib is not + * in any way thread safe, we must send it to the main loop to be + * processedx + */ + g_idle_add (add_message_idle, new_msg); } -- 1.7.2.2