QPMS
Electromagnetic multiple scattering library and toolkit.
qpms_error.h
Go to the documentation of this file.
1 
6 #ifndef QPMS_ERROR_H
7 #define QPMS_ERROR_H
8 
9 #include "optim.h"
10 
12 QPMS_NORETURN void qpms_pr_error(const char *fmt, ...);
13 //void qpms_error(const char *fmt, ...);
14 
16 
22 QPMS_NORETURN void qpms_pr_error_at_flf(const char *filename, unsigned int linenum,
23  const char *func,
24  const char *fmt, ...);
25 
27 void qpms_warn_at_flf(const char *filename, unsigned int linenum,
28  const char *func,
29  const char *fmt, ...);
30 
32 
35 typedef enum {
36  QPMS_DBGMSG_MISC = 1,
40 
42 void qpms_debug_at_flf(const char *filename, unsigned int linenum,
43  const char *func,
44  qpms_dbgmsg_flags type,
45  const char *fmt, ...);
46 
48 
55 
57 
59 
61 
63 
65 
68 #define QPMS_WARN(msg, ...) qpms_warn_at_flf(__FILE__,__LINE__,__func__,msg, ##__VA_ARGS__)
69 
71 
74 #define QPMS_WARN_ONCE(msg, ...) {\
75  static _Bool already_bitched = 0; \
76  if (QPMS_UNLIKELY(!already_bitched)) {\
77  qpms_warn_at_flf(__FILE__,__LINE__,__func__,msg, ##__VA_ARGS__);\
78  already_bitched = 1;\
79  }\
80 }
81 
82 
84 
92 #define QPMS_DEBUG(type , msg, ...) qpms_debug_at_flf(__FILE__,__LINE__,__func__,type,msg, ##__VA_ARGS__)
93 
95 
109 #define QPMS_CRASHING_MALLOC(pointer, size) {\
110  (pointer) = malloc(size);\
111  if(QPMS_UNLIKELY(!(pointer) && (size)))\
112  qpms_pr_error_at_flf(__FILE__,__LINE__,__func__,\
113  "Allocation of %zd bytes for " #pointer " failed.",\
114  (size_t) (size));\
115 }
116 
118 
124 #define QPMS_CRASHING_MALLOCPY(dest, src, size) {\
125  (dest) = malloc(size);\
126  if(QPMS_UNLIKELY(!(dest) && (size)))\
127  qpms_pr_error_at_flf(__FILE__,__LINE__,__func__,\
128  "Allocation of %zd bytes for " #dest " failed.",\
129  (size_t) (size));\
130  memcpy((dest), (src), (size));\
131 }
132 
134 
148 #define QPMS_CRASHING_CALLOC(pointer, nmemb, size) {\
149  (pointer) = calloc((nmemb), (size));\
150  if(QPMS_UNLIKELY(!(pointer) && (nmemb) && (size)))\
151  qpms_pr_error_at_flf(__FILE__,__LINE__,__func__,\
152  "Allocation of %zd bytes for " #pointer " failed.",\
153  (size_t)((nmemb)*(size)));\
154 }
155 
157 
171 #define QPMS_CRASHING_REALLOC(pointer, size) {\
172  (pointer) = realloc((pointer), size);\
173  if(QPMS_UNLIKELY(!(pointer) && (size)))\
174  qpms_pr_error_at_flf(__FILE__,__LINE__,__func__,\
175  "Rellocation of %zd bytes for " #pointer " failed.",\
176  (size_t) (size));\
177 }
178 
180 
181 #define QPMS_WTF qpms_pr_error_at_flf(__FILE__,__LINE__,__func__,"Unexpected error.")
182 
184 #define QPMS_INVALID_ENUM(x) qpms_pr_error_at_flf(__FILE__,__LINE__,__func__,"Invalid enum value (" #x " == %d)", (int) (x))
185 
187 #define QPMS_UNTESTED {\
188  static _Bool already_bitched = 0; \
189  if (QPMS_UNLIKELY(!already_bitched)) {\
190  qpms_warn_at_flf(__FILE__,__LINE__,__func__,"Warning: untested function/feature!");\
191  already_bitched = 1;\
192  }\
193 }
194 
196 
197 #define QPMS_PR_ERROR(msg, ...) qpms_pr_error_at_flf(__FILE__,__LINE__,__func__,msg, ##__VA_ARGS__)
198 
200 #define QPMS_ENSURE_SUCCESS(x) { \
201  int errorcode = (x); /* evaluate x only once */ \
202  if(QPMS_UNLIKELY(errorcode)) \
203  qpms_pr_error_at_flf(__FILE__,__LINE__,__func__,"Unexpected error (%d)", errorcode); \
204 }
205 
207 #define QPMS_ENSURE_SUCCESS_M(x, msg, ...) { \
208  int errorcode = (x); \
209  if(QPMS_UNLIKELY(errorcode)) \
210  qpms_pr_error_at_flf(__FILE__,__LINE__,__func__,msg, ##__VA_ARGS__); \
211 }
212 
214 #define QPMS_ENSURE_SUCCESS_OR(x, ...) { \
215  int errorcode = (x); /* evaluate x only once */ \
216  static const int allowed_errorcodes[] = {0, ##__VA_ARGS__};\
217  static const int n_allowed = sizeof(allowed_errorcodes) / sizeof(int); \
218  int i; \
219  for(i = 0; i < n_allowed; ++i) \
220  if (errorcode == allowed_errorcodes[i]) break; \
221  if (QPMS_UNLIKELY(i >= n_allowed)) \
222  qpms_pr_error_at_flf(__FILE__,__LINE__,__func__,"Unexpected error (%d)", errorcode); \
223 }
224 
226 #define QPMS_ENSURE(x, msg, ...) {if(QPMS_UNLIKELY(!(x))) qpms_pr_error_at_flf(__FILE__,__LINE__,__func__,msg, ##__VA_ARGS__); }
227 
229 
234 #define QPMS_ASSERT(x) {\
235  if(QPMS_UNLIKELY(!(x)))\
236  qpms_pr_error_at_flf(__FILE__,__LINE__,__func__,\
237  "Unexpected error. This is most certainly a bug.");\
238 }
239 
240 #ifdef QPMS_EVALUATE_PARANOID_ASSERTS
245  #define QPMS_PARANOID_ASSERT(x) {\
246  if(QPMS_UNLIKELY(!(x)))\
247  qpms_pr_error_at_flf(__FILE__,__LINE__,__func__,\
248  "Unexpected error. This is most certainly a bug.");\
249 }
250 #else
251  #define QPMS_PARANOID_ASSERT(x) {;}
252 #endif
253 
255 
256 #define QPMS_NOT_IMPLEMENTED(msg, ...) qpms_pr_error_at_flf(__FILE__,__LINE__,__func__, \
257  "Not implemented:" msg, ##__VA_ARGS__)
258 
260 
261 #define QPMS_INCOMPLETE_IMPLEMENTATION(msg, ...) {\
262  static _Bool already_bitched = 0; \
263  if (QPMS_UNLIKELY(!already_bitched)) {\
264  qpms_warn_at_flf(__FILE__,__LINE__,__func__,msg, ##__VA_ARGS__);\
265  already_bitched = 1;\
266  }\
267 }
268 
269 #endif
Macros for compiler optimisation.
QPMS_NORETURN void qpms_pr_error_at_flf(const char *filename, unsigned int linenum, const char *func, const char *fmt,...)
Print an error message, indicating source, function name and line number, and abort().
Definition: error.c:43
qpms_dbgmsg_flags qpms_dbgmsg_enable(qpms_dbgmsg_flags types)
Disable debugging messages of given types.
Definition: error.c:8
qpms_dbgmsg_flags
Classification of debugging messages.
Definition: qpms_error.h:35
@ QPMS_DBGMSG_INTEGRATION
Quadrature-related debug messages.
Definition: qpms_error.h:38
@ QPMS_DBGMSG_THREADS
Multithreading-related debug messages.
Definition: qpms_error.h:37
QPMS_NORETURN void qpms_pr_error(const char *fmt,...)
Print error message and abort();.
Definition: error.c:16
qpms_dbgmsg_flags qpms_dbgmsg_enabled
Global variable determining which types of debug messages shall be printed with QPMS_DEBUG().
Definition: error.c:6
qpms_dbgmsg_flags qpms_dbgmsg_disable(qpms_dbgmsg_flags types)
Enable debugging messages of given types.
Definition: error.c:12
void qpms_debug_at_flf(const char *filename, unsigned int linenum, const char *func, qpms_dbgmsg_flags type, const char *fmt,...)
Print a debugging message to stderr and flush the buffer. Don't call this directly,...
Definition: error.c:69
void qpms_warn_at_flf(const char *filename, unsigned int linenum, const char *func, const char *fmt,...)
Print a warning message to stderr and flush the buffer. Don't call this directly, use QPMS_WARN().
Definition: error.c:57