Export the temp buffer functionality in the debug functions interface
[wine] / include / 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #ifndef __WINE_WINE_DEBUG_H
22 #define __WINE_WINE_DEBUG_H
23
24 #include <stdarg.h>
25 #include <windef.h>
26 #ifndef GUID_DEFINED
27 #include <guiddef.h>
28 #endif
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 struct _GUID;
35
36 /*
37  * Internal definitions (do not use these directly)
38  */
39
40 enum __wine_debug_class
41 {
42     __WINE_DBCL_FIXME,
43     __WINE_DBCL_ERR,
44     __WINE_DBCL_WARN,
45     __WINE_DBCL_TRACE,
46     __WINE_DBCL_COUNT
47 };
48
49 struct __wine_debug_channel
50 {
51     unsigned char flags;
52     char name[15];
53 };
54
55 #ifndef WINE_NO_TRACE_MSGS
56 # define __WINE_GET_DEBUGGING_TRACE(dbch) ((dbch)->flags & (1 << __WINE_DBCL_TRACE))
57 #else
58 # define __WINE_GET_DEBUGGING_TRACE(dbch) 0
59 #endif
60
61 #ifndef WINE_NO_DEBUG_MSGS
62 # define __WINE_GET_DEBUGGING_WARN(dbch)  ((dbch)->flags & (1 << __WINE_DBCL_WARN))
63 # define __WINE_GET_DEBUGGING_FIXME(dbch) ((dbch)->flags & (1 << __WINE_DBCL_FIXME))
64 #else
65 # define __WINE_GET_DEBUGGING_WARN(dbch)  0
66 # define __WINE_GET_DEBUGGING_FIXME(dbch) 0
67 #endif
68
69 /* define error macro regardless of what is configured */
70 #define __WINE_GET_DEBUGGING_ERR(dbch)  ((dbch)->flags & (1 << __WINE_DBCL_ERR))
71
72 #define __WINE_GET_DEBUGGING(dbcl,dbch)  __WINE_GET_DEBUGGING##dbcl(dbch)
73 #define __WINE_SET_DEBUGGING(dbcl,dbch,on) \
74     ((on) ? ((dbch)[0] |= 1 << (dbcl)) : ((dbch)[0] &= ~(1 << (dbcl))))
75
76 #ifdef __GNUC__
77
78 #define __WINE_DPRINTF(dbcl,dbch) \
79   do { if(__WINE_GET_DEBUGGING(dbcl,(dbch))) { \
80        struct __wine_debug_channel * const __dbch = (dbch); \
81        const enum __wine_debug_class __dbcl = __WINE_DBCL##dbcl; \
82        __WINE_DBG_LOG
83
84 #define __WINE_DBG_LOG(args...) \
85     wine_dbg_log( __dbcl, __dbch, __FUNCTION__, args); } } while(0)
86
87 #define __WINE_PRINTF_ATTR(fmt,args) __attribute__((format (printf,fmt,args)))
88
89
90 #ifdef WINE_NO_TRACE_MSGS
91 #define WINE_TRACE(args...) do { } while(0)
92 #define WINE_TRACE_(ch) WINE_TRACE
93 #endif
94
95 #ifdef WINE_NO_DEBUG_MSGS
96 #define WINE_WARN(args...) do { } while(0)
97 #define WINE_WARN_(ch) WINE_WARN
98 #define WINE_FIXME(args...) do { } while(0)
99 #define WINE_FIXME_(ch) WINE_FIXME
100 #endif
101
102 #elif defined(__SUNPRO_C)
103
104 #define __WINE_DPRINTF(dbcl,dbch) \
105   do { if(__WINE_GET_DEBUGGING(dbcl,(dbch))) { \
106        struct __wine_debug_channel * const __dbch = (dbch); \
107        const enum __WINE_DEBUG_CLASS __dbcl = __WINE_DBCL##dbcl; \
108        __WINE_DBG_LOG
109
110 #define __WINE_DBG_LOG(...) \
111    wine_dbg_log( __dbcl, __dbch, __func__, __VA_ARGS__); } } while(0)
112
113 #define __WINE_PRINTF_ATTR(fmt,args)
114
115 #ifdef WINE_NO_TRACE_MSGS
116 #define WINE_TRACE(...) do { } while(0)
117 #define WINE_TRACE_(ch) WINE_TRACE
118 #endif
119
120 #ifdef WINE_NO_DEBUG_MSGS
121 #define WINE_WARN(...) do { } while(0)
122 #define WINE_WARN_(ch) WINE_WARN
123 #define WINE_FIXME(...) do { } while(0)
124 #define WINE_FIXME_(ch) WINE_FIXME
125 #endif
126
127 #else  /* !__GNUC__ && !__SUNPRO_C */
128
129 #define __WINE_DPRINTF(dbcl,dbch) \
130     (!__WINE_GET_DEBUGGING(dbcl,(dbch)) || \
131      (wine_dbg_log(__WINE_DBCL##dbcl,(dbch),__FILE__,"%d: ",__LINE__),0)) ? \
132      (void)0 : (void)wine_dbg_printf
133
134 #define __WINE_PRINTF_ATTR(fmt, args)
135
136 #endif  /* !__GNUC__ && !__SUNPRO_C */
137
138 struct __wine_debug_functions
139 {
140     char * (*get_temp_buffer)( size_t n );
141     void   (*release_temp_buffer)( char *buffer, size_t n );
142     const char * (*dbgstr_an)( const char * s, int n );
143     const char * (*dbgstr_wn)( const WCHAR *s, int n );
144     int (*dbg_vprintf)( const char *format, va_list args );
145     int (*dbg_vlog)( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
146                      const char *function, const char *format, va_list args );
147 };
148
149 extern void __wine_dbg_set_functions( const struct __wine_debug_functions *new_funcs,
150                                       struct __wine_debug_functions *old_funcs, size_t size );
151
152 /*
153  * Exported definitions and macros
154  */
155
156 /* These function return a printable version of a string, including
157    quotes.  The string will be valid for some time, but not indefinitely
158    as strings are re-used.  */
159 extern const char *wine_dbgstr_an( const char * s, int n );
160 extern const char *wine_dbgstr_wn( const WCHAR *s, int n );
161 extern const char *wine_dbg_sprintf( const char *format, ... ) __WINE_PRINTF_ATTR(1,2);
162
163 extern int wine_dbg_printf( const char *format, ... ) __WINE_PRINTF_ATTR(1,2);
164 extern int wine_dbg_log( enum __wine_debug_class cls, struct __wine_debug_channel *ch, const char *func,
165                          const char *format, ... ) __WINE_PRINTF_ATTR(4,5);
166
167 extern inline const char *wine_dbgstr_a( const char *s )
168 {
169     return wine_dbgstr_an( s, -1 );
170 }
171
172 extern inline const char *wine_dbgstr_w( const WCHAR *s )
173 {
174     return wine_dbgstr_wn( s, -1 );
175 }
176
177 static inline const char *wine_dbgstr_guid( const GUID *id )
178 {
179     if (!id) return "(null)";
180     if (!((INT_PTR)id >> 16)) return wine_dbg_sprintf( "<guid-0x%04x>", (INT_PTR)id & 0xffff );
181     return wine_dbg_sprintf( "{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
182                              id->Data1, id->Data2, id->Data3,
183                              id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
184                              id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7] );
185 }
186
187 static inline const char *wine_dbgstr_point( const POINT *pt )
188 {
189     if (!pt) return "(null)";
190     return wine_dbg_sprintf( "(%ld,%ld)", pt->x, pt->y );
191 }
192
193 static inline const char *wine_dbgstr_size( const SIZE *size )
194 {
195     if (!size) return "(null)";
196     return wine_dbg_sprintf( "(%ld,%ld)", size->cx, size->cy );
197 }
198
199 static inline const char *wine_dbgstr_rect( const RECT *rect )
200 {
201     if (!rect) return "(null)";
202     return wine_dbg_sprintf( "(%ld,%ld)-(%ld,%ld)", rect->left, rect->top, rect->right, rect->bottom );
203 }
204
205 static inline const char *wine_dbgstr_longlong( ULONGLONG ll )
206 {
207     if (sizeof(ll) > sizeof(unsigned long) && ll >> 32)
208         return wine_dbg_sprintf( "%lx%08lx", (unsigned long)(ll >> 32), (unsigned long)ll );
209     else return wine_dbg_sprintf( "%lx", (unsigned long)ll );
210 }
211
212 #ifndef WINE_TRACE
213 #define WINE_TRACE                 __WINE_DPRINTF(_TRACE,__wine_dbch___default)
214 #define WINE_TRACE_(ch)            __WINE_DPRINTF(_TRACE,&__wine_dbch_##ch)
215 #endif
216 #define WINE_TRACE_ON(ch)          __WINE_GET_DEBUGGING(_TRACE,&__wine_dbch_##ch)
217
218 #ifndef WINE_WARN
219 #define WINE_WARN                  __WINE_DPRINTF(_WARN,__wine_dbch___default)
220 #define WINE_WARN_(ch)             __WINE_DPRINTF(_WARN,&__wine_dbch_##ch)
221 #endif
222 #define WINE_WARN_ON(ch)           __WINE_GET_DEBUGGING(_WARN,&__wine_dbch_##ch)
223
224 #ifndef WINE_FIXME
225 #define WINE_FIXME                 __WINE_DPRINTF(_FIXME,__wine_dbch___default)
226 #define WINE_FIXME_(ch)            __WINE_DPRINTF(_FIXME,&__wine_dbch_##ch)
227 #endif
228 #define WINE_FIXME_ON(ch)          __WINE_GET_DEBUGGING(_FIXME,&__wine_dbch_##ch)
229
230 #define WINE_ERR                   __WINE_DPRINTF(_ERR,__wine_dbch___default)
231 #define WINE_ERR_(ch)              __WINE_DPRINTF(_ERR,&__wine_dbch_##ch)
232 #define WINE_ERR_ON(ch)            __WINE_GET_DEBUGGING(_ERR,&__wine_dbch_##ch)
233
234 #define WINE_DECLARE_DEBUG_CHANNEL(ch) \
235     extern struct __wine_debug_channel __wine_dbch_##ch
236 #define WINE_DEFAULT_DEBUG_CHANNEL(ch) \
237     extern struct __wine_debug_channel __wine_dbch_##ch; \
238     static struct __wine_debug_channel * const __wine_dbch___default = &__wine_dbch_##ch
239
240 #define WINE_DPRINTF               wine_dbg_printf
241 #define WINE_MESSAGE               wine_dbg_printf
242
243 #ifdef __WINESRC__
244 /* Wine uses shorter names that are very likely to conflict with other software */
245
246 inline static const char *debugstr_an( const char * s, int n ) { return wine_dbgstr_an( s, n ); }
247 inline static const char *debugstr_wn( const WCHAR *s, int n ) { return wine_dbgstr_wn( s, n ); }
248 inline static const char *debugstr_guid( const struct _GUID *id ) { return wine_dbgstr_guid(id); }
249 inline static const char *debugstr_a( const char *s )  { return wine_dbgstr_an( s, -1 ); }
250 inline static const char *debugstr_w( const WCHAR *s ) { return wine_dbgstr_wn( s, -1 ); }
251
252 #define TRACE                      WINE_TRACE
253 #define TRACE_(ch)                 WINE_TRACE_(ch)
254 #define TRACE_ON(ch)               WINE_TRACE_ON(ch)
255
256 #define WARN                       WINE_WARN
257 #define WARN_(ch)                  WINE_WARN_(ch)
258 #define WARN_ON(ch)                WINE_WARN_ON(ch)
259
260 #define FIXME                      WINE_FIXME
261 #define FIXME_(ch)                 WINE_FIXME_(ch)
262 #define FIXME_ON(ch)               WINE_FIXME_ON(ch)
263
264 #undef ERR  /* Solaris got an 'ERR' define in <sys/reg.h> */
265 #define ERR                        WINE_ERR
266 #define ERR_(ch)                   WINE_ERR_(ch)
267 #define ERR_ON(ch)                 WINE_ERR_ON(ch)
268
269 #define DPRINTF                    WINE_DPRINTF
270 #define MESSAGE                    WINE_MESSAGE
271
272 #endif /* __WINESRC__ */
273
274 #ifdef __cplusplus
275 }
276 #endif
277
278 #endif  /* __WINE_WINE_DEBUG_H */