X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=os_win32.cpp;h=587503c41553fa7cd013cf5ad28862fa34f938a7;hb=9d50fbb84f9b3086aa8e985e32534961336563b2;hp=0849ab06e522959e5d25b70f27d98ef428b474f3;hpb=8fbdd3aff14448a8096a5b466d55b3154e836df5;p=apitrace diff --git a/os_win32.cpp b/os_win32.cpp old mode 100755 new mode 100644 index 0849ab0..587503c --- a/os_win32.cpp +++ b/os_win32.cpp @@ -24,11 +24,12 @@ **************************************************************************/ #include +#include +#include #include #include #include "os.hpp" -#include "log.hpp" namespace OS { @@ -89,14 +90,39 @@ GetCurrentDir(char *str, size_t size) } void -DebugMessage(const char *message) +DebugMessage(const char *format, ...) { - OutputDebugStringA(message); - if (!IsDebuggerPresent()) { - fflush(stdout); - fputs(message, stderr); - fflush(stderr); - } + char buf[4096]; + + va_list ap; + va_start(ap, format); + fflush(stdout); + vsnprintf(buf, sizeof buf, format, ap); + va_end(ap); + + OutputDebugStringA(buf); + + /* + * Also write the message to stderr, when a debugger is not present (to + * avoid duplicate messages in command line debuggers). + */ +#if _WIN32_WINNT > 0x0400 + if (!IsDebuggerPresent()) { + fflush(stdout); + fputs(buf, stderr); + fflush(stderr); + } +#endif +} + +long long GetTime(void) +{ + static LARGE_INTEGER frequency; + LARGE_INTEGER counter; + if (!frequency.QuadPart) + QueryPerformanceFrequency(&frequency); + QueryPerformanceCounter(&counter); + return counter.QuadPart*1000000LL/frequency.QuadPart; } void @@ -110,24 +136,40 @@ Abort(void) } +static LPTOP_LEVEL_EXCEPTION_FILTER prevExceptionFilter = NULL; +static void (*gCallback)(void) = NULL; -} /* namespace OS */ +static LONG WINAPI UnhandledExceptionFilter(PEXCEPTION_POINTERS pExceptionInfo) +{ + if (gCallback) { + gCallback(); + } + + if (prevExceptionFilter) { + return prevExceptionFilter(pExceptionInfo); + } else { + return EXCEPTION_CONTINUE_SEARCH; + } +} +void +SetExceptionCallback(void (*callback)(void)) +{ + assert(!gCallback); + + if (!gCallback) { + gCallback = callback; -#if 0 -BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) { - switch(fdwReason) { - case DLL_PROCESS_ATTACH: - case DLL_THREAD_ATTACH: - return TRUE; - case DLL_THREAD_DETACH: - return TRUE; - case DLL_PROCESS_DETACH: - Log::Close(); - return TRUE; + assert(!prevExceptionFilter); + prevExceptionFilter = SetUnhandledExceptionFilter(UnhandledExceptionFilter); } - (void)hinstDLL; - (void)lpvReserved; - return TRUE; } -#endif + +void +ResetExceptionCallback(void) +{ + gCallback = NULL; +} + + +} /* namespace OS */