2 * Debugging channels functions for WINE
10 #include <wine/debugtools.h>
12 //DECLARE_DEBUG_CHANNEL(tid);
13 DECLARE_DEBUG_CHANNEL(winedbgc);
16 /* ---------------------------------------------------------------------- */
20 char *str_pos; /* current position in strings buffer */
21 char *out_pos; /* current position in output buffer */
22 char strings[1024]; /* buffer for temporary strings */
23 char output[1024]; /* current output line */
26 static struct debug_info tmp;
28 /* get the debug info pointer for the current thread */
29 static inline struct debug_info *get_info(void)
31 struct debug_info *info = NtCurrentTeb()->WineDebugInfo;
36 tmp.str_pos = tmp.strings;
37 tmp.out_pos = tmp.output;
39 if (!RtlGetProcessHeap()) return &tmp;
40 /* setup the temp structure in case HeapAlloc wants to print something */
41 NtCurrentTeb()->WineDebugInfo = &tmp;
42 info = RtlAllocateHeap( RtlGetProcessHeap(), 0, sizeof(*info) );
43 info->str_pos = info->strings;
44 info->out_pos = info->output;
45 NtCurrentTeb()->WineDebugInfo = info;
50 /* allocate some tmp space for a string */
51 static void *gimme1(int n)
53 struct debug_info *info = get_info();
54 char *res = info->str_pos;
56 if (res + n >= &info->strings[sizeof(info->strings)]) res = info->strings;
57 info->str_pos = res + n;
61 /* release extra space that we requested in gimme1() */
62 static inline void release( void *ptr )
64 struct debug_info *info = NtCurrentTeb()->WineDebugInfo;
68 /***********************************************************************
69 * wine_dbgstr_an (NTDLL.@)
71 const char *wine_dbgstr_an( const char *src, int n )
75 if (!((WORD)(DWORD)(src) >> 16))
77 if (!src) return "(null)";
79 sprintf(res, "#%04x", (WORD)(DWORD)(src) );
83 else if (n > 200) n = 200;
84 dst = res = gimme1 (n * 4 + 6);
86 while (n-- > 0 && *src)
88 unsigned char c = *src++;
91 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
92 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
93 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
94 case '"': *dst++ = '\\'; *dst++ = '"'; break;
95 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
97 if (c >= ' ' && c <= 126)
102 *dst++ = '0' + ((c >> 6) & 7);
103 *dst++ = '0' + ((c >> 3) & 7);
104 *dst++ = '0' + ((c >> 0) & 7);
120 /***********************************************************************
121 * wine_dbgstr_wn (NTDLL.@)
123 const char *wine_dbgstr_wn( const WCHAR *src, int n )
127 if (!((WORD)(DWORD)(src) >> 16))
129 if (!src) return "(null)";
131 sprintf(res, "#%04x", (WORD)(DWORD)(src) );
135 else if (n > 200) n = 200;
136 dst = res = gimme1 (n * 5 + 7);
139 while (n-- > 0 && *src)
144 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
145 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
146 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
147 case '"': *dst++ = '\\'; *dst++ = '"'; break;
148 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
150 if (c >= ' ' && c <= 126)
155 sprintf(dst,"%04x",c);
172 /***********************************************************************
173 * wine_dbgstr_guid (NTDLL.@)
175 const char *wine_dbgstr_guid( const GUID *id )
179 if (!id) return "(null)";
180 if (!((WORD)(DWORD)(id) >> 16))
183 sprintf( str, "<guid-0x%04x>", (WORD)(DWORD)(id) );
188 sprintf( str, "{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
189 id->Data1, id->Data2, id->Data3,
190 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
191 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7] );
196 /***********************************************************************
197 * wine_dbg_vprintf (NTDLL.@)
199 int wine_dbg_vprintf( const char *format, va_list args )
201 struct debug_info *info = get_info();
204 int ret = _vsnprintf( info->out_pos, sizeof(info->output) - (info->out_pos - info->output),
207 p = strrchr( info->out_pos, '\n' );
208 if (!p) info->out_pos += ret;
211 char *pos = info->output;
218 /* move beginning of next line to start of buffer */
219 while ((*pos = *p++)) pos++;
225 /***********************************************************************
226 * wine_dbg_printf (NTDLL.@)
228 int wine_dbg_printf(const char *format, ...)
233 va_start(valist, format);
234 ret = wine_dbg_vprintf( format, valist );
239 /***********************************************************************
240 * wine_dbg_log (NTDLL.@)
242 int wine_dbg_log(enum __DEBUG_CLASS cls, const char *channel,
243 const char *function, const char *format, ... )
245 static const char *classes[__DBCL_COUNT] = { "fixme", "err", "warn", "trace" };
249 va_start(valist, format);
250 if (TRACE_ON(winedbgc))
251 ret = wine_dbg_printf( "%08lx:", NtCurrentTeb()->Cid.UniqueThread);
252 if (cls < __DBCL_COUNT)
253 ret += wine_dbg_printf( "%s:%s:%s ", classes[cls], channel + 1, function );
255 ret += wine_dbg_vprintf( format, valist );