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