libui/windows/debug.cpp

85 lines
2.2 KiB
C++
Raw Normal View History

2016-04-21 20:57:36 -05:00
// 25 february 2015
#include "uipriv_windows.hpp"
2016-04-21 20:57:36 -05:00
// LONGTERM disable logging and stopping on no-debug builds
2016-04-21 20:57:36 -05:00
2016-04-23 21:15:33 -05:00
static void printDebug(const WCHAR *msg)
2016-04-21 20:57:36 -05:00
{
OutputDebugStringW(msg);
}
2016-04-23 21:15:33 -05:00
HRESULT _logLastError(debugargs, const WCHAR *s)
2016-04-21 20:57:36 -05:00
{
DWORD le;
WCHAR *msg;
WCHAR *formatted;
BOOL useFormatted;
le = GetLastError();
useFormatted = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, le, 0, (LPWSTR) (&formatted), 0, NULL) != 0;
if (!useFormatted)
2019-04-03 09:05:00 -05:00
formatted = (WCHAR *) L"\n"; // TODO
msg = strf(L"[libui] %s:%s:%s() %s: GetLastError() == %I32u %s",
2016-04-21 20:57:36 -05:00
file, line, func,
s, le, formatted);
if (useFormatted)
LocalFree(formatted); // ignore error
printDebug(msg);
uiprivFree(msg);
2016-04-21 20:57:36 -05:00
DebugBreak();
SetLastError(le);
// a function does not have to set a last error
// if the last error we get is actually 0, then HRESULT_FROM_WIN32(0) will return S_OK (0 cast to an HRESULT, since 0 <= 0), which we don't want
// prevent this by returning E_FAIL
if (le == 0)
return E_FAIL;
return HRESULT_FROM_WIN32(le);
}
HRESULT _logHRESULT(debugargs, const WCHAR *s, HRESULT hr)
{
WCHAR *msg;
WCHAR *formatted;
BOOL useFormatted;
useFormatted = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, hr, 0, (LPWSTR) (&formatted), 0, NULL) != 0;
if (!useFormatted)
2019-04-03 09:05:00 -05:00
formatted = (WCHAR *) L"\n"; // TODO
msg = strf(L"[libui] %s:%s:%s() %s: HRESULT == 0x%08I32X %s",
2016-04-21 20:57:36 -05:00
file, line, func,
s, hr, formatted);
if (useFormatted)
LocalFree(formatted); // ignore error
printDebug(msg);
uiprivFree(msg);
2016-04-21 20:57:36 -05:00
DebugBreak();
return hr;
}
void uiprivRealBug(const char *file, const char *line, const char *func, const char *prefix, const char *format, va_list ap)
2016-04-21 20:57:36 -05:00
{
va_list ap2;
char *msg;
size_t n;
WCHAR *final;
2016-04-21 20:57:36 -05:00
va_copy(ap2, ap);
n = _vscprintf(format, ap2);
va_end(ap2);
n++; // terminating '\0'
2016-04-21 20:57:36 -05:00
msg = (char *) uiprivAlloc(n * sizeof (char), "char[]");
// includes terminating '\0' according to example in https://msdn.microsoft.com/en-us/library/xa1a1a6z.aspx
vsprintf_s(msg, n, format, ap);
2016-04-21 20:57:36 -05:00
final = strf(L"[libui] %hs:%hs:%hs() %hs%hs\n", file, line, func, prefix, msg);
uiprivFree(msg);
printDebug(final);
uiprivFree(final);
2016-04-21 20:57:36 -05:00
DebugBreak();
}