62f7da5f2c9196942a4bf212c17780eb62c3521c
[reactos.git] / sdk / include / reactos / wine / debug.h
1 /*
2 * Wine debugging interface
3 *
4 * Copyright 1999 Patrik Stridvall
5 *
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.
10 *
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.
15 *
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #ifndef __WINE_DEBUG_H
22 #define __WINE_DEBUG_H
23
24 #include <stdarg.h>
25 #include <windef.h>
26 #ifndef GUID_DEFINED
27 #include <guiddef.h>
28 #endif
29
30 #include <builddir.h>
31
32 #ifdef __WINE_WINE_TEST_H
33 #error This file should not be used in Wine tests
34 #endif
35
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39
40 struct _GUID;
41
42 /*
43 * Internal definitions (do not use these directly)
44 */
45
46 enum __wine_debug_class
47 {
48 __WINE_DBCL_FIXME = 0, /* 0x1 */
49 __WINE_DBCL_ERR = 1, /* 0x2 */
50 __WINE_DBCL_WARN = 2, /* 0x4 */
51 __WINE_DBCL_TRACE = 3, /* 0x8 */
52
53 /* lazy init flag */
54 __WINE_DBCL_INIT = 0x7
55 };
56
57 struct __wine_debug_channel
58 {
59 unsigned char flags;
60 char name[15];
61 };
62
63 #define UNIMPLEMENTED WINE_FIXME("%s is UNIMPLEMENTED!\n", __FUNCTION__)
64
65 #ifndef WINE_NO_TRACE_MSGS
66 # define __WINE_GET_DEBUGGING_TRACE(dbch) ((dbch)->flags & (1 << __WINE_DBCL_TRACE))
67 #else
68 # define __WINE_GET_DEBUGGING_TRACE(dbch) 0
69 #endif
70
71 #ifndef WINE_NO_DEBUG_MSGS
72 # define __WINE_GET_DEBUGGING_WARN(dbch) ((dbch)->flags & (1 << __WINE_DBCL_WARN))
73 # define __WINE_GET_DEBUGGING_FIXME(dbch) ((dbch)->flags & (1 << __WINE_DBCL_FIXME))
74 #else
75 # define __WINE_GET_DEBUGGING_WARN(dbch) 0
76 # define __WINE_GET_DEBUGGING_FIXME(dbch) 0
77 #endif
78
79 /* define error macro regardless of what is configured */
80 #define __WINE_GET_DEBUGGING_ERR(dbch) ((dbch)->flags & (1 << __WINE_DBCL_ERR))
81
82 #define __WINE_GET_DEBUGGING(dbcl,dbch) __WINE_GET_DEBUGGING##dbcl(dbch)
83
84 #define __WINE_IS_DEBUG_ON(dbcl,dbch) \
85 (__WINE_GET_DEBUGGING##dbcl(dbch) && (__wine_dbg_get_channel_flags(dbch) & (1 << __WINE_DBCL##dbcl)))
86
87 #ifdef __GNUC__
88
89 #define __WINE_DPRINTF(dbcl,dbch) \
90 do { if(__WINE_GET_DEBUGGING(dbcl,(dbch))) { \
91 struct __wine_debug_channel * const __dbch = (dbch); \
92 const enum __wine_debug_class __dbcl = __WINE_DBCL##dbcl; \
93 __WINE_DBG_LOG
94
95 #define __WINE_DBG_LOG(args...) \
96 ros_dbg_log( __dbcl, __dbch, __RELFILE__, __FUNCTION__, __LINE__, args); } } while(0)
97
98 #define __WINE_PRINTF_ATTR(fmt,args) /*__attribute__((format (printf,fmt,args)))*/
99
100
101 #ifdef WINE_NO_TRACE_MSGS
102 #define WINE_TRACE(args...) do { } while(0)
103 #define WINE_TRACE_(ch) WINE_TRACE
104 #endif
105
106 #ifdef WINE_NO_DEBUG_MSGS
107 #define WINE_WARN(args...) do { } while(0)
108 #define WINE_WARN_(ch) WINE_WARN
109 #define WINE_FIXME(args...) do { } while(0)
110 #define WINE_FIXME_(ch) WINE_FIXME
111 #endif
112
113 #elif defined(__SUNPRO_C)
114
115 #define __WINE_DPRINTF(dbcl,dbch) \
116 do { if(__WINE_GET_DEBUGGING(dbcl,(dbch))) { \
117 struct __wine_debug_channel * const __dbch = (dbch); \
118 const enum __WINE_DEBUG_CLASS __dbcl = __WINE_DBCL##dbcl; \
119 __WINE_DBG_LOG
120
121 #define __WINE_DBG_LOG(...) \
122 wine_dbg_log( __dbcl, __dbch, __func__, __VA_ARGS__); } } while(0)
123
124 #define __WINE_PRINTF_ATTR(fmt,args)
125
126 #ifdef WINE_NO_TRACE_MSGS
127 #define WINE_TRACE(...) do { } while(0)
128 #define WINE_TRACE_(ch) WINE_TRACE
129 #endif
130
131 #ifdef WINE_NO_DEBUG_MSGS
132 #define WINE_WARN(...) do { } while(0)
133 #define WINE_WARN_(ch) WINE_WARN
134 #define WINE_FIXME(...) do { } while(0)
135 #define WINE_FIXME_(ch) WINE_FIXME
136 #endif
137
138 #else /* !__GNUC__ && !__SUNPRO_C */
139
140 #define __WINE_DPRINTF(dbcl,dbch) \
141 (!__WINE_GET_DEBUGGING(dbcl,(dbch)) || \
142 (ros_dbg_log(__WINE_DBCL##dbcl,(dbch),__FILE__,"",__LINE__,"") == -1)) ? \
143 (void)0 : (void)wine_dbg_printf
144
145 #define __WINE_PRINTF_ATTR(fmt, args)
146
147 #endif /* !__GNUC__ && !__SUNPRO_C */
148
149 struct __wine_debug_functions
150 {
151 char * (*get_temp_buffer)( size_t n );
152 void (*release_temp_buffer)( char *buffer, size_t n );
153 const char * (*dbgstr_an)( const char * s, int n );
154 const char * (*dbgstr_wn)( const WCHAR *s, int n );
155 int (*dbg_vprintf)( const char *format, va_list args );
156 int (*dbg_vlog)( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
157 const char *file, const char *function, const int line, const char *format, va_list args );
158 };
159
160 extern unsigned char __wine_dbg_get_channel_flags( struct __wine_debug_channel *channel );
161 extern int __wine_dbg_set_channel_flags( struct __wine_debug_channel *channel,
162 unsigned char set, unsigned char clear );
163 extern void __wine_dbg_set_functions( const struct __wine_debug_functions *new_funcs,
164 struct __wine_debug_functions *old_funcs, size_t size );
165
166 /*
167 * Exported definitions and macros
168 */
169
170 /* These functions return a printable version of a string, including
171 quotes. The string will be valid for some time, but not indefinitely
172 as strings are re-used. */
173 extern const char *wine_dbgstr_an( const char * s, int n );
174 extern const char *wine_dbgstr_wn( const WCHAR *s, int n );
175 extern const char *wine_dbg_sprintf( const char *format, ... ) __WINE_PRINTF_ATTR(1,2);
176
177 extern int wine_dbg_printf( const char *format, ... ) __WINE_PRINTF_ATTR(1,2);
178 extern int wine_dbg_log( enum __wine_debug_class cls, struct __wine_debug_channel *ch, const char *func,
179 const char *format, ... ) __WINE_PRINTF_ATTR(4,5);
180 /* ReactOS compliant debug format */
181 extern int ros_dbg_log( enum __wine_debug_class cls, struct __wine_debug_channel *ch, const char *file,
182 const char *func, const int line, const char *format, ... ) __WINE_PRINTF_ATTR(6,7);
183
184 static __inline const char *wine_dbgstr_a( const char *s )
185 {
186 return wine_dbgstr_an( s, -1 );
187 }
188
189 static __inline const char *wine_dbgstr_w( const WCHAR *s )
190 {
191 return wine_dbgstr_wn( s, -1 );
192 }
193
194 static __inline const char *wine_dbgstr_guid( const GUID *id )
195 {
196 if (!id) return "(null)";
197 if (!((ULONG_PTR)id >> 16)) return wine_dbg_sprintf( "<guid-0x%04lx>", (ULONG_PTR)id & 0xffff );
198 return wine_dbg_sprintf( "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
199 id->Data1, id->Data2, id->Data3,
200 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
201 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7] );
202 }
203
204 static __inline const char *wine_dbgstr_point( const POINT *pt )
205 {
206 if (!pt) return "(null)";
207 return wine_dbg_sprintf( "(%ld,%ld)", pt->x, pt->y );
208 }
209
210 static __inline const char *wine_dbgstr_size( const SIZE *size )
211 {
212 if (!size) return "(null)";
213 return wine_dbg_sprintf( "(%ld,%ld)", size->cx, size->cy );
214 }
215
216 static __inline const char *wine_dbgstr_rect( const RECT *rect )
217 {
218 if (!rect) return "(null)";
219 return wine_dbg_sprintf( "(%ld,%ld)-(%ld,%ld)", rect->left, rect->top,
220 rect->right, rect->bottom );
221 }
222
223 static __inline const char *wine_dbgstr_longlong( ULONGLONG ll )
224 {
225 if (/*sizeof(ll) > sizeof(unsigned long) &&*/ ll >> 32) /* ULONGLONG is always > long in ReactOS */
226 return wine_dbg_sprintf( "%lx%08lx", (unsigned long)(ll >> 32), (unsigned long)ll );
227 else return wine_dbg_sprintf( "%lx", (unsigned long)ll );
228 }
229
230 #if defined(__oaidl_h__) && defined(V_VT)
231
232 static inline const char *wine_dbgstr_vt( VARTYPE vt )
233 {
234 static const char *const variant_types[] =
235 {
236 "VT_EMPTY","VT_NULL","VT_I2","VT_I4","VT_R4","VT_R8","VT_CY","VT_DATE",
237 "VT_BSTR","VT_DISPATCH","VT_ERROR","VT_BOOL","VT_VARIANT","VT_UNKNOWN",
238 "VT_DECIMAL","15","VT_I1","VT_UI1","VT_UI2","VT_UI4","VT_I8","VT_UI8",
239 "VT_INT","VT_UINT","VT_VOID","VT_HRESULT","VT_PTR","VT_SAFEARRAY",
240 "VT_CARRAY","VT_USERDEFINED","VT_LPSTR","VT_LPWSTR","32","33","34","35",
241 "VT_RECORD","VT_INT_PTR","VT_UINT_PTR","39","40","41","42","43","44","45",
242 "46","47","48","49","50","51","52","53","54","55","56","57","58","59","60",
243 "61","62","63","VT_FILETIME","VT_BLOB","VT_STREAM","VT_STORAGE",
244 "VT_STREAMED_OBJECT","VT_STORED_OBJECT","VT_BLOB_OBJECT","VT_CF","VT_CLSID",
245 "VT_VERSIONED_STREAM"
246 };
247
248 static const char *const variant_flags[16] =
249 {
250 "",
251 "|VT_VECTOR",
252 "|VT_ARRAY",
253 "|VT_VECTOR|VT_ARRAY",
254 "|VT_BYREF",
255 "|VT_VECTOR|VT_BYREF",
256 "|VT_ARRAY|VT_BYREF",
257 "|VT_VECTOR|VT_ARRAY|VT_BYREF",
258 "|VT_RESERVED",
259 "|VT_VECTOR|VT_RESERVED",
260 "|VT_ARRAY|VT_RESERVED",
261 "|VT_VECTOR|VT_ARRAY|VT_RESERVED",
262 "|VT_BYREF|VT_RESERVED",
263 "|VT_VECTOR|VT_BYREF|VT_RESERVED",
264 "|VT_ARRAY|VT_BYREF|VT_RESERVED",
265 "|VT_VECTOR|VT_ARRAY|VT_BYREF|VT_RESERVED",
266 };
267
268 if (vt & ~VT_TYPEMASK)
269 return wine_dbg_sprintf( "%s%s", wine_dbgstr_vt(vt&VT_TYPEMASK), variant_flags[vt>>12] );
270
271 if (vt < sizeof(variant_types)/sizeof(*variant_types))
272 return variant_types[vt];
273
274 if (vt == VT_BSTR_BLOB)
275 return "VT_BSTR_BLOB";
276
277 return wine_dbg_sprintf( "vt(invalid %x)", vt );
278 }
279
280 static inline const char *wine_dbgstr_variant( const VARIANT *v )
281 {
282 if (!v)
283 return "(null)";
284
285 if (V_VT(v) & VT_BYREF) {
286 if (V_VT(v) == (VT_VARIANT|VT_BYREF))
287 return wine_dbg_sprintf( "%p {VT_VARIANT|VT_BYREF: %s}", v, wine_dbgstr_variant(V_VARIANTREF(v)) );
288 if (V_VT(v) == (VT_BSTR|VT_BYREF))
289 return wine_dbg_sprintf( "%p {VT_BSTR|VT_BYREF: %s}", v, V_BSTRREF(v) ? wine_dbgstr_w(*V_BSTRREF(v)) : "(none)" );
290 return wine_dbg_sprintf( "%p {%s %p}", v, wine_dbgstr_vt(V_VT(v)), V_BYREF(v) );
291 }
292
293 if (V_ISARRAY(v) || V_ISVECTOR(v))
294 return wine_dbg_sprintf( "%p {%s %p}", v, wine_dbgstr_vt(V_VT(v)), V_ARRAY(v) );
295
296 switch(V_VT(v)) {
297 case VT_EMPTY:
298 return wine_dbg_sprintf( "%p {VT_EMPTY}", v );
299 case VT_NULL:
300 return wine_dbg_sprintf( "%p {VT_NULL}", v );
301 case VT_I2:
302 return wine_dbg_sprintf( "%p {VT_I2: %d}", v, V_I2(v) );
303 case VT_I4:
304 return wine_dbg_sprintf( "%p {VT_I4: %d}", v, V_I4(v) );
305 case VT_R4:
306 return wine_dbg_sprintf( "%p {VT_R4: %f}", v, V_R4(v) );
307 case VT_R8:
308 return wine_dbg_sprintf( "%p {VT_R8: %lf}", v, V_R8(v) );
309 case VT_CY:
310 return wine_dbg_sprintf( "%p {VT_CY: %s}", v, wine_dbgstr_longlong(V_CY(v).int64) );
311 case VT_DATE:
312 return wine_dbg_sprintf( "%p {VT_DATE: %lf}", v, V_DATE(v) );
313 case VT_BSTR:
314 return wine_dbg_sprintf( "%p {VT_BSTR: %s}", v, wine_dbgstr_w(V_BSTR(v)) );
315 case VT_DISPATCH:
316 return wine_dbg_sprintf( "%p {VT_DISPATCH: %p}", v, V_DISPATCH(v) );
317 case VT_ERROR:
318 return wine_dbg_sprintf( "%p {VT_ERROR: %08x}", v, V_ERROR(v) );
319 case VT_BOOL:
320 return wine_dbg_sprintf( "%p {VT_BOOL: %x}", v, V_BOOL(v) );
321 case VT_UNKNOWN:
322 return wine_dbg_sprintf( "%p {VT_UNKNOWN: %p}", v, V_UNKNOWN(v) );
323 case VT_I1:
324 return wine_dbg_sprintf( "%p {VT_I1: %d}", v, V_I1(v) );
325 case VT_UI1:
326 return wine_dbg_sprintf( "%p {VT_UI1: %u}", v, V_UI1(v) );
327 case VT_UI2:
328 return wine_dbg_sprintf( "%p {VT_UI2: %d}", v, V_UI2(v) );
329 case VT_UI4:
330 return wine_dbg_sprintf( "%p {VT_UI4: %d}", v, V_UI4(v) );
331 case VT_I8:
332 return wine_dbg_sprintf( "%p {VT_I8: %s}", v, wine_dbgstr_longlong(V_I8(v)) );
333 case VT_UI8:
334 return wine_dbg_sprintf( "%p {VT_UI8: %s}", v, wine_dbgstr_longlong(V_UI8(v)) );
335 case VT_INT:
336 return wine_dbg_sprintf( "%p {VT_INT: %d}", v, V_INT(v) );
337 case VT_UINT:
338 return wine_dbg_sprintf( "%p {VT_UINT: %u}", v, V_UINT(v) );
339 case VT_VOID:
340 return wine_dbg_sprintf( "%p {VT_VOID}", v );
341 case VT_RECORD:
342 return wine_dbg_sprintf( "%p {VT_RECORD: %p %p}", v, V_RECORD(v), V_RECORDINFO(v) );
343 default:
344 return wine_dbg_sprintf( "%p {vt %s}", v, wine_dbgstr_vt(V_VT(v)) );
345 }
346 }
347
348 #endif /* defined(__oaidl_h__) && defined(V_VT) */
349
350 #ifndef WINE_TRACE
351 #define WINE_TRACE __WINE_DPRINTF(_TRACE,__wine_dbch___default)
352 #define WINE_TRACE_(ch) __WINE_DPRINTF(_TRACE,&__wine_dbch_##ch)
353 #endif
354 #define WINE_TRACE_ON(ch) __WINE_IS_DEBUG_ON(_TRACE,&__wine_dbch_##ch)
355
356 #ifndef WINE_WARN
357 #define WINE_WARN __WINE_DPRINTF(_WARN,__wine_dbch___default)
358 #define WINE_WARN_(ch) __WINE_DPRINTF(_WARN,&__wine_dbch_##ch)
359 #endif
360 #define WINE_WARN_ON(ch) __WINE_IS_DEBUG_ON(_WARN,&__wine_dbch_##ch)
361
362 #ifndef WINE_FIXME
363 #define WINE_FIXME __WINE_DPRINTF(_FIXME,__wine_dbch___default)
364 #define WINE_FIXME_(ch) __WINE_DPRINTF(_FIXME,&__wine_dbch_##ch)
365 #endif
366 #define WINE_FIXME_ON(ch) __WINE_IS_DEBUG_ON(_FIXME,&__wine_dbch_##ch)
367
368 #define WINE_ERR __WINE_DPRINTF(_ERR,__wine_dbch___default)
369 #define WINE_ERR_(ch) __WINE_DPRINTF(_ERR,&__wine_dbch_##ch)
370 #define WINE_ERR_ON(ch) __WINE_IS_DEBUG_ON(_ERR,&__wine_dbch_##ch)
371
372 #define WINE_DECLARE_DEBUG_CHANNEL(ch) \
373 static struct __wine_debug_channel __wine_dbch_##ch = { (unsigned char)~0, #ch }
374 #define WINE_DEFAULT_DEBUG_CHANNEL(ch) \
375 static struct __wine_debug_channel __wine_dbch_##ch = { (unsigned char)~0, #ch }; \
376 static struct __wine_debug_channel * const __wine_dbch___default = &__wine_dbch_##ch
377
378 #define WINE_DPRINTF wine_dbg_printf
379 #define WINE_MESSAGE wine_dbg_printf
380
381 /* Wine uses shorter names that are very likely to conflict with other software */
382
383 static __inline const char *debugstr_an( const char * s, int n ) { return wine_dbgstr_an( s, n ); }
384 static __inline const char *debugstr_wn( const WCHAR *s, int n ) { return wine_dbgstr_wn( s, n ); }
385 static __inline const char *debugstr_guid( const struct _GUID *id ) { return wine_dbgstr_guid(id); }
386 static __inline const char *debugstr_a( const char *s ) { return wine_dbgstr_an( s, -1 ); }
387 static __inline const char *debugstr_w( const WCHAR *s ) { return wine_dbgstr_wn( s, -1 ); }
388
389 #if defined(__oaidl_h__) && defined(V_VT)
390 static __inline const char *debugstr_vt( VARTYPE vt ) { return wine_dbgstr_vt( vt ); }
391 static __inline const char *debugstr_variant( const VARIANT *v ) { return wine_dbgstr_variant( v ); }
392 #endif
393
394 #define TRACE WINE_TRACE
395 #define TRACE_(ch) WINE_TRACE_(ch)
396 #define TRACE_ON(ch) WINE_TRACE_ON(ch)
397
398 #define WARN WINE_WARN
399 #define WARN_(ch) WINE_WARN_(ch)
400 #define WARN_ON(ch) WINE_WARN_ON(ch)
401
402 #define FIXME WINE_FIXME
403 #define FIXME_(ch) WINE_FIXME_(ch)
404 #define FIXME_ON(ch) WINE_FIXME_ON(ch)
405
406 #undef ERR /* Solaris got an 'ERR' define in <sys/reg.h> */
407 #define ERR WINE_ERR
408 #define ERR_(ch) WINE_ERR_(ch)
409 #define ERR_ON(ch) WINE_ERR_ON(ch)
410
411 #define DPRINTF WINE_DPRINTF
412 #define MESSAGE WINE_MESSAGE
413
414 #ifdef __cplusplus
415 }
416 #endif
417
418 #endif /* __WINE_DEBUG_H */