1 #include "Debug.h"
2
3 #include <stdarg.h>
4 #include <stdio.h>
5
6
7 #ifdef DEBUG
8
9 unsigned st_debug = 0;
10
11 static const
12 struct debug_named_value st_debug_flags[] = {
13 {"oldtexops", ST_DEBUG_OLD_TEX_OPS, "oldtexops"},
14 {"tgsi", ST_DEBUG_TGSI, "tgsi"},
15 {NULL, 0, NULL},
16 };
17 void
st_debug_parse(void)18 st_debug_parse(void)
19 {
20 st_debug = debug_get_flags_option("ST_DEBUG", st_debug_flags, st_debug);
21 }
22
23 #endif
24
25
26 void
DebugPrintf(const char * format,...)27 DebugPrintf(const char *format, ...)
28 {
29 char buf[4096];
30
31 va_list ap;
32 va_start(ap, format);
33 vsnprintf(buf, sizeof buf, format, ap);
34 va_end(ap);
35
36 OutputDebugStringA(buf);
37 }
38
39
40 /**
41 * Produce a human readable message from HRESULT.
42 *
43 * @sa http://msdn.microsoft.com/en-us/library/ms679351(VS.85).aspx
44 */
45 void
CheckHResult(HRESULT hr,const char * function,unsigned line)46 CheckHResult(HRESULT hr, const char *function, unsigned line)
47 {
48 if (FAILED(hr)) {
49 LPSTR lpMessageBuffer = NULL;
50
51 FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
52 FORMAT_MESSAGE_FROM_SYSTEM,
53 NULL,
54 hr,
55 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
56 (LPSTR)&lpMessageBuffer,
57 0,
58 NULL);
59
60 DebugPrintf("%s: %u: 0x%08lX: %s", function, line, hr, lpMessageBuffer);
61
62 LocalFree(lpMessageBuffer);
63 }
64 }
65
66
67 void
AssertFail(const char * expr,const char * file,unsigned line,const char * function)68 AssertFail(const char *expr,
69 const char *file,
70 unsigned line,
71 const char *function)
72 {
73 DebugPrintf("%s:%u:%s: Assertion `%s' failed.\n", file, line, function, expr);
74 #if defined(__GNUC__)
75 __asm("int3");
76 #elif defined(_MSC_VER)
77 __debugbreak();
78 #else
79 DebugBreak();
80 #endif
81 }
82