summaryrefslogtreecommitdiff
path: root/new/windows/debug.c
diff options
context:
space:
mode:
authorPietro Gagliardi <[email protected]>2015-04-15 18:49:45 -0400
committerPietro Gagliardi <[email protected]>2015-04-15 18:49:45 -0400
commit518a5ddbf15d50a254c732a80d5907ef8878abe0 (patch)
tree48cf259f98994e4570e65c389fcd9824272884ad /new/windows/debug.c
parent50ae3ca045e7f5f5744043df0a4506e2f6930bb1 (diff)
Split all OS backends into their own folders.
Diffstat (limited to 'new/windows/debug.c')
-rw-r--r--new/windows/debug.c111
1 files changed, 111 insertions, 0 deletions
diff --git a/new/windows/debug.c b/new/windows/debug.c
new file mode 100644
index 0000000..bc84b63
--- /dev/null
+++ b/new/windows/debug.c
@@ -0,0 +1,111 @@
+// 25 february 2015
+#include "uipriv_windows.h"
+
+// uncomment the following line to enable debug messages
+#define tableDebug
+// uncomment the following line to halt on a debug message
+#define tableDebugStop
+
+#ifdef tableDebug
+
+#include <stdio.h>
+
+HRESULT logLastError(const char *context)
+{
+ DWORD le;
+ WCHAR *msg;
+ BOOL parenthesize = FALSE;
+ BOOL localFreeFailed = FALSE;
+ DWORD localFreeLastError;
+
+ le = GetLastError();
+ fprintf(stderr, "%s: ", context);
+ if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, le, 0, (LPWSTR) (&msg), 0, NULL) != 0) {
+ fprintf(stderr, "%S (", msg);
+ if (LocalFree(msg) != NULL) {
+ localFreeFailed = TRUE;
+ localFreeLastError = GetLastError();
+ }
+ parenthesize = TRUE;
+ }
+ fprintf(stderr, "GetLastError() == %I32u", le);
+ if (parenthesize)
+ fprintf(stderr, ")");
+ if (localFreeFailed)
+ fprintf(stderr, "; local free of system message failed with last error %I32u", localFreeLastError);
+ fprintf(stderr, "\n");
+#ifdef tableDebugStop
+ DebugBreak();
+#endif
+ 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, so the rest of the Table code doesn't barge onward
+ if (le == 0)
+ return E_FAIL;
+ return HRESULT_FROM_WIN32(le);
+}
+
+HRESULT logHRESULT(const char *context, HRESULT hr)
+{
+ WCHAR *msg;
+ BOOL parenthesize = FALSE;
+ BOOL localFreeFailed = FALSE;
+ DWORD localFreeLastError;
+
+ fprintf(stderr, "%s: ", context);
+ // this isn't technically documented, but everyone does it, including Microsoft (see the implementation of _com_error::ErrorMessage() in a copy of comdef.h that comes with the Windows DDK)
+ if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, (DWORD) hr, 0, (LPWSTR) (&msg), 0, NULL) != 0) {
+ fprintf(stderr, "%S (", msg);
+ if (LocalFree(msg) != NULL) {
+ localFreeFailed = TRUE;
+ localFreeLastError = GetLastError();
+ }
+ parenthesize = TRUE;
+ }
+ fprintf(stderr, "HRESULT == 0x%I32X", hr);
+ if (parenthesize)
+ fprintf(stderr, ")");
+ if (localFreeFailed)
+ fprintf(stderr, "; local free of system message failed with last error %I32u", localFreeLastError);
+ fprintf(stderr, "\n");
+#ifdef tableDebugStop
+ DebugBreak();
+#endif
+ return hr;
+}
+
+HRESULT logMemoryExhausted(const char *reason)
+{
+ fprintf(stderr, "memory exhausted %s\n", reason);
+#ifdef tableDebugStop
+ DebugBreak();
+#endif
+ return E_OUTOFMEMORY;
+}
+
+#else
+
+HRESULT logLastError(const char *reason)
+{
+ DWORD le;
+
+ le = GetLastError();
+ // we shouldn't need to do this, but let's do this anyway just to be safe
+ SetLastError(le);
+ if (le == 0)
+ return E_FAIL;
+ return HRESULT_FROM_WIN32(le);
+}
+
+HRESULT logHRESULT(const char *reason, HRESULT hr)
+{
+ return hr;
+}
+
+HRESULT logMemoryExhausted(const char *reason)
+{
+ return E_OUTOFMEMORY;
+}
+
+#endif