1819f973069b99f670fd5411f6d7809c35daffee
[lttng-ust.git] / include / usterr.h
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 #include <stdarg.h>
9
10 #include "share.h"
11
12 #ifndef UST_COMPONENT
13 //#error UST_COMPONENT is undefined
14 #define UST_COMPONENT libust
15 #endif
16
17 /* To stringify the expansion of a define */
18 #define XSTR(d) STR(d)
19 #define STR(s) #s
20
21 /* We sometimes print in the tracing path, and tracing can occur in
22 * signal handlers, so we must use a print method which is signal safe.
23 */
24
25 #define sigsafe_print_err(fmt, args...) \
26 { \
27 /* Can't use dynamic allocation. Limit ourselves to 250 chars. */ \
28 char ____buf[250]; \
29 int ____saved_errno; \
30 \
31 /* Save the errno. */ \
32 ____saved_errno = errno; \
33 \
34 snprintf(____buf, sizeof(____buf), fmt, ## args); \
35 \
36 /* Add end of string in case of buffer overflow. */ \
37 ____buf[sizeof(____buf)-1] = 0; \
38 \
39 patient_write(STDERR_FILENO, ____buf, strlen(____buf)); \
40 /* Can't print errors because we are in the error printing code path. */ \
41 \
42 /* Restore errno, in order to be async-signal safe. */ \
43 errno = ____saved_errno; \
44 }
45
46 #define UST_STR_COMPONENT XSTR(UST_COMPONENT)
47
48 #define ERRMSG(fmt, args...) do { sigsafe_print_err(UST_STR_COMPONENT "[%ld/%ld]: " fmt " (" __FILE__ ":" XSTR(__LINE__) ")\n", (long) getpid(), (long) syscall(SYS_gettid), ## args); fflush(stderr); } while(0)
49
50 #define DEBUG
51 #ifdef DEBUG
52 # define DBG(fmt, args...) ERRMSG(fmt, ## args)
53 #else
54 # define DBG(fmt, args...) do {} while(0)
55 #endif
56 #define WARN(fmt, args...) ERRMSG("Warning: " fmt, ## args)
57 #define ERR(fmt, args...) ERRMSG("Error: " fmt, ## args)
58 #define BUG(fmt, args...) ERRMSG("BUG: " fmt, ## args)
59
60 #if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE)
61 #define PERROR(call, args...)\
62 do { \
63 char buf[200] = "Error in strerror_r()"; \
64 strerror_r(errno, buf, sizeof(buf)); \
65 ERRMSG("Error: " call ": %s", ## args, buf); \
66 } while(0);
67 #else
68 #define PERROR(call, args...)\
69 do { \
70 char *buf; \
71 char tmp[200]; \
72 buf = strerror_r(errno, tmp, sizeof(tmp)); \
73 ERRMSG("Error: " call ": %s", ## args, buf); \
74 } while(0);
75 #endif
76
77 #define BUG_ON(condition) do { if (unlikely(condition)) ERR("condition not respected (BUG)"); } while(0)
78 #define WARN_ON(condition) do { if (unlikely(condition)) WARN("condition not respected on line %s:%d", __FILE__, __LINE__); } while(0)
79
80 #endif /* USTERR_H */
This page took 0.03427 seconds and 4 git commands to generate.