12 #include "debugtools.h"
18 /* ---------------------------------------------------------------------- */
22 char *str_pos; /* current position in strings buffer */
23 char *out_pos; /* current position in output buffer */
24 char strings[1024]; /* buffer for temporary strings */
25 char output[1024]; /* current output line */
28 static struct debug_info tmp;
30 /* get the debug info pointer for the current thread */
31 static inline struct debug_info *get_info(void)
33 struct debug_info *info = NtCurrentTeb()->debug_info;
38 tmp.str_pos = tmp.strings;
39 tmp.out_pos = tmp.output;
41 if (!GetProcessHeap()) return &tmp;
42 /* setup the temp structure in case HeapAlloc wants to print something */
43 NtCurrentTeb()->debug_info = &tmp;
44 info = HeapAlloc( GetProcessHeap(), 0, sizeof(*info) );
45 info->str_pos = info->strings;
46 info->out_pos = info->output;
47 NtCurrentTeb()->debug_info = info;
52 /* allocate some tmp space for a string */
53 static void *gimme1(int n)
55 struct debug_info *info = get_info();
56 char *res = info->str_pos;
58 if (res + n >= &info->strings[sizeof(info->strings)]) res = info->strings;
59 info->str_pos = res + n;
63 /* release extra space that we requested in gimme1() */
64 static inline void release( void *ptr )
66 struct debug_info *info = NtCurrentTeb()->debug_info;
70 /***********************************************************************
73 const char *wine_dbgstr_an( const char *src, int n )
79 if (!src) return "(null)";
81 sprintf(res, "#%04x", LOWORD(src) );
85 dst = res = gimme1 (n * 4 + 6);
87 while (n-- > 0 && *src)
89 unsigned char c = *src++;
92 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
93 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
94 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
95 case '"': *dst++ = '\\'; *dst++ = '"'; break;
96 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
98 if (c >= ' ' && c <= 126)
103 *dst++ = '0' + ((c >> 6) & 7);
104 *dst++ = '0' + ((c >> 3) & 7);
105 *dst++ = '0' + ((c >> 0) & 7);
121 /***********************************************************************
124 const char *wine_dbgstr_wn( const WCHAR *src, int n )
130 if (!src) return "(null)";
132 sprintf(res, "#%04x", LOWORD(src) );
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 /***********************************************************************
175 const char *wine_dbgstr_guid( const GUID *id )
179 if (!id) return "(null)";
183 sprintf( str, "<guid-0x%04x>", LOWORD(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 /***********************************************************************
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 /* make sure we didn't exceed the buffer length
208 * the two asserts are due to glibc changes in vsnprintfs return value */
210 assert( ret < sizeof(info->output) - (info->out_pos - info->output) );
212 p = strrchr( info->out_pos, '\n' );
213 if (!p) info->out_pos += ret;
216 char *pos = info->output;
218 write( 2, pos, p - pos );
219 /* move beginning of next line to start of buffer */
220 while ((*pos = *p++)) pos++;
226 /***********************************************************************
229 int wine_dbg_printf(const char *format, ...)
234 va_start(valist, format);
235 ret = wine_dbg_vprintf( format, valist );
240 /***********************************************************************
243 int wine_dbg_log(enum __DEBUG_CLASS cls, const char *channel,
244 const char *function, const char *format, ... )
246 static const char *classes[__DBCL_COUNT] = { "fixme", "err", "warn", "trace" };
250 va_start(valist, format);
251 if (cls < __DBCL_COUNT)
252 ret = wine_dbg_printf( "%s:%s:%s ", classes[cls], channel + 1, function );
254 ret += wine_dbg_vprintf( format, valist );