improve error handling macros
[ust.git] / share / usterr.h
1 #ifndef USTERR_H
2 #define USTERR_H
3
4 #include <string.h>
5
6 #ifndef UST_COMPONENT
7 //#error UST_COMPONENT is undefined
8 #define UST_COMPONENT libust
9 #endif
10
11 /* To stringify the expansion of a define */
12 #define XSTR(d) STR(d)
13 #define STR(s) #s
14
15 #define UST_STR_COMPONENT XSTR(UST_COMPONENT)
16
17 #define DEBUG
18 #ifdef DEBUG
19 # define DBG(fmt, args...) do { fprintf(stderr, UST_STR_COMPONENT ": " fmt " (" __FILE__ ":" XSTR(__LINE__) ")\n", ## args); fflush(stderr); } while(0)
20 #else
21 # define DBG(fmt, args...) do {} while(0)
22 #endif
23 #define WARN(fmt, args...) fprintf(stderr, UST_STR_COMPONENT ": Warning: " fmt "\n", ## args); fflush(stderr)
24 #define ERR(fmt, args...) fprintf(stderr, UST_STR_COMPONENT ": Error: " fmt "\n", ## args); fflush(stderr)
25 #define BUG(fmt, args...) fprintf(stderr, UST_STR_COMPONENT ": BUG: " fmt "\n", ## args); fflush(stderr)
26
27 #if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE)
28 #define PERROR(call, args...)\
29 do { \
30 char buf[200] = "Error in strerror_r()"; \
31 strerror_r(errno, buf, sizeof(buf)); \
32 fprintf(stderr, UST_STR_COMPONENT ": Error: " call ": %s\n", ## args, buf); fflush(stderr); \
33 } while(0);
34 #else
35 #define PERROR(call, args...)\
36 do { \
37 char *buf; \
38 char tmp[200]; \
39 buf = strerror_r(errno, tmp, sizeof(tmp)); \
40 fprintf(stderr, UST_STR_COMPONENT ": Error: " call ": %s\n", ## args, buf); fflush(stderr); \
41 } while(0);
42 #endif
43
44 #define BUG_ON(condition) do { if (unlikely(condition)) ERR("condition not respected (BUG)"); } while(0)
45 #define WARN_ON(condition) do { if (unlikely(condition)) WARN("condition not respected on line %s:%d", __FILE__, __LINE__); } while(0)
46
47 #endif /* USTERR_H */
This page took 0.030352 seconds and 5 git commands to generate.