X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=os_win32.cpp;h=587503c41553fa7cd013cf5ad28862fa34f938a7;hb=851d0b0452234ace66a511327bd8e6f9d68fe9e9;hp=5f8dddfc1204f7805d720d4a7ae3c98c8df052b5;hpb=768198600907c473d65f97395bd53d9df2da4834;p=apitrace diff --git a/os_win32.cpp b/os_win32.cpp index 5f8dddf..587503c 100644 --- 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 { @@ -91,20 +92,37 @@ GetCurrentDir(char *str, size_t size) void DebugMessage(const char *format, ...) { - char buf[4096]; - - va_list ap; - va_start(ap, format); - fflush(stdout); - vsnprintf(buf, sizeof buf, format, ap); - va_end(ap); - - OutputDebugStringA(buf); - if (!IsDebuggerPresent()) { - fflush(stdout); - fputs(buf, 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 @@ -118,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 */