diff options
author | Olivier CrĂȘte <olivier.crete@collabora.com> | 2014-07-23 22:24:27 +0100 |
---|---|---|
committer | Olivier CrĂȘte <olivier.crete@collabora.com> | 2014-07-23 22:45:41 +0100 |
commit | 7dea3231842feaac966c6d9b7ae095b17d07060f (patch) | |
tree | ca767904a5faeafbf1cd952b740fdd0c17653418 /stun/debug.c | |
parent | 5ab7c88349008136ebf847b6bc7dfb31d13bfa70 (diff) | |
download | libnice-7dea3231842feaac966c6d9b7ae095b17d07060f.tar.gz |
debug: Make debug messages ready for g_log()
Send thing one line at a time, so no explicit \n
Also make it possible to set a log handler explicitly
Diffstat (limited to 'stun/debug.c')
-rw-r--r-- | stun/debug.c | 37 |
1 files changed, 33 insertions, 4 deletions
diff --git a/stun/debug.c b/stun/debug.c index 24a3732..598094d 100644 --- a/stun/debug.c +++ b/stun/debug.c @@ -55,22 +55,51 @@ void stun_debug_disable (void) { debug_enabled = 0; } + +static void +default_handler (const char *format, va_list ap) +{ + vfprintf (stderr, format, ap); + fprintf (stderr, "\n"); +} + +static StunDebugHandler handler = default_handler; + void stun_debug (const char *fmt, ...) { va_list ap; if (debug_enabled) { va_start (ap, fmt); - vfprintf (stderr, fmt, ap); + handler (fmt, ap); va_end (ap); } } -void stun_debug_bytes (const void *data, size_t len) +void stun_debug_bytes (const char *prefix, const void *data, size_t len) { size_t i; + size_t prefix_len = strlen (prefix); + char bytes[prefix_len + 2 + (len * 2) + 1]; + + if (!debug_enabled) + return; + + bytes[0] = 0; + strcpy (bytes, prefix); + strcpy (bytes + prefix_len, "0x"); - stun_debug ("0x"); for (i = 0; i < len; i++) - stun_debug ("%02x", ((const unsigned char *)data)[i]); + snprintf (bytes + prefix_len + 2 + (i * 2), 3, "%02x", ((const unsigned char *)data)[i]); + + stun_debug ("%s", bytes); +} + + +void stun_set_debug_handler (StunDebugHandler _handler) +{ + if (_handler == NULL) + _handler = default_handler; + + handler = _handler; } |