17ec681f3Smrg#include "Debug.h"
27ec681f3Smrg
37ec681f3Smrg#include <stdarg.h>
47ec681f3Smrg#include <stdio.h>
57ec681f3Smrg
67ec681f3Smrg
77ec681f3Smrg#ifdef DEBUG
87ec681f3Smrg
97ec681f3Smrgunsigned st_debug = 0;
107ec681f3Smrg
117ec681f3Smrgstatic const
127ec681f3Smrgstruct debug_named_value st_debug_flags[] = {
137ec681f3Smrg   {"oldtexops", ST_DEBUG_OLD_TEX_OPS, "oldtexops"},
147ec681f3Smrg   {"tgsi", ST_DEBUG_TGSI, "tgsi"},
157ec681f3Smrg   {NULL, 0, NULL},
167ec681f3Smrg};
177ec681f3Smrgvoid
187ec681f3Smrgst_debug_parse(void)
197ec681f3Smrg{
207ec681f3Smrg   st_debug = debug_get_flags_option("ST_DEBUG", st_debug_flags, st_debug);
217ec681f3Smrg}
227ec681f3Smrg
237ec681f3Smrg#endif
247ec681f3Smrg
257ec681f3Smrg
267ec681f3Smrgvoid
277ec681f3SmrgDebugPrintf(const char *format, ...)
287ec681f3Smrg{
297ec681f3Smrg    char buf[4096];
307ec681f3Smrg
317ec681f3Smrg    va_list ap;
327ec681f3Smrg    va_start(ap, format);
337ec681f3Smrg    vsnprintf(buf, sizeof buf, format, ap);
347ec681f3Smrg    va_end(ap);
357ec681f3Smrg
367ec681f3Smrg    OutputDebugStringA(buf);
377ec681f3Smrg}
387ec681f3Smrg
397ec681f3Smrg
407ec681f3Smrg/**
417ec681f3Smrg * Produce a human readable message from HRESULT.
427ec681f3Smrg *
437ec681f3Smrg * @sa http://msdn.microsoft.com/en-us/library/ms679351(VS.85).aspx
447ec681f3Smrg */
457ec681f3Smrgvoid
467ec681f3SmrgCheckHResult(HRESULT hr, const char *function, unsigned line)
477ec681f3Smrg{
487ec681f3Smrg   if (FAILED(hr)) {
497ec681f3Smrg      LPSTR lpMessageBuffer = NULL;
507ec681f3Smrg
517ec681f3Smrg      FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
527ec681f3Smrg                     FORMAT_MESSAGE_FROM_SYSTEM,
537ec681f3Smrg                     NULL,
547ec681f3Smrg                     hr,
557ec681f3Smrg                     MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
567ec681f3Smrg                     (LPSTR)&lpMessageBuffer,
577ec681f3Smrg                     0,
587ec681f3Smrg                     NULL);
597ec681f3Smrg
607ec681f3Smrg      DebugPrintf("%s: %u: 0x%08lX: %s", function, line, hr, lpMessageBuffer);
617ec681f3Smrg
627ec681f3Smrg      LocalFree(lpMessageBuffer);
637ec681f3Smrg   }
647ec681f3Smrg}
657ec681f3Smrg
667ec681f3Smrg
677ec681f3Smrgvoid
687ec681f3SmrgAssertFail(const char *expr,
697ec681f3Smrg           const char *file,
707ec681f3Smrg           unsigned line,
717ec681f3Smrg           const char *function)
727ec681f3Smrg{
737ec681f3Smrg   DebugPrintf("%s:%u:%s: Assertion `%s' failed.\n", file, line, function, expr);
747ec681f3Smrg#if defined(__GNUC__)
757ec681f3Smrg   __asm("int3");
767ec681f3Smrg#elif defined(_MSC_VER)
777ec681f3Smrg   __debugbreak();
787ec681f3Smrg#else
797ec681f3Smrg   DebugBreak();
807ec681f3Smrg#endif
817ec681f3Smrg}
82