Commit | Line | Data |
---|---|---|
826d496d | 1 | /* |
21cf9b6b | 2 | * Copyright (C) 2011 EfficiOS Inc. |
fac6795d | 3 | * |
c922647d | 4 | * SPDX-License-Identifier: LGPL-2.1-only |
d14d33bf | 5 | * |
fac6795d DG |
6 | */ |
7 | ||
db758600 DG |
8 | #ifndef _ERROR_H |
9 | #define _ERROR_H | |
fac6795d | 10 | |
c9e313bc | 11 | #include <common/compat/errno.hpp> |
0038180d JG |
12 | #include <common/compat/time.hpp> |
13 | #include <common/format.hpp> | |
14 | #include <common/macros.hpp> | |
15 | #include <common/string-utils/format.hpp> | |
16 | ||
17 | #include <stdbool.h> | |
f73fabfd | 18 | #include <stdint.h> |
0038180d | 19 | #include <stdio.h> |
d05ea5da | 20 | #include <string.h> |
a0b439da | 21 | #include <urcu/tls-compat.h> |
fac6795d | 22 | |
4c462e79 MD |
23 | #ifndef _GNU_SOURCE |
24 | #error "lttng-tools error.h needs _GNU_SOURCE" | |
25 | #endif | |
26 | ||
c9e313bc | 27 | #include <common/compat/tid.hpp> |
f73fabfd | 28 | |
28f23191 JG |
29 | #include <lttng/lttng-error.h> |
30 | ||
1e9e234a MJ |
31 | /* Avoid conflict with Solaris <sys/regset.h> */ |
32 | #if defined(ERR) && defined(__sun__) | |
33 | #undef ERR | |
34 | #endif | |
35 | ||
57d0d501 DG |
36 | /* Stringify the expansion of a define */ |
37 | #define XSTR(d) STR(d) | |
28f23191 | 38 | #define STR(s) #s |
57d0d501 | 39 | |
a0b439da DG |
40 | /* |
41 | * Contains the string of the log entry time. This is used as a thread local | |
42 | * storage so we don't race between thread and also avoid memory allocation | |
43 | * every time a log is fired. | |
44 | */ | |
45 | struct log_time { | |
870bdaf9 JD |
46 | /* Format: 00:00:00.000000000 plus NULL byte. */ |
47 | char str[19]; | |
a0b439da | 48 | }; |
9fd09ab6 | 49 | |
16ff37cb | 50 | extern thread_local const char *logger_thread_name; |
a0b439da | 51 | |
97e19046 DG |
52 | extern int lttng_opt_quiet; |
53 | extern int lttng_opt_verbose; | |
c7e35b03 | 54 | extern int lttng_opt_mi; |
fac6795d | 55 | |
ffe60014 | 56 | /* Error type. */ |
ce69c47f | 57 | enum lttng_error_level { |
28f23191 JG |
58 | PRINT_ERR = 0, |
59 | PRINT_BUG = 1, | |
60 | PRINT_WARN = 2, | |
61 | PRINT_MSG = 3, | |
62 | PRINT_DBG = 4, | |
63 | PRINT_DBG2 = 5, | |
64 | PRINT_DBG3 = 6, | |
ab530aaf MD |
65 | }; |
66 | ||
ce69c47f | 67 | static inline bool __lttng_print_check_opt(enum lttng_error_level type) |
ab530aaf MD |
68 | { |
69 | /* lttng_opt_mi and lttng_opt_quiet. */ | |
70 | switch (type) { | |
71 | case PRINT_DBG3: | |
72 | case PRINT_DBG2: | |
73 | case PRINT_DBG: | |
74 | case PRINT_MSG: | |
75 | if (lttng_opt_mi) { | |
76 | return false; | |
77 | } | |
78 | /* Fall-through. */ | |
79 | case PRINT_WARN: | |
80 | case PRINT_BUG: | |
81 | case PRINT_ERR: | |
82 | if (lttng_opt_quiet) { | |
83 | return false; | |
84 | } | |
85 | } | |
86 | ||
87 | /* lttng_opt_verbose */ | |
88 | switch (type) { | |
89 | case PRINT_DBG3: | |
90 | if (lttng_opt_verbose < 3) { | |
91 | return false; | |
92 | } | |
93 | break; | |
94 | case PRINT_DBG2: | |
95 | if (lttng_opt_verbose < 2) { | |
96 | return false; | |
97 | } | |
98 | break; | |
99 | case PRINT_DBG: | |
100 | if (lttng_opt_verbose < 1) { | |
101 | return false; | |
102 | } | |
103 | break; | |
104 | case PRINT_MSG: | |
105 | case PRINT_WARN: | |
106 | case PRINT_BUG: | |
107 | case PRINT_ERR: | |
108 | break; | |
109 | } | |
110 | ||
111 | return true; | |
112 | } | |
113 | ||
d50d200a | 114 | C_LINKAGE void lttng_abort_on_error(void); |
ab530aaf | 115 | |
ce69c47f | 116 | static inline void __lttng_print_check_abort(enum lttng_error_level type) |
ab530aaf MD |
117 | { |
118 | switch (type) { | |
119 | case PRINT_DBG3: | |
120 | case PRINT_DBG2: | |
121 | case PRINT_DBG: | |
122 | case PRINT_MSG: | |
123 | case PRINT_WARN: | |
124 | break; | |
125 | case PRINT_BUG: | |
126 | case PRINT_ERR: | |
127 | lttng_abort_on_error(); | |
128 | } | |
129 | } | |
fac6795d DG |
130 | |
131 | /* | |
75dea654 | 132 | * Macro for printing message depending on command line option and verbosity. |
c7e35b03 JR |
133 | * |
134 | * Machine interface: | |
135 | * We use lttng_opt_mi to suppress all normal msg to stdout. We don't | |
136 | * want any nested msg to show up when printing mi to stdout(if it's the case). | |
137 | * All warnings and errors should be printed to stderr as normal. | |
fac6795d | 138 | */ |
28f23191 JG |
139 | #define __lttng_print(type, fmt, args...) \ |
140 | do { \ | |
141 | if (__lttng_print_check_opt(type)) { \ | |
142 | fprintf((type) == PRINT_MSG ? stdout : stderr, fmt, ##args); \ | |
143 | } \ | |
144 | __lttng_print_check_abort(type); \ | |
ab530aaf | 145 | } while (0) |
fac6795d | 146 | |
ffe60014 | 147 | /* Three level of debug. Use -v, -vv or -vvv for the levels */ |
28f23191 JG |
148 | #define _ERRMSG(msg, type, fmt, args...) \ |
149 | do { \ | |
150 | if (caa_unlikely(__lttng_print_check_opt(type))) { \ | |
151 | char generic_name[MAX_INT_DEC_LEN(long) + MAX_INT_DEC_LEN(long)]; \ | |
152 | \ | |
153 | snprintf(generic_name, \ | |
154 | sizeof(generic_name), \ | |
155 | "%ld/%ld", \ | |
156 | (long) getpid(), \ | |
157 | (long) lttng_gettid()); \ | |
158 | \ | |
159 | __lttng_print(type, \ | |
160 | msg " - %s [%s]: " fmt " (in %s() at " __FILE__ \ | |
161 | ":" XSTR(__LINE__) ")\n", \ | |
162 | log_add_time(), \ | |
9fd09ab6 | 163 | logger_thread_name ?: generic_name, \ |
28f23191 JG |
164 | ##args, \ |
165 | __func__); \ | |
166 | } \ | |
f5fb86c1 | 167 | } while (0) |
ffe60014 | 168 | |
28f23191 JG |
169 | #define _ERRMSG_NO_LOC(msg, type, fmt, args...) \ |
170 | do { \ | |
171 | if (caa_unlikely(__lttng_print_check_opt(type))) { \ | |
172 | char generic_name[MAX_INT_DEC_LEN(long) + MAX_INT_DEC_LEN(long)]; \ | |
173 | \ | |
174 | snprintf(generic_name, \ | |
175 | sizeof(generic_name), \ | |
176 | "%ld/%ld", \ | |
177 | (long) getpid(), \ | |
178 | (long) lttng_gettid()); \ | |
179 | \ | |
180 | __lttng_print(type, \ | |
181 | msg " - %s [%s]: " fmt "\n", \ | |
182 | log_add_time(), \ | |
9fd09ab6 | 183 | logger_thread_name ?: generic_name, \ |
28f23191 JG |
184 | ##args); \ |
185 | } \ | |
f5fb86c1 | 186 | } while (0) |
e6142f2e | 187 | |
28f23191 JG |
188 | #define MSG(fmt, args...) __lttng_print(PRINT_MSG, fmt "\n", ##args) |
189 | #define _MSG(fmt, args...) __lttng_print(PRINT_MSG, fmt, ##args) | |
190 | #define ERR(fmt, args...) __lttng_print(PRINT_ERR, "Error: " fmt "\n", ##args) | |
191 | #define WARN(fmt, args...) __lttng_print(PRINT_WARN, "Warning: " fmt "\n", ##args) | |
75dea654 | 192 | |
28f23191 | 193 | #define BUG(fmt, args...) _ERRMSG("BUG", PRINT_BUG, fmt, ##args) |
ffe60014 | 194 | |
28f23191 JG |
195 | #define DBG(fmt, args...) _ERRMSG("DBG1", PRINT_DBG, fmt, ##args) |
196 | #define DBG_NO_LOC(fmt, args...) _ERRMSG_NO_LOC("DBG1", PRINT_DBG, fmt, ##args) | |
197 | #define DBG2(fmt, args...) _ERRMSG("DBG2", PRINT_DBG2, fmt, ##args) | |
198 | #define DBG3(fmt, args...) _ERRMSG("DBG3", PRINT_DBG3, fmt, ##args) | |
199 | #define LOG(type, fmt, args...) \ | |
200 | do { \ | |
201 | switch (type) { \ | |
202 | case PRINT_ERR: \ | |
203 | ERR(fmt, ##args); \ | |
204 | break; \ | |
205 | case PRINT_WARN: \ | |
206 | WARN(fmt, ##args); \ | |
207 | break; \ | |
208 | case PRINT_BUG: \ | |
209 | BUG(fmt, ##args); \ | |
210 | break; \ | |
211 | case PRINT_MSG: \ | |
212 | MSG(fmt, ##args); \ | |
213 | break; \ | |
214 | case PRINT_DBG: \ | |
215 | DBG(fmt, ##args); \ | |
216 | break; \ | |
217 | case PRINT_DBG2: \ | |
218 | DBG2(fmt, ##args); \ | |
219 | break; \ | |
220 | case PRINT_DBG3: \ | |
221 | DBG3(fmt, ##args); \ | |
222 | break; \ | |
223 | default: \ | |
224 | abort(); \ | |
225 | } \ | |
226 | } while (0); | |
ffe60014 | 227 | |
28f23191 | 228 | #define _PERROR(fmt, args...) _ERRMSG("PERROR", PRINT_ERR, fmt, ##args) |
75dea654 | 229 | |
28f23191 JG |
230 | #if !defined(__GLIBC__) || \ |
231 | ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE)) | |
97e19046 | 232 | |
818d276f MD |
233 | /* |
234 | * Version using XSI strerror_r. | |
235 | */ | |
0c818c97 SM |
236 | #define PERROR(call, args...) \ |
237 | do { \ | |
238 | char _perror_buf[200]; \ | |
239 | strerror_r(errno, _perror_buf, sizeof(_perror_buf)); \ | |
240 | _PERROR(call ": %s", ##args, _perror_buf); \ | |
241 | } while (0); | |
818d276f MD |
242 | #else |
243 | /* | |
244 | * Version using GNU strerror_r, for linux with appropriate defines. | |
245 | */ | |
28f23191 JG |
246 | #define PERROR(call, args...) \ |
247 | do { \ | |
248 | char *_perror_buf; \ | |
249 | char _perror_tmp[200]; \ | |
250 | _perror_buf = strerror_r(errno, _perror_tmp, sizeof(_perror_tmp)); \ | |
251 | _PERROR(call ": %s", ##args, _perror_buf); \ | |
0c818c97 | 252 | } while (0); |
818d276f | 253 | #endif |
fac6795d | 254 | |
2441eac3 MJ |
255 | const char *error_get_str(int32_t code); |
256 | ||
003f455d JG |
257 | namespace lttng { |
258 | namespace logging { | |
259 | namespace details { | |
260 | [[noreturn]] void die_formatting_exception(const char *format, | |
261 | const std::exception& formatting_exception); | |
262 | } /* namespace details */ | |
263 | } /* namespace logging */ | |
264 | } /* namespace lttng */ | |
0038180d | 265 | |
003f455d JG |
266 | #define DBG_FMT(format_str, args...) \ |
267 | do { \ | |
268 | try { \ | |
f9a41357 | 269 | DBG("%s", lttng::format(format_str, ##args).c_str()); \ |
003f455d JG |
270 | } catch (const std::exception& _formatting_exception) { \ |
271 | lttng::logging::details::die_formatting_exception(format_str, \ | |
272 | _formatting_exception); \ | |
273 | } \ | |
274 | } while (0); | |
275 | ||
276 | #define WARN_FMT(format_str, args...) \ | |
277 | do { \ | |
278 | try { \ | |
f9a41357 | 279 | WARN("%s", lttng::format(format_str, ##args).c_str()); \ |
003f455d JG |
280 | } catch (const std::exception& _formatting_exception) { \ |
281 | lttng::logging::details::die_formatting_exception(format_str, \ | |
282 | _formatting_exception); \ | |
283 | } \ | |
284 | } while (0); | |
285 | ||
286 | #define ERR_FMT(format_str, args...) \ | |
287 | do { \ | |
288 | try { \ | |
f9a41357 | 289 | ERR("%s", lttng::format(format_str, ##args).c_str()); \ |
003f455d JG |
290 | } catch (const std::exception& _formatting_exception) { \ |
291 | lttng::logging::details::die_formatting_exception(format_str, \ | |
292 | _formatting_exception); \ | |
293 | } \ | |
294 | } while (0); | |
f73fabfd | 295 | |
a0b439da DG |
296 | /* |
297 | * Function that format the time and return the reference of log_time.str to | |
298 | * the caller. On error, an empty string is returned thus no time will be | |
299 | * printed in the log. | |
300 | */ | |
cd9adb8b | 301 | const char *log_add_time(); |
a0b439da | 302 | |
f5fb86c1 | 303 | /* Name must be a statically-allocated string. */ |
f5fb86c1 JG |
304 | void logger_set_thread_name(const char *name, bool set_pthread_name); |
305 | ||
db758600 | 306 | #endif /* _ERROR_H */ |