diff options
| author | Emmanuele Bassi <ebassi@linux.intel.com> | 2010-03-18 15:21:29 +0000 |
|---|---|---|
| committer | Emmanuele Bassi <ebassi@linux.intel.com> | 2010-03-18 15:21:29 +0000 |
| commit | b3435c6a05ecee58c64dce669ce7e44f829afc98 (patch) | |
| tree | 03b6b727b29f44d2f029b00637b8ae099dcfadc0 | |
| parent | 88ac0d5111eb528e4c396e4c169ceee4fb046e62 (diff) | |
| download | json-glib-b3435c6a05ecee58c64dce669ce7e44f829afc98.tar.gz | |
Add debugging macros
Similarly to what GTK+ and Clutter do, we can use macros that evaluate
to nothing if JSON_ENABLE_DEBUG is disabled; they evaluate to messages
when the JSON_DEBUG environment variable is set to a debug domain.
| -rw-r--r-- | json-glib/Makefile.am | 4 | ||||
| -rw-r--r-- | json-glib/json-debug.c | 37 | ||||
| -rw-r--r-- | json-glib/json-debug.h | 45 |
3 files changed, 85 insertions, 1 deletions
diff --git a/json-glib/Makefile.am b/json-glib/Makefile.am index 8492be2..d83c735 100644 --- a/json-glib/Makefile.am +++ b/json-glib/Makefile.am @@ -37,13 +37,15 @@ source_h = \ $(NULL) source_h_private = \ - $(top_srcdir)/json-glib/json-scanner.h \ + $(top_srcdir)/json-glib/json-debug.h \ $(top_srcdir)/json-glib/json-gobject-private.h \ + $(top_srcdir)/json-glib/json-scanner.h \ $(top_srcdir)/json-glib/json-types-private.h \ $(NULL) source_c = \ $(srcdir)/json-array.c \ + $(srcdir)/json-debug.c \ $(srcdir)/json-gboxed.c \ $(srcdir)/json-generator.c \ $(srcdir)/json-gobject.c \ diff --git a/json-glib/json-debug.c b/json-glib/json-debug.c new file mode 100644 index 0000000..471d082 --- /dev/null +++ b/json-glib/json-debug.c @@ -0,0 +1,37 @@ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "json-debug.h" + +static unsigned int json_debug_flags = 0; +static gboolean json_debug_flags_set = FALSE; + +#ifdef JSON_ENABLE_DEBUG +static const GDebugKey json_debug_keys[] = { + { "parser", JSON_DEBUG_PARSER } +}; +#endif /* JSON_ENABLE_DEBUG */ + +JsonDebugFlags +_json_get_debug_flags (void) +{ +#ifdef JSON_ENABLE_DEBUG + const gchar *env_str; + + if (json_debug_flags_set) + return json_debug_flags; + + env_str = g_getenv ("JSON_DEBUG"); + if (env_str != NULL && *env_str != '\0') + { + json_debug_flags |= g_parse_debug_string (env_str, + json_debug_keys, + G_N_ELEMENTS (json_debug_keys)); + } + + json_debug_flags_set = TRUE; +#endif /* JSON_ENABLE_DEBUG */ + + return json_debug_flags; +} diff --git a/json-glib/json-debug.h b/json-glib/json-debug.h new file mode 100644 index 0000000..dc0d861 --- /dev/null +++ b/json-glib/json-debug.h @@ -0,0 +1,45 @@ +#ifndef __JSON_DEBUG_H__ +#define __JSON_DEBUG_H__ + +#include <glib.h> + +G_BEGIN_DECLS + +typedef enum { + JSON_DEBUG_PARSER = 1 << 0 +} JsonDebugFlags; + +#ifdef JSON_ENABLE_DEBUG + +# ifdef __GNUC__ + +# define JSON_NOTE(type,x,a...) G_STMT_START { \ + if (_json_get_debug_flags () & JSON_DEBUG_##type) { \ + g_message ("[" #type "] " G_STRLOC ": " x, ##a); \ + } } G_STMT_END + +# else +/* Try the C99 version; unfortunately, this does not allow us to pass + * empty arguments to the macro, which means we have to + * do an intemediate printf. + */ +# define JSON_NOTE(type,...) G_STMT_START { \ + if (_json_get_debug_flags () & JSON_DEBUG_##type) { \ + gchar * _fmt = g_strdup_printf (__VA_ARGS__); \ + g_message ("[" #type "] " G_STRLOC ": %s",_fmt); \ + g_free (_fmt); \ + } } G_STMT_END + +# endif /* __GNUC__ */ + +#else + +#define JSON_NOTE(type,...) G_STMT_START { } G_STMT_END + +#endif /* JSON_ENABLE_DEBUG */ + +JsonDebugFlags _json_get_debug_flags (void); + +G_END_DECLS + +#endif /* __JSON_DEBUG_H__ */ |
