15#define DTTR_ERROR_TITLE "DttR: Error"
16#define DTTR_FATAL_ERROR_TITLE "DttR: Fatal Error"
18#define DTTR_ERROR(error_message, ...) \
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); \
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"
30#define DTTR_FATAL(error_message, ...) \
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); \
40#define DTTR_UNWRAP_WINAPI() \
42 DWORD error_code = GetLastError(); \
43 if (error_code == ERROR_SUCCESS) { \
46 LPSTR message = NULL; \
48 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM \
49 | FORMAT_MESSAGE_IGNORE_INSERTS, \
52 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), \
57 const char *message_text = message ? message : "unknown"; \
58 DTTR_FATAL("Win32 API Error 0x%lX: %s", error_code, message_text); \
61#define DTTR_UNWRAP_WINAPI_IF(result, is_error) \
63 __typeof__(result) _r = (result); \
65 DTTR_UNWRAP_WINAPI(); \
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)
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)
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)
void DTTR_Errors_SetMessageHandler(DTTR_ErrorMessageHandler handler)
void DTTR_Errors_ShowMessage(const char *title, const char *message)
void(* DTTR_ErrorMessageHandler)(const char *title, const char *message)