9d3d59b48150101462e2a58b0f2e38a7a90f7412
[platform/upstream/libnice.git] / stun / debug.c
1 /*
2  * This file is part of the Nice GLib ICE library.
3  *
4  * (C) 2008-2009 Collabora Ltd.
5  *  Contact: Youness Alaoui
6  * (C) 2007 Nokia Corporation. All rights reserved.
7  *
8  * The contents of this file are subject to the Mozilla Public License Version
9  * 1.1 (the "License"); you may not use this file except in compliance with
10  * the License. You may obtain a copy of the License at
11  * http://www.mozilla.org/MPL/
12  *
13  * Software distributed under the License is distributed on an "AS IS" basis,
14  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
15  * for the specific language governing rights and limitations under the
16  * License.
17  *
18  * The Original Code is the Nice GLib ICE library.
19  *
20  * The Initial Developers of the Original Code are Collabora Ltd and Nokia
21  * Corporation. All Rights Reserved.
22  *
23  * Contributors:
24  *   Youness Alaoui, Collabora Ltd.
25  *
26  * Alternatively, the contents of this file may be used under the terms of the
27  * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
28  * case the provisions of LGPL are applicable instead of those above. If you
29  * wish to allow use of your version of this file only under the terms of the
30  * LGPL and not to allow others to use your version of this file under the
31  * MPL, indicate your decision by deleting the provisions above and replace
32  * them with the notice and other provisions required by the LGPL. If you do
33  * not delete the provisions above, a recipient may use your version of this
34  * file under either the MPL or the LGPL.
35  */
36
37 #ifdef HAVE_CONFIG_H
38 # include <config.h>
39 #endif
40
41 #include <string.h>
42 #include <stdlib.h>
43 #include <stdio.h>
44 #include <stdarg.h>
45
46 #include "debug.h"
47
48
49 static int debug_enabled = 0;
50
51 void stun_debug_enable (void) {
52   debug_enabled = 1;
53 }
54 void stun_debug_disable (void) {
55   debug_enabled = 0;
56 }
57
58 #if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
59 #define GNUC_PRINTF(format_idx, arg_idx) \
60   __attribute__((__format__ (__printf__, format_idx, arg_idx)))
61 #else
62 #define GNUC_PRINTF( format_idx, arg_idx)
63 #endif
64
65 static void
66 default_handler (const char *format, va_list ap) GNUC_PRINTF (1, 0);
67
68 static void
69 default_handler (const char *format, va_list ap)
70 {
71   vfprintf (stderr, format, ap);
72   fprintf (stderr, "\n");
73 }
74
75 static StunDebugHandler handler = default_handler;
76
77 void stun_debug (const char *fmt, ...)
78 {
79   va_list ap;
80   if (debug_enabled) {
81     va_start (ap, fmt);
82     handler (fmt, ap);
83     va_end (ap);
84   }
85 }
86
87 void stun_debug_bytes (const char *prefix, const void *data, size_t len)
88 {
89   size_t i;
90   size_t prefix_len = strlen (prefix);
91   char *bytes;
92   char *j;
93   unsigned char k;
94   const char *hex = "0123456789abcdef";
95
96   if (!debug_enabled)
97     return;
98
99   bytes = malloc (prefix_len + 2 + (len * 2) + 1);
100   bytes[0] = 0;
101   strcpy (bytes, prefix);
102   strcpy (bytes + prefix_len, "0x");
103
104   j = bytes + prefix_len + 2;
105   for (i = 0; i < len; i++) {
106     k = ((const unsigned char *)data)[i];
107     j[0] = hex[(k & 0xf0) >> 4];
108     j[1] = hex[k & 0xf];
109     j = j + 2;
110   }
111   j[0] = 0;
112   stun_debug ("%s", bytes);
113   free (bytes);
114 }
115
116
117 void stun_set_debug_handler (StunDebugHandler _handler)
118 {
119   if (_handler == NULL)
120     _handler = default_handler;
121
122   handler = _handler;
123 }
124