Commit | Line | Data |
---|---|---|
59b161cd PMF |
1 | #ifndef USTERR_H |
2 | #define USTERR_H | |
3 | ||
d61bfa72 PMF |
4 | #include <string.h> |
5 | ||
2028e7fd PMF |
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 | |
17674885 | 18 | #ifdef DEBUG |
d61bfa72 | 19 | # define DBG(fmt, args...) do { fprintf(stderr, UST_STR_COMPONENT ": " fmt " (" __FILE__ ":" XSTR(__LINE__) ")\n", ## args); fflush(stderr); } while(0) |
17674885 PMF |
20 | #else |
21 | # define DBG(fmt, args...) do {} while(0) | |
22 | #endif | |
2028e7fd PMF |
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) | |
d61bfa72 PMF |
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 | |
c9b64079 PMF |
43 | |
44 | #define BUG_ON(condition) do { if (unlikely(condition)) ERR("condition not respected (BUG)"); } while(0) | |
5f54827b | 45 | #define WARN_ON(condition) do { if (unlikely(condition)) WARN("condition not respected on line %s:%d", __FILE__, __LINE__); } while(0) |
c9b64079 | 46 | |
59b161cd | 47 | #endif /* USTERR_H */ |