Commit | Line | Data |
---|---|---|
5e96a467 MD |
1 | #ifndef _USTERR_H |
2 | #define _USTERR_H | |
3 | ||
4 | /* | |
5 | * Copyright (C) 2009 Pierre-Marc Fournier | |
6 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
a09dac63 PMF |
7 | * |
8 | * This library is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
5e96a467 MD |
10 | * License as published by the Free Software Foundation; version 2.1 of |
11 | * the License. | |
a09dac63 PMF |
12 | * |
13 | * This library is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * Lesser General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU Lesser General Public | |
19 | * License along with this library; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
21 | */ | |
22 | ||
d61bfa72 | 23 | #include <string.h> |
dd80aafe PMF |
24 | #include <sys/types.h> |
25 | #include <sys/syscall.h> | |
26 | #include <errno.h> | |
ff1fedb9 | 27 | #include <stdarg.h> |
c1f20530 | 28 | #include <stdio.h> |
ff1fedb9 | 29 | |
4318ae1b MD |
30 | #include <lttng/core.h> |
31 | #include "lttng/share.h" | |
d61bfa72 | 32 | |
5e96a467 MD |
33 | enum ust_loglevel { |
34 | UST_LOGLEVEL_UNKNOWN = 0, | |
35 | UST_LOGLEVEL_NORMAL, | |
36 | UST_LOGLEVEL_DEBUG, | |
37 | }; | |
38 | ||
39 | extern volatile enum ust_loglevel ust_loglevel; | |
40 | void init_usterr(void); | |
41 | ||
42 | static inline int ust_debug(void) | |
43 | { | |
44 | return ust_loglevel == UST_LOGLEVEL_DEBUG; | |
45 | } | |
46 | ||
2028e7fd | 47 | #ifndef UST_COMPONENT |
2028e7fd PMF |
48 | #define UST_COMPONENT libust |
49 | #endif | |
50 | ||
51 | /* To stringify the expansion of a define */ | |
52 | #define XSTR(d) STR(d) | |
53 | #define STR(s) #s | |
54 | ||
55 | #define UST_STR_COMPONENT XSTR(UST_COMPONENT) | |
56 | ||
5e96a467 MD |
57 | #define ERRMSG(fmt, args...) \ |
58 | do { \ | |
59 | fprintf(stderr, UST_STR_COMPONENT "[%ld/%ld]: " fmt " (in %s() at " __FILE__ ":" XSTR(__LINE__) ")\n", \ | |
60 | (long) getpid(), \ | |
61 | (long) syscall(SYS_gettid), \ | |
62 | ## args, \ | |
63 | __func__); \ | |
64 | } while(0) | |
dd80aafe | 65 | |
69400ac4 | 66 | #ifdef LTTNG_UST_DEBUG |
5e96a467 MD |
67 | # define DBG(fmt, args...) ERRMSG(fmt, ## args) |
68 | # define DBG_raw(fmt, args...) \ | |
69 | do { \ | |
70 | fprintf(stderr, fmt, ## args); \ | |
71 | } while(0) | |
17674885 | 72 | #else |
5e96a467 MD |
73 | # define DBG(fmt, args...) \ |
74 | do { \ | |
75 | if (ust_debug()) \ | |
76 | ERRMSG(fmt, ## args); \ | |
77 | } while (0) | |
78 | # define DBG_raw(fmt, args...) \ | |
79 | do { \ | |
80 | if (ust_debug()) { \ | |
81 | fprintf(stderr, fmt, ## args); \ | |
82 | } \ | |
83 | } while(0) | |
17674885 | 84 | #endif |
5e96a467 | 85 | |
dd80aafe PMF |
86 | #define WARN(fmt, args...) ERRMSG("Warning: " fmt, ## args) |
87 | #define ERR(fmt, args...) ERRMSG("Error: " fmt, ## args) | |
88 | #define BUG(fmt, args...) ERRMSG("BUG: " fmt, ## args) | |
d61bfa72 PMF |
89 | |
90 | #if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE) | |
91 | #define PERROR(call, args...)\ | |
92 | do { \ | |
93 | char buf[200] = "Error in strerror_r()"; \ | |
94 | strerror_r(errno, buf, sizeof(buf)); \ | |
dd80aafe | 95 | ERRMSG("Error: " call ": %s", ## args, buf); \ |
d61bfa72 PMF |
96 | } while(0); |
97 | #else | |
98 | #define PERROR(call, args...)\ | |
99 | do { \ | |
100 | char *buf; \ | |
101 | char tmp[200]; \ | |
102 | buf = strerror_r(errno, tmp, sizeof(tmp)); \ | |
dd80aafe | 103 | ERRMSG("Error: " call ": %s", ## args, buf); \ |
d61bfa72 PMF |
104 | } while(0); |
105 | #endif | |
c9b64079 | 106 | |
5e96a467 MD |
107 | #define BUG_ON(condition) \ |
108 | do { \ | |
b5a3dfa5 | 109 | if (caa_unlikely(condition)) \ |
5e96a467 MD |
110 | ERR("condition not respected (BUG) on line %s:%d", __FILE__, __LINE__); \ |
111 | } while(0) | |
112 | #define WARN_ON(condition) \ | |
113 | do { \ | |
b5a3dfa5 | 114 | if (caa_unlikely(condition)) \ |
5e96a467 MD |
115 | WARN("condition not respected on line %s:%d", __FILE__, __LINE__); \ |
116 | } while(0) | |
d6322067 | 117 | #define WARN_ON_ONCE(condition) WARN_ON(condition) |
c9b64079 | 118 | |
30ffe279 | 119 | #endif /* _USTERR_H */ |