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