Skip to content

Commit

Permalink
call log window functions indirectly via idle callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
nadvornik committed Nov 12, 2011
1 parent 04bb6f2 commit 4cbe6b6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
30 changes: 21 additions & 9 deletions src/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,43 @@

#include <glib/gprintf.h>

GMutex *debug_mutex;
/*
* Logging functions
*/

gint log_domain_printf(const gchar *domain, const gchar *format, ...)
static gboolean log_msg_cb(gpointer data)
{
gchar *buf = data;
log_window_append(buf, LOG_MSG);
g_free(buf);
return FALSE;
}

static gboolean log_normal_cb(gpointer data)
{
gchar *buf = data;
log_window_append(buf, LOG_NORMAL);
g_free(buf);
return FALSE;
}

void log_domain_printf(const gchar *domain, const gchar *format, ...)
{
va_list ap;
gchar buf[4096];
gint ret;
gchar *buf;

va_start(ap, format);
ret = g_vsnprintf(buf, sizeof(buf), format, ap);
buf = g_strdup_vprintf(format, ap);
va_end(ap);

print_term(buf);
if (strcmp(domain, DOMAIN_INFO) == 0)
log_window_append(buf, LOG_NORMAL);
g_idle_add(log_normal_cb, buf);
else
log_window_append(buf, LOG_MSG);
g_idle_add(log_msg_cb, buf);

return ret;
}


/*
* Debugging only functions
*/
Expand Down
6 changes: 1 addition & 5 deletions src/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
#define DOMAIN_DEBUG "debug"
#define DOMAIN_INFO "info"

extern GMutex *debug_mutex;

gint log_domain_printf(const gchar *domain, const gchar *format, ...) G_GNUC_PRINTF(2, 3);
void log_domain_printf(const gchar *domain, const gchar *format, ...) G_GNUC_PRINTF(2, 3);
#define log_printf(...) log_domain_printf(DOMAIN_INFO, __VA_ARGS__)

#ifdef DEBUG
Expand All @@ -39,11 +37,9 @@ void init_exec_time(void);
gint debug_level = get_debug_level(); \
if (debug_level >= (n)) \
{ \
g_mutex_lock(debug_mutex); \
if (debug_level != 1) log_domain_printf(DOMAIN_DEBUG, "%s:%d: ", __FILE__, __LINE__); \
log_domain_printf(DOMAIN_DEBUG, __VA_ARGS__); \
log_domain_printf(DOMAIN_DEBUG, "\n"); \
g_mutex_unlock(debug_mutex); \
} \
} while (0)

Expand Down
1 change: 0 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,6 @@ gint main(gint argc, gchar *argv[])
g_thread_init(NULL);
gdk_threads_init();
gdk_threads_enter();
debug_mutex = g_mutex_new();

#endif

Expand Down

0 comments on commit 4cbe6b6

Please sign in to comment.