12 #include "debugtools.h"
18 DECLARE_DEBUG_CHANNEL(tid);
20 /* ---------------------------------------------------------------------- */
24 char *str_pos; /* current position in strings buffer */
25 char *out_pos; /* current position in output buffer */
26 char strings[1024]; /* buffer for temporary strings */
27 char output[1024]; /* current output line */
30 static struct debug_info tmp;
32 /* get the debug info pointer for the current thread */
33 static inline struct debug_info *get_info(void)
35 struct debug_info *info = NtCurrentTeb()->debug_info;
40 tmp.str_pos = tmp.strings;
41 tmp.out_pos = tmp.output;
43 if (!GetProcessHeap()) return &tmp;
44 /* setup the temp structure in case HeapAlloc wants to print something */
45 NtCurrentTeb()->debug_info = &tmp;
46 info = HeapAlloc( GetProcessHeap(), 0, sizeof(*info) );
47 info->str_pos = info->strings;
48 info->out_pos = info->output;
49 NtCurrentTeb()->debug_info = info;
54 /* allocate some tmp space for a string */
55 static void *gimme1(int n)
57 struct debug_info *info = get_info();
58 char *res = info->str_pos;
60 if (res + n >= &info->strings[sizeof(info->strings)]) res = info->strings;
61 info->str_pos = res + n;
65 /* release extra space that we requested in gimme1() */
66 static inline void release( void *ptr )
68 struct debug_info *info = NtCurrentTeb()->debug_info;
72 /***********************************************************************
75 const char *wine_dbgstr_an( const char *src, int n )
81 if (!src) return "(null)";
83 sprintf(res, "#%04x", LOWORD(src) );
87 dst = res = gimme1 (n * 4 + 6);
89 while (n-- > 0 && *src)
91 unsigned char c = *src++;
94 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
95 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
96 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
97 case '"': *dst++ = '\\'; *dst++ = '"'; break;
98 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
100 if (c >= ' ' && c <= 126)
105 *dst++ = '0' + ((c >> 6) & 7);
106 *dst++ = '0' + ((c >> 3) & 7);
107 *dst++ = '0' + ((c >> 0) & 7);
123 /***********************************************************************
126 const char *wine_dbgstr_wn( const WCHAR *src, int n )
132 if (!src) return "(null)";
134 sprintf(res, "#%04x", LOWORD(src) );
138 dst = res = gimme1 (n * 5 + 7);
141 while (n-- > 0 && *src)
146 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
147 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
148 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
149 case '"': *dst++ = '\\'; *dst++ = '"'; break;
150 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
152 if (c >= ' ' && c <= 126)
157 sprintf(dst,"%04x",c);
174 /***********************************************************************
177 const char *wine_dbgstr_guid( const GUID *id )
181 if (!id) return "(null)";
185 sprintf( str, "<guid-0x%04x>", LOWORD(id) );
190 sprintf( str, "{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
191 id->Data1, id->Data2, id->Data3,
192 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
193 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7] );
198 /***********************************************************************
201 int wine_dbg_vprintf( const char *format, va_list args )
203 struct debug_info *info = get_info();
206 int ret = vsnprintf( info->out_pos, sizeof(info->output) - (info->out_pos - info->output),
209 /* make sure we didn't exceed the buffer length
210 * the two asserts are due to glibc changes in vsnprintfs return value */
212 assert( ret < sizeof(info->output) - (info->out_pos - info->output) );
214 p = strrchr( info->out_pos, '\n' );
215 if (!p) info->out_pos += ret;
218 char *pos = info->output;
220 write( 2, pos, p - pos );
221 /* move beginning of next line to start of buffer */
222 while ((*pos = *p++)) pos++;
228 /***********************************************************************
231 int wine_dbg_printf(const char *format, ...)
236 va_start(valist, format);
237 ret = wine_dbg_vprintf( format, valist );
242 /***********************************************************************
245 int wine_dbg_log(enum __DEBUG_CLASS cls, const char *channel,
246 const char *function, const char *format, ... )
248 static const char *classes[__DBCL_COUNT] = { "fixme", "err", "warn", "trace" };
252 va_start(valist, format);
254 ret = wine_dbg_printf( "%08lx:", (DWORD)NtCurrentTeb()->tid );
255 if (cls < __DBCL_COUNT)
256 ret += wine_dbg_printf( "%s:%s:%s ", classes[cls], channel + 1, function );
258 ret += wine_dbg_vprintf( format, valist );