QPMS_UNLIKELY macros to qpms_error.h
[qpms.git] / qpms / qpms_error.h
blob9919296413726a4d10a0d4e7fe6a6c07db890fd9
1 #ifndef QPMS_ERROR_H
2 #define QPMS_ERROR_H
4 #include "optim.h"
6 /// Provisional error message with abort();
7 void qpms_pr_error(const char *fmt, ...);
8 //void qpms_error(const char *fmt, ...);
10 /// Provisional error message with abort(), indicating source and line number.
11 void qpms_pr_error_at_line(const char *filename, unsigned int linenum,
12 const char *fmt, ...);
14 void qpms_pr_error_at_flf(const char *filename, unsigned int linenum,
15 const char *func,
16 const char *fmt, ...);
18 /// Print a warning message to stderr and flush the buffer. Don't call this directly, use QPMS_WARN().
19 void qpms_warn_at_flf(const char *filename, unsigned int linenum,
20 const char *func,
21 const char *fmt, ...);
23 void qpms_pr_debug_at_flf(const char *filename, unsigned int linenum,
24 const char *func,
25 const char *fmt, ...);
26 //void qpms_error_at_line(const char *filename, unsigned int linenum,
27 // const char *fmt, ...);
30 typedef enum {
31 QPMS_DBGMSG_MISC = 1,
32 QPMS_DBGMSG_THREADS = 2 // Multithreading-related debug messages.
33 } qpms_dbgmsg_flags;
35 void qpms_debug_at_flf(const char *filename, unsigned int linenum,
36 const char *func,
37 qpms_dbgmsg_flags type,
38 const char *fmt, ...);
40 extern qpms_dbgmsg_flags qpms_dbgmsg_enabled;
42 qpms_dbgmsg_flags qpms_dbgmsg_disable(qpms_dbgmsg_flags types);
43 qpms_dbgmsg_flags qpms_dbgmsg_enable(qpms_dbgmsg_flags types);
46 #define QPMS_WARN(msg, ...) qpms_warn_at_flf(__FILE__,__LINE__,__func__,msg, ##__VA_ARGS__)
48 #define QPMS_DEBUG(type, msg, ...) qpms_debug_at_flf(__FILE__,__LINE__,__func__,type,msg, ##__VA_ARGS__)
50 #define QPMS_CRASHING_MALLOC(pointer, size) {\
51 (pointer) = malloc(size);\
52 if(QPMS_UNLIKELY(!(pointer) && (size)))\
53 qpms_pr_debug_at_flf(__FILE__,__LINE__,__func__,\
54 "Allocation of %zd bytes for " #pointer " failed.",\
55 (size_t) (size));\
58 #define QPMS_CRASHING_CALLOC(pointer, nmemb, size) {\
59 (pointer) = calloc((nmemb), (size));\
60 if(QPMS_UNLIKELY(!(pointer) && (nmemb) && (size)))\
61 qpms_pr_debug_at_flf(__FILE__,__LINE__,__func__,\
62 "Allocation of %zd bytes for " #pointer " failed.",\
63 (size_t)((nmemb)*(size)));\
66 #define QPMS_CRASHING_REALLOC(pointer, size) {\
67 (pointer) = realloc((pointer), size);\
68 if(QPMS_UNLIKELY(!(pointer) && (size)))\
69 qpms_pr_debug_at_flf(__FILE__,__LINE__,__func__,\
70 "Rellocation of %zd bytes for " #pointer " failed.",\
71 (size_t) (size));\
74 #define QPMS_WTF qpms_pr_error_at_flf(__FILE__,__LINE__,__func__,"Unexpected error.")
76 #define QPMS_UNTESTED {\
77 static _Bool already_bitched = 0; \
78 if (QPMS_UNLIKELY(!already_bitched)) {\
79 qpms_warn_at_flf(__FILE__,__LINE__,__func__,"Warning: untested function/feature!");\
80 already_bitched = 1;\
84 #define QPMS_PR_ERROR(msg, ...) qpms_pr_error_at_flf(__FILE__,__LINE__,__func__,msg, ##__VA_ARGS__)
86 // TODO replace this macro with an inline function?
87 #define QPMS_ENSURE_SUCCESS(x) { \
88 int errorcode = (x); \
89 if(QPMS_UNLIKELY(errorcode)) \
90 qpms_pr_error_at_flf(__FILE__,__LINE__,__func__,"Unexpected error (%d)", errorcode); \
94 #define QPMS_ENSURE(x, msg, ...) {if(QPMS_UNLIKELY(!(x))) qpms_pr_error_at_flf(__FILE__,__LINE__,__func__,msg, ##__VA_ARGS__); }
96 #define QPMS_ASSERT(x) {\
97 if(QPMS_UNLIKELY(!(x)))\
98 qpms_pr_error_at_flf(__FILE__,__LINE__,__func__,\
99 "Unexpected error. This is most certainly a bug.");\
102 #define QPMS_NOT_IMPLEMENTED(msg, ...) qpms_pr_error_at_flf(__FILE__,__LINE__,__func__, \
103 "Not implemented:" msg, ##__VA_ARGS__)
105 #define QPMS_INCOMPLETE_IMPLEMENTATION(msg, ...) {\
106 static _Bool already_bitched = 0; \
107 if (QPMS_UNLIKELY(!already_bitched)) {\
108 qpms_warn_at_flf(__FILE__,__LINE__,__func__,msg, ##__VA_ARGS__);\
109 already_bitched = 1;\
113 #endif