102 Patches: Detours to the Rescue
C reference for DttR maintainers and modders.
Loading...
Searching...
No Matches
dttr_errors.h
Go to the documentation of this file.
1#ifndef DTTR_ERRORS_H
2#define DTTR_ERRORS_H
3
4#include <stdlib.h>
5
6#include <dttr_log.h>
7#include <sds.h>
8#include <windows.h>
9
10typedef void (*DTTR_ErrorMessageHandler)(const char *title, const char *message);
11
13void DTTR_Errors_ShowMessage(const char *title, const char *message);
14
15#define DTTR_ERROR_TITLE "DttR: Error"
16#define DTTR_FATAL_ERROR_TITLE "DttR: Fatal Error"
17
18#define DTTR_ERROR(error_message, ...) \
19 do { \
20 sds _err_msg = sdscatprintf(sdsempty(), error_message, ##__VA_ARGS__); \
21 DTTR_LOG_ERROR("%s", _err_msg); \
22 DTTR_Errors_ShowMessage(DTTR_ERROR_TITLE, _err_msg); \
23 sdsfree(_err_msg); \
24 } while (0)
25
26#define DTTR_REPORT_SUFFIX \
27 "\n\nFeel free to report this error as an " \
28 "issue if it's unexpected:\nhttps://gitlab.com/dogstuff/detours-to-the-rescue\n"
29
30#define DTTR_FATAL(error_message, ...) \
31 do { \
32 sds _err_msg = sdscatprintf(sdsempty(), error_message, ##__VA_ARGS__); \
33 _err_msg = sdscat(_err_msg, DTTR_REPORT_SUFFIX); \
34 DTTR_LOG_ERROR("%s", _err_msg); \
35 DTTR_Errors_ShowMessage(DTTR_FATAL_ERROR_TITLE, _err_msg); \
36 sdsfree(_err_msg); \
37 exit(EXIT_FAILURE); \
38 } while (0)
39
40#define DTTR_UNWRAP_WINAPI() \
41 do { \
42 DWORD error_code = GetLastError(); \
43 if (error_code == ERROR_SUCCESS) { \
44 break; \
45 } \
46 LPSTR message = NULL; \
47 FormatMessageA( \
48 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM \
49 | FORMAT_MESSAGE_IGNORE_INSERTS, \
50 NULL, \
51 error_code, \
52 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), \
53 (LPSTR) & message, \
54 0, \
55 NULL \
56 ); \
57 const char *message_text = message ? message : "unknown"; \
58 DTTR_FATAL("Win32 API Error 0x%lX: %s", error_code, message_text); \
59 } while (0)
60
61#define DTTR_UNWRAP_WINAPI_IF(result, is_error) \
62 __extension__({ \
63 __typeof__(result) _r = (result); \
64 if (is_error(_r)) { \
65 DTTR_UNWRAP_WINAPI(); \
66 } \
67 _r; \
68 })
69
70#define DTTR_UNWRAP_WINAPI_NONNEGATIVE_IS_ERROR(x) ((x) < 0)
71#define DTTR_UNWRAP_WINAPI_NONNEGATIVE(result) \
72 DTTR_UNWRAP_WINAPI_IF(result, DTTR_UNWRAP_WINAPI_NONNEGATIVE_IS_ERROR)
73
74#define DTTR_UNWRAP_WINAPI_NONZERO_IS_ERROR(x) ((x) == FALSE)
75#define DTTR_UNWRAP_WINAPI_NONZERO(result) \
76 DTTR_UNWRAP_WINAPI_IF(result, DTTR_UNWRAP_WINAPI_NONZERO_IS_ERROR)
77
78#define DTTR_UNWRAP_WINAPI_EXISTS_IS_ERROR(x) ((x) == NULL)
79#define DTTR_UNWRAP_WINAPI_EXISTS(result) \
80 DTTR_UNWRAP_WINAPI_IF(result, DTTR_UNWRAP_WINAPI_EXISTS_IS_ERROR)
81
82#endif // DTTR_ERRORS_H
void DTTR_Errors_SetMessageHandler(DTTR_ErrorMessageHandler handler)
Definition errors.c:6
void DTTR_Errors_ShowMessage(const char *title, const char *message)
Definition errors.c:10
void(* DTTR_ErrorMessageHandler)(const char *title, const char *message)
Definition dttr_errors.h:10