08ffb0784db55fac388cd343bff858c8d951fa36
[platform/upstream/libnice.git] / agent / debug.c
1 /*
2  * This file is part of the Nice GLib ICE library.
3  *
4  * (C) 2008 Collabora Ltd.
5  *  Contact: Youness Alaoui
6  * (C) 2008 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
43 #include "debug.h"
44
45 #include "stunagent.h"
46 #include "pseudotcp.h"
47
48 #include "agent-priv.h"
49
50 static int debug_enabled = 0;
51 static int debug_verbose_enabled = 0;
52
53 #define NICE_DEBUG_STUN 1
54 #define NICE_DEBUG_NICE 2
55 #define NICE_DEBUG_PSEUDOTCP 4
56 #define NICE_DEBUG_PSEUDOTCP_VERBOSE 8
57 #define NICE_DEBUG_NICE_VERBOSE 16
58
59 static const GDebugKey keys[] = {
60   { (gchar *)"stun",  NICE_DEBUG_STUN },
61   { (gchar *)"nice",  NICE_DEBUG_NICE },
62   { (gchar *)"pseudotcp",  NICE_DEBUG_PSEUDOTCP },
63   { (gchar *)"pseudotcp-verbose",  NICE_DEBUG_PSEUDOTCP_VERBOSE },
64   { (gchar *)"nice-verbose",  NICE_DEBUG_NICE_VERBOSE }
65 };
66
67 static const GDebugKey gkeys[] = {
68   { (gchar *)"libnice-stun",  NICE_DEBUG_STUN },
69   { (gchar *)"libnice",  NICE_DEBUG_NICE },
70   { (gchar *)"libnice-pseudotcp",  NICE_DEBUG_PSEUDOTCP },
71   { (gchar *)"libnice-pseudotcp-verbose",  NICE_DEBUG_PSEUDOTCP_VERBOSE },
72   { (gchar *)"libnice-verbose",  NICE_DEBUG_NICE_VERBOSE }
73 };
74
75 static void
76 stun_handler (const char *format, va_list ap) G_GNUC_PRINTF (1, 0);
77
78 static void
79 stun_handler (const char *format, va_list ap)
80 {
81   g_logv ("libnice-stun", G_LOG_LEVEL_DEBUG, format, ap);
82 }
83
84 void nice_debug_init (void)
85 {
86   static gboolean debug_initialized = FALSE;
87   const gchar *flags_string;
88   const gchar *gflags_string;
89   guint flags = 0;
90
91   if (!debug_initialized) {
92     debug_initialized = TRUE;
93
94     flags_string = g_getenv ("NICE_DEBUG");
95     gflags_string = g_getenv ("G_MESSAGES_DEBUG");
96
97     if (flags_string)
98       flags = g_parse_debug_string (flags_string, keys, G_N_ELEMENTS (keys));
99     if (gflags_string)
100       flags |= g_parse_debug_string (gflags_string, gkeys, G_N_ELEMENTS (gkeys));
101     if (gflags_string && strstr (gflags_string, "libnice-pseudotcp-verbose"))
102       flags |= NICE_DEBUG_PSEUDOTCP_VERBOSE;
103     if (gflags_string && strstr (gflags_string, "libnice-verbose")) {
104       flags |= NICE_DEBUG_NICE_VERBOSE;
105     }
106
107     stun_set_debug_handler (stun_handler);
108     debug_enabled = !!(flags & NICE_DEBUG_NICE);
109     if (flags & NICE_DEBUG_STUN)
110       stun_debug_enable ();
111     else
112       stun_debug_disable ();
113
114     if (flags & NICE_DEBUG_NICE_VERBOSE)
115       debug_verbose_enabled = TRUE;
116
117     /* Set verbose before normal so that if we use 'all', then only
118        normal debug is enabled, we'd need to set pseudotcp-verbose without the
119        pseudotcp flag in order to actually enable verbose pseudotcp */
120     if (flags & NICE_DEBUG_PSEUDOTCP_VERBOSE)
121       pseudo_tcp_set_debug_level (PSEUDO_TCP_DEBUG_VERBOSE);
122     else if (flags & NICE_DEBUG_PSEUDOTCP)
123       pseudo_tcp_set_debug_level (PSEUDO_TCP_DEBUG_NORMAL);
124   }
125 }
126
127 #ifndef NDEBUG
128 gboolean nice_debug_is_enabled (void)
129 {
130   return debug_enabled;
131 }
132 gboolean nice_debug_is_verbose (void)
133 {
134   return debug_verbose_enabled;
135 }
136 #else
137 /* Defined in agent-priv.h. */
138 #endif
139
140 void nice_debug_enable (gboolean with_stun)
141 {
142   nice_debug_init ();
143   debug_enabled = 1;
144   if (with_stun)
145     stun_debug_enable ();
146 }
147 void nice_debug_disable (gboolean with_stun)
148 {
149   nice_debug_init ();
150   debug_enabled = 0;
151   if (with_stun)
152     stun_debug_disable ();
153 }
154
155 #ifndef NDEBUG
156 void nice_debug (const char *fmt, ...)
157 {
158   va_list ap;
159   if (debug_enabled) {
160     va_start (ap, fmt);
161     g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, fmt, ap);
162     va_end (ap);
163   }
164 }
165 void nice_debug_verbose (const char *fmt, ...)
166 {
167   va_list ap;
168   if (debug_enabled && debug_verbose_enabled) {
169     va_start (ap, fmt);
170     g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, fmt, ap);
171     va_end (ap);
172   }
173 }
174 #else
175 /* Defined in agent-priv.h. */
176 #endif