Fixed incorrect labeling of question boxes.
[wine.git] / libs / wine / debug.c
blob7419cb216bde4c443fc5cad3cf6efb51ef19bb6c
1 /*
2 * Management of the debugging channels
4 * Copyright 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <ctype.h>
30 #include "wine/debug.h"
31 #include "wine/library.h"
32 #include "wine/unicode.h"
34 struct dll
36 struct dll *next; /* linked list of dlls */
37 struct dll *prev;
38 char * const *channels; /* array of channels */
39 int nb_channels; /* number of channels in array */
42 static struct dll *first_dll;
44 struct debug_option
46 struct debug_option *next; /* next option in list */
47 unsigned char set; /* bits to set */
48 unsigned char clear; /* bits to clear */
49 char name[14]; /* channel name, or empty for "all" */
52 static struct debug_option *first_option;
53 static struct debug_option *last_option;
55 static const char * const debug_classes[] = { "fixme", "err", "warn", "trace" };
57 static int cmp_name( const void *p1, const void *p2 )
59 const char *name = p1;
60 const char * const *chan = p2;
61 return strcmp( name, *chan + 1 );
64 /* apply a debug option to the channels of a given dll */
65 static void apply_option( struct dll *dll, const struct debug_option *opt )
67 if (opt->name[0])
69 char **dbch = bsearch( opt->name, dll->channels, dll->nb_channels,
70 sizeof(*dll->channels), cmp_name );
71 if (dbch) **dbch = (**dbch & ~opt->clear) | opt->set;
73 else /* all */
75 int i;
76 for (i = 0; i < dll->nb_channels; i++)
77 dll->channels[i][0] = (dll->channels[i][0] & ~opt->clear) | opt->set;
81 /* register a new set of channels for a dll */
82 void *__wine_dbg_register( char * const *channels, int nb )
84 struct debug_option *opt = first_option;
85 struct dll *dll = malloc( sizeof(*dll) );
86 if (dll)
88 dll->channels = channels;
89 dll->nb_channels = nb;
90 dll->prev = NULL;
91 if ((dll->next = first_dll)) dll->next->prev = dll;
92 first_dll = dll;
94 /* apply existing options to this dll */
95 while (opt)
97 apply_option( dll, opt );
98 opt = opt->next;
101 return dll;
105 /* unregister a set of channels; must pass the pointer obtained from wine_dbg_register */
106 void __wine_dbg_unregister( void *channel )
108 struct dll *dll = channel;
109 if (dll)
111 if (dll->next) dll->next->prev = dll->prev;
112 if (dll->prev) dll->prev->next = dll->next;
113 else first_dll = dll->next;
114 free( dll );
119 /* add a new debug option at the end of the option list */
120 void wine_dbg_add_option( const char *name, unsigned char set, unsigned char clear )
122 struct dll *dll = first_dll;
123 struct debug_option *opt;
125 if (!(opt = malloc( sizeof(*opt) ))) return;
126 opt->next = NULL;
127 opt->set = set;
128 opt->clear = clear;
129 strncpy( opt->name, name, sizeof(opt->name) );
130 opt->name[sizeof(opt->name)-1] = 0;
131 if (last_option) last_option->next = opt;
132 else first_option = opt;
133 last_option = opt;
135 /* apply option to all existing dlls */
136 while (dll)
138 apply_option( dll, opt );
139 dll = dll->next;
143 /* parse a set of debugging option specifications and add them to the option list */
144 int wine_dbg_parse_options( const char *str )
146 char *opt, *next, *options;
147 int i, errors = 0;
149 if (!(options = strdup(str))) return -1;
150 for (opt = options; opt; opt = next)
152 const char *p;
153 unsigned char set = 0, clear = 0;
155 if ((next = strchr( opt, ',' ))) *next++ = 0;
157 p = opt + strcspn( opt, "+-" );
158 if (!p[0] || !p[1]) /* bad option, skip it */
160 errors++;
161 continue;
164 if (p > opt)
166 for (i = 0; i < sizeof(debug_classes)/sizeof(debug_classes[0]); i++)
168 int len = strlen(debug_classes[i]);
169 if (len != (p - opt)) continue;
170 if (!memcmp( opt, debug_classes[i], len )) /* found it */
172 if (*p == '+') set |= 1 << i;
173 else clear |= 1 << i;
174 break;
177 if (i == sizeof(debug_classes)/sizeof(debug_classes[0])) /* bad class name, skip it */
179 errors++;
180 continue;
183 else
185 if (*p == '+') set = ~0;
186 else clear = ~0;
188 p++;
189 if (!strcmp( p, "all" )) p = ""; /* empty string means all */
190 wine_dbg_add_option( p, set, clear );
192 free( options );
193 return errors;
196 /* varargs wrapper for __wine_dbg_vprintf */
197 int wine_dbg_printf( const char *format, ... )
199 int ret;
200 va_list valist;
202 va_start(valist, format);
203 ret = __wine_dbg_vprintf( format, valist );
204 va_end(valist);
205 return ret;
209 /* varargs wrapper for __wine_dbg_vsprintf */
210 const char *wine_dbg_sprintf( const char *format, ... )
212 const char *ret;
213 va_list valist;
215 va_start(valist, format);
216 ret = __wine_dbg_vsprintf( format, valist );
217 va_end(valist);
218 return ret;
222 /* varargs wrapper for __wine_dbg_vlog */
223 int wine_dbg_log( unsigned int cls, const char *channel, const char *func, const char *format, ... )
225 int ret;
226 va_list valist;
228 va_start(valist, format);
229 ret = __wine_dbg_vlog( cls, channel, func, format, valist );
230 va_end(valist);
231 return ret;
235 /* allocate some tmp string space */
236 /* FIXME: this is not 100% thread-safe */
237 static char *get_tmp_space( int size )
239 static char *list[32];
240 static long pos;
241 char *ret;
242 int idx;
244 idx = interlocked_xchg_add( &pos, 1 ) % (sizeof(list)/sizeof(list[0]));
245 if ((ret = realloc( list[idx], size ))) list[idx] = ret;
246 return ret;
250 /* default implementation of wine_dbgstr_an */
251 static const char *default_dbgstr_an( const char *str, int n )
253 char *dst, *res;
255 if (!HIWORD(str))
257 if (!str) return "(null)";
258 res = get_tmp_space( 6 );
259 sprintf( res, "#%04x", LOWORD(str) );
260 return res;
262 if (n == -1) n = strlen(str);
263 if (n < 0) n = 0;
264 else if (n > 200) n = 200;
265 dst = res = get_tmp_space( n * 4 + 6 );
266 *dst++ = '"';
267 while (n-- > 0)
269 unsigned char c = *str++;
270 switch (c)
272 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
273 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
274 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
275 case '"': *dst++ = '\\'; *dst++ = '"'; break;
276 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
277 default:
278 if (c >= ' ' && c <= 126)
279 *dst++ = c;
280 else
282 *dst++ = '\\';
283 *dst++ = '0' + ((c >> 6) & 7);
284 *dst++ = '0' + ((c >> 3) & 7);
285 *dst++ = '0' + ((c >> 0) & 7);
289 *dst++ = '"';
290 if (*str)
292 *dst++ = '.';
293 *dst++ = '.';
294 *dst++ = '.';
296 *dst = 0;
297 return res;
301 /* default implementation of wine_dbgstr_wn */
302 static const char *default_dbgstr_wn( const WCHAR *str, int n )
304 char *dst, *res;
306 if (!HIWORD(str))
308 if (!str) return "(null)";
309 res = get_tmp_space( 6 );
310 sprintf( res, "#%04x", LOWORD(str) );
311 return res;
313 if (n == -1) n = strlenW(str);
314 if (n < 0) n = 0;
315 else if (n > 200) n = 200;
316 dst = res = get_tmp_space( n * 5 + 7 );
317 *dst++ = 'L';
318 *dst++ = '"';
319 while (n-- > 0)
321 WCHAR c = *str++;
322 switch (c)
324 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
325 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
326 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
327 case '"': *dst++ = '\\'; *dst++ = '"'; break;
328 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
329 default:
330 if (c >= ' ' && c <= 126)
331 *dst++ = c;
332 else
334 *dst++ = '\\';
335 sprintf(dst,"%04x",c);
336 dst+=4;
340 *dst++ = '"';
341 if (*str)
343 *dst++ = '.';
344 *dst++ = '.';
345 *dst++ = '.';
347 *dst = 0;
348 return res;
352 /* default implementation of wine_dbg_vsprintf */
353 static const char *default_dbg_vsprintf( const char *format, va_list args )
355 static const int max_size = 200;
357 char *res = get_tmp_space( max_size );
358 int len = vsnprintf( res, max_size, format, args );
359 if (len == -1 || len >= max_size) res[max_size-1] = 0;
360 return res;
363 /* default implementation of wine_dbg_vprintf */
364 static int default_dbg_vprintf( const char *format, va_list args )
366 return vfprintf( stderr, format, args );
370 /* default implementation of wine_dbg_vlog */
371 static int default_dbg_vlog( unsigned int cls, const char *channel, const char *func,
372 const char *format, va_list args )
374 int ret = 0;
376 if (cls < sizeof(debug_classes)/sizeof(debug_classes[0]))
377 ret += wine_dbg_printf( "%s:%s:%s ", debug_classes[cls], channel + 1, func );
378 if (format)
379 ret += __wine_dbg_vprintf( format, args );
380 return ret;
384 /* exported function pointers so that debugging functions can be redirected at run-time */
386 const char * (*__wine_dbgstr_an)( const char * s, int n ) = default_dbgstr_an;
387 const char * (*__wine_dbgstr_wn)( const WCHAR *s, int n ) = default_dbgstr_wn;
388 const char * (*__wine_dbg_vsprintf)( const char *format, va_list args ) = default_dbg_vsprintf;
389 int (*__wine_dbg_vprintf)( const char *format, va_list args ) = default_dbg_vprintf;
390 int (*__wine_dbg_vlog)( unsigned int cls, const char *channel, const char *function,
391 const char *format, va_list args ) = default_dbg_vlog;
393 /* wrappers to use the function pointers */
395 const char *wine_dbgstr_an( const char * s, int n )
397 return __wine_dbgstr_an(s, n);
400 const char *wine_dbgstr_wn( const WCHAR *s, int n )
402 return __wine_dbgstr_wn(s, n);
405 const char *wine_dbgstr_a( const char *s )
407 return __wine_dbgstr_an( s, -1 );
410 const char *wine_dbgstr_w( const WCHAR *s )
412 return __wine_dbgstr_wn( s, -1 );