Add the ContextMenuHandlers key for shortcuts so the new context menu
[wine/multimedia.git] / libs / wine / debug.c
blobf6ee71b67228104bff852160751095012a94a650
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 static const char * const debug_classes[] = { "fixme", "err", "warn", "trace" };
36 #define MAX_DEBUG_OPTIONS 256
38 static unsigned char default_flags = (1 << __WINE_DBCL_ERR) | (1 << __WINE_DBCL_FIXME);
39 static unsigned int nb_debug_options = 0;
40 static struct __wine_debug_channel debug_options[MAX_DEBUG_OPTIONS];
42 static struct __wine_debug_functions funcs;
44 static int cmp_name( const void *p1, const void *p2 )
46 const char *name = p1;
47 const struct __wine_debug_channel *chan = p2;
48 return strcmp( name, chan->name );
51 /* get the flags to use for a given channel, possibly setting them too in case of lazy init */
52 unsigned char __wine_dbg_get_channel_flags( struct __wine_debug_channel *channel )
54 if (nb_debug_options)
56 struct __wine_debug_channel *opt = bsearch( channel->name, debug_options, nb_debug_options,
57 sizeof(debug_options[0]), cmp_name );
58 if (opt) return opt->flags;
60 /* no option for this channel */
61 if (channel->flags & (1 << __WINE_DBCL_INIT)) channel->flags = default_flags;
62 return default_flags;
65 /* set the flags to use for a given channel; return 0 if the channel is not available to set */
66 int __wine_dbg_set_channel_flags( struct __wine_debug_channel *channel,
67 unsigned char set, unsigned char clear )
69 if (nb_debug_options)
71 struct __wine_debug_channel *opt = bsearch( channel->name, debug_options, nb_debug_options,
72 sizeof(debug_options[0]), cmp_name );
73 if (opt)
75 opt->flags = (opt->flags & ~clear) | set;
76 return 1;
79 return 0;
82 /* add a new debug option at the end of the option list */
83 static void add_option( const char *name, unsigned char set, unsigned char clear )
85 int min = 0, max = nb_debug_options - 1, pos, res;
87 if (!name[0]) /* "all" option */
89 default_flags = (default_flags & ~clear) | set;
90 return;
92 if (strlen(name) >= sizeof(debug_options[0].name)) return;
94 while (min <= max)
96 pos = (min + max) / 2;
97 res = strcmp( name, debug_options[pos].name );
98 if (!res)
100 debug_options[pos].flags = (debug_options[pos].flags & ~clear) | set;
101 return;
103 if (res < 0) max = pos - 1;
104 else min = pos + 1;
106 if (nb_debug_options >= MAX_DEBUG_OPTIONS) return;
108 pos = min;
109 if (pos < nb_debug_options) memmove( &debug_options[pos + 1], &debug_options[pos],
110 (nb_debug_options - pos) * sizeof(debug_options[0]) );
111 strcpy( debug_options[pos].name, name );
112 debug_options[pos].flags = (default_flags & ~clear) | set;
113 nb_debug_options++;
116 /* parse a set of debugging option specifications and add them to the option list */
117 static void parse_options( const char *str )
119 char *opt, *next, *options;
120 unsigned int i;
122 if (!(options = strdup(str))) return;
123 for (opt = options; opt; opt = next)
125 const char *p;
126 unsigned char set = 0, clear = 0;
128 if ((next = strchr( opt, ',' ))) *next++ = 0;
130 p = opt + strcspn( opt, "+-" );
131 if (!p[0]) p = opt; /* assume it's a debug channel name */
133 if (p > opt)
135 for (i = 0; i < sizeof(debug_classes)/sizeof(debug_classes[0]); i++)
137 int len = strlen(debug_classes[i]);
138 if (len != (p - opt)) continue;
139 if (!memcmp( opt, debug_classes[i], len )) /* found it */
141 if (*p == '+') set |= 1 << i;
142 else clear |= 1 << i;
143 break;
146 if (i == sizeof(debug_classes)/sizeof(debug_classes[0])) /* bad class name, skip it */
147 continue;
149 else
151 if (*p == '-') clear = ~0;
152 else set = ~0;
154 if (*p == '+' || *p == '-') p++;
155 if (!p[0]) continue;
157 if (!strcmp( p, "all" ))
158 default_flags = (default_flags & ~clear) | set;
159 else
160 add_option( p, set, clear );
162 free( options );
166 /* print the usage message */
167 static void debug_usage(void)
169 static const char usage[] =
170 "Syntax of the WINEDEBUG variable:\n"
171 " WINEDEBUG=[class]+xxx,[class]-yyy,...\n\n"
172 "Example: WINEDEBUG=+all,warn-heap\n"
173 " turns on all messages except warning heap messages\n"
174 "Available message classes: err, warn, fixme, trace\n";
175 write( 2, usage, sizeof(usage) - 1 );
176 exit(1);
180 /* initialize all options at startup */
181 void debug_init(void)
183 char *wine_debug;
185 if ((wine_debug = getenv("WINEDEBUG")))
187 if (!strcmp( wine_debug, "help" )) debug_usage();
188 parse_options( wine_debug );
192 /* varargs wrapper for funcs.dbg_vprintf */
193 int wine_dbg_printf( const char *format, ... )
195 int ret;
196 va_list valist;
198 va_start(valist, format);
199 ret = funcs.dbg_vprintf( format, valist );
200 va_end(valist);
201 return ret;
204 /* printf with temp buffer allocation */
205 const char *wine_dbg_sprintf( const char *format, ... )
207 static const int max_size = 200;
208 char *ret;
209 int len;
210 va_list valist;
212 va_start(valist, format);
213 ret = funcs.get_temp_buffer( max_size );
214 len = vsnprintf( ret, max_size, format, valist );
215 if (len == -1 || len >= max_size) ret[max_size-1] = 0;
216 else funcs.release_temp_buffer( ret, len + 1 );
217 va_end(valist);
218 return ret;
222 /* varargs wrapper for funcs.dbg_vlog */
223 int wine_dbg_log( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
224 const char *func, const char *format, ... )
226 int ret;
227 va_list valist;
229 if (!(__wine_dbg_get_channel_flags( channel ) & (1 << cls))) return -1;
231 va_start(valist, format);
232 ret = funcs.dbg_vlog( cls, channel, func, format, valist );
233 va_end(valist);
234 return ret;
238 /* allocate some tmp string space */
239 /* FIXME: this is not 100% thread-safe */
240 static char *get_temp_buffer( size_t size )
242 static char *list[32];
243 static int pos;
244 char *ret;
245 int idx;
247 idx = interlocked_xchg_add( &pos, 1 ) % (sizeof(list)/sizeof(list[0]));
248 if ((ret = realloc( list[idx], size ))) list[idx] = ret;
249 return ret;
253 /* release unused part of the buffer */
254 static void release_temp_buffer( char *buffer, size_t size )
256 /* don't bother doing anything */
260 /* default implementation of wine_dbgstr_an */
261 static const char *default_dbgstr_an( const char *str, int n )
263 static const char hex[16] = "0123456789abcdef";
264 char *dst, *res;
265 size_t size;
267 if (!((ULONG_PTR)str >> 16))
269 if (!str) return "(null)";
270 res = funcs.get_temp_buffer( 6 );
271 sprintf( res, "#%04x", LOWORD(str) );
272 return res;
274 if (n == -1) n = strlen(str);
275 if (n < 0) n = 0;
276 size = 10 + min( 300, n * 4 );
277 dst = res = funcs.get_temp_buffer( size );
278 *dst++ = '"';
279 while (n-- > 0 && dst <= res + size - 9)
281 unsigned char c = *str++;
282 switch (c)
284 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
285 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
286 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
287 case '"': *dst++ = '\\'; *dst++ = '"'; break;
288 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
289 default:
290 if (c >= ' ' && c <= 126)
291 *dst++ = c;
292 else
294 *dst++ = '\\';
295 *dst++ = 'x';
296 *dst++ = hex[(c >> 4) & 0x0f];
297 *dst++ = hex[c & 0x0f];
301 *dst++ = '"';
302 if (*str)
304 *dst++ = '.';
305 *dst++ = '.';
306 *dst++ = '.';
308 *dst++ = 0;
309 funcs.release_temp_buffer( res, dst - res );
310 return res;
314 /* default implementation of wine_dbgstr_wn */
315 static const char *default_dbgstr_wn( const WCHAR *str, int n )
317 char *dst, *res;
318 size_t size;
320 if (!((ULONG_PTR)str >> 16))
322 if (!str) return "(null)";
323 res = funcs.get_temp_buffer( 6 );
324 sprintf( res, "#%04x", LOWORD(str) );
325 return res;
327 if (n == -1) n = strlenW(str);
328 if (n < 0) n = 0;
329 size = 12 + min( 300, n * 5 );
330 dst = res = funcs.get_temp_buffer( n * 5 + 7 );
331 *dst++ = 'L';
332 *dst++ = '"';
333 while (n-- > 0 && dst <= res + size - 10)
335 WCHAR c = *str++;
336 switch (c)
338 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
339 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
340 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
341 case '"': *dst++ = '\\'; *dst++ = '"'; break;
342 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
343 default:
344 if (c >= ' ' && c <= 126)
345 *dst++ = c;
346 else
348 *dst++ = '\\';
349 sprintf(dst,"%04x",c);
350 dst+=4;
354 *dst++ = '"';
355 if (*str)
357 *dst++ = '.';
358 *dst++ = '.';
359 *dst++ = '.';
361 *dst++ = 0;
362 funcs.release_temp_buffer( res, dst - res );
363 return res;
367 /* default implementation of wine_dbg_vprintf */
368 static int default_dbg_vprintf( const char *format, va_list args )
370 return vfprintf( stderr, format, args );
374 /* default implementation of wine_dbg_vlog */
375 static int default_dbg_vlog( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
376 const char *func, const char *format, va_list args )
378 int ret = 0;
380 if (cls < sizeof(debug_classes)/sizeof(debug_classes[0]))
381 ret += wine_dbg_printf( "%s:%s:%s ", debug_classes[cls], channel->name, func );
382 if (format)
383 ret += funcs.dbg_vprintf( format, args );
384 return ret;
387 /* wrappers to use the function pointers */
389 const char *wine_dbgstr_an( const char * s, int n )
391 return funcs.dbgstr_an(s, n);
394 const char *wine_dbgstr_wn( const WCHAR *s, int n )
396 return funcs.dbgstr_wn(s, n);
399 void __wine_dbg_set_functions( const struct __wine_debug_functions *new_funcs,
400 struct __wine_debug_functions *old_funcs, size_t size )
402 if (old_funcs) memcpy( old_funcs, &funcs, min(sizeof(funcs),size) );
403 if (new_funcs) memcpy( &funcs, new_funcs, min(sizeof(funcs),size) );
406 static struct __wine_debug_functions funcs =
408 get_temp_buffer,
409 release_temp_buffer,
410 default_dbgstr_an,
411 default_dbgstr_wn,
412 default_dbg_vprintf,
413 default_dbg_vlog