X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=os_win32.cpp;h=4bfdf5e00d96373c1a7e003f42c2dfd521ef935c;hb=93dfad1dc7e20d694e2c8b63515bff8ae91f3700;hp=0849ab06e522959e5d25b70f27d98ef428b474f3;hpb=8fbdd3aff14448a8096a5b466d55b3154e836df5;p=apitrace diff --git a/os_win32.cpp b/os_win32.cpp old mode 100755 new mode 100644 index 0849ab0..4bfdf5e --- a/os_win32.cpp +++ b/os_win32.cpp @@ -24,11 +24,11 @@ **************************************************************************/ #include +#include #include #include #include "os.hpp" -#include "log.hpp" namespace OS { @@ -89,14 +89,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 +135,50 @@ Abort(void) } +struct Interrupts +{ + Interrupts() + : set(false), + prevfilter(NULL), + handler(NULL) + {} -} /* namespace OS */ + bool set; + LPTOP_LEVEL_EXCEPTION_FILTER prevFilter; + + void (*handler)(int); +}; +static Interrupts interrupts; + +LONG WINAPI InterruptHandler(EXCEPTION_POINTERS *exceptionInfo) +{ + if (interrupts.handler) { + int exceptionCode = 0; + if (exceptionInfo) { + exceptionCode = exceptionInfo->ExceptionRecord.ExceptionCode; + } + interrupts.handler(exceptionCode); + } -#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; + if (interrupts.prevFilter) { + return interrupts.prevFilter(exceptionInfo); + } else { + return EXCEPTION_CONTINUE_SEARCH; } - (void)hinstDLL; - (void)lpvReserved; - return TRUE; } -#endif + +void +CatchInterrupts(void (*func)(int)) +{ + interrupts.handler = func; + + if (!interrupts.set) { + interrupts.prevFilter = + SetUnhandledExceptionFilter(InterruptHandler); + interrupts.set = true; + } +} + + +} /* namespace OS */