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 | }; |
ca806b0b | 49 | extern DECLARE_URCU_TLS(const char *, logger_thread_name); |
a0b439da | 50 | |
97e19046 DG |
51 | extern int lttng_opt_quiet; |
52 | extern int lttng_opt_verbose; | |
c7e35b03 | 53 | extern int lttng_opt_mi; |
fac6795d | 54 | |
ffe60014 | 55 | /* Error type. */ |
ce69c47f | 56 | enum lttng_error_level { |
28f23191 JG |
57 | PRINT_ERR = 0, |
58 | PRINT_BUG = 1, | |
59 | PRINT_WARN = 2, | |
60 | PRINT_MSG = 3, | |
61 | PRINT_DBG = 4, | |
62 | PRINT_DBG2 = 5, | |
63 | PRINT_DBG3 = 6, | |
ab530aaf MD |
64 | }; |
65 | ||
ce69c47f | 66 | static inline bool __lttng_print_check_opt(enum lttng_error_level type) |
ab530aaf MD |
67 | { |
68 | /* lttng_opt_mi and lttng_opt_quiet. */ | |
69 | switch (type) { | |
70 | case PRINT_DBG3: | |
71 | case PRINT_DBG2: | |
72 | case PRINT_DBG: | |
73 | case PRINT_MSG: | |
74 | if (lttng_opt_mi) { | |
75 | return false; | |
76 | } | |
77 | /* Fall-through. */ | |
78 | case PRINT_WARN: | |
79 | case PRINT_BUG: | |
80 | case PRINT_ERR: | |
81 | if (lttng_opt_quiet) { | |
82 | return false; | |
83 | } | |
84 | } | |
85 | ||
86 | /* lttng_opt_verbose */ | |
87 | switch (type) { | |
88 | case PRINT_DBG3: | |
89 | if (lttng_opt_verbose < 3) { | |
90 | return false; | |
91 | } | |
92 | break; | |
93 | case PRINT_DBG2: | |
94 | if (lttng_opt_verbose < 2) { | |
95 | return false; | |
96 | } | |
97 | break; | |
98 | case PRINT_DBG: | |
99 | if (lttng_opt_verbose < 1) { | |
100 | return false; | |
101 | } | |
102 | break; | |
103 | case PRINT_MSG: | |
104 | case PRINT_WARN: | |
105 | case PRINT_BUG: | |
106 | case PRINT_ERR: | |
107 | break; | |
108 | } | |
109 | ||
110 | return true; | |
111 | } | |
112 | ||
d50d200a | 113 | C_LINKAGE void lttng_abort_on_error(void); |
ab530aaf | 114 | |
ce69c47f | 115 | static inline void __lttng_print_check_abort(enum lttng_error_level type) |
ab530aaf MD |
116 | { |
117 | switch (type) { | |
118 | case PRINT_DBG3: | |
119 | case PRINT_DBG2: | |
120 | case PRINT_DBG: | |
121 | case PRINT_MSG: | |
122 | case PRINT_WARN: | |
123 | break; | |
124 | case PRINT_BUG: | |
125 | case PRINT_ERR: | |
126 | lttng_abort_on_error(); | |
127 | } | |
128 | } | |
fac6795d DG |
129 | |
130 | /* | |
75dea654 | 131 | * Macro for printing message depending on command line option and verbosity. |
c7e35b03 JR |
132 | * |
133 | * Machine interface: | |
134 | * We use lttng_opt_mi to suppress all normal msg to stdout. We don't | |
135 | * want any nested msg to show up when printing mi to stdout(if it's the case). | |
136 | * All warnings and errors should be printed to stderr as normal. | |
fac6795d | 137 | */ |
28f23191 JG |
138 | #define __lttng_print(type, fmt, args...) \ |
139 | do { \ | |
140 | if (__lttng_print_check_opt(type)) { \ | |
141 | fprintf((type) == PRINT_MSG ? stdout : stderr, fmt, ##args); \ | |
142 | } \ | |
143 | __lttng_print_check_abort(type); \ | |
ab530aaf | 144 | } while (0) |
fac6795d | 145 | |
ffe60014 | 146 | /* Three level of debug. Use -v, -vv or -vvv for the levels */ |
28f23191 JG |
147 | #define _ERRMSG(msg, type, fmt, args...) \ |
148 | do { \ | |
149 | if (caa_unlikely(__lttng_print_check_opt(type))) { \ | |
150 | char generic_name[MAX_INT_DEC_LEN(long) + MAX_INT_DEC_LEN(long)]; \ | |
151 | \ | |
152 | snprintf(generic_name, \ | |
153 | sizeof(generic_name), \ | |
154 | "%ld/%ld", \ | |
155 | (long) getpid(), \ | |
156 | (long) lttng_gettid()); \ | |
157 | \ | |
158 | __lttng_print(type, \ | |
159 | msg " - %s [%s]: " fmt " (in %s() at " __FILE__ \ | |
160 | ":" XSTR(__LINE__) ")\n", \ | |
161 | log_add_time(), \ | |
162 | URCU_TLS(logger_thread_name) ?: generic_name, \ | |
163 | ##args, \ | |
164 | __func__); \ | |
165 | } \ | |
f5fb86c1 | 166 | } while (0) |
ffe60014 | 167 | |
28f23191 JG |
168 | #define _ERRMSG_NO_LOC(msg, type, fmt, args...) \ |
169 | do { \ | |
170 | if (caa_unlikely(__lttng_print_check_opt(type))) { \ | |
171 | char generic_name[MAX_INT_DEC_LEN(long) + MAX_INT_DEC_LEN(long)]; \ | |
172 | \ | |
173 | snprintf(generic_name, \ | |
174 | sizeof(generic_name), \ | |
175 | "%ld/%ld", \ | |
176 | (long) getpid(), \ | |
177 | (long) lttng_gettid()); \ | |
178 | \ | |
179 | __lttng_print(type, \ | |
180 | msg " - %s [%s]: " fmt "\n", \ | |
181 | log_add_time(), \ | |
182 | URCU_TLS(logger_thread_name) ?: generic_name, \ | |
183 | ##args); \ | |
184 | } \ | |
f5fb86c1 | 185 | } while (0) |
e6142f2e | 186 | |
28f23191 JG |
187 | #define MSG(fmt, args...) __lttng_print(PRINT_MSG, fmt "\n", ##args) |
188 | #define _MSG(fmt, args...) __lttng_print(PRINT_MSG, fmt, ##args) | |
189 | #define ERR(fmt, args...) __lttng_print(PRINT_ERR, "Error: " fmt "\n", ##args) | |
190 | #define WARN(fmt, args...) __lttng_print(PRINT_WARN, "Warning: " fmt "\n", ##args) | |
75dea654 | 191 | |
28f23191 | 192 | #define BUG(fmt, args...) _ERRMSG("BUG", PRINT_BUG, fmt, ##args) |
ffe60014 | 193 | |
28f23191 JG |
194 | #define DBG(fmt, args...) _ERRMSG("DBG1", PRINT_DBG, fmt, ##args) |
195 | #define DBG_NO_LOC(fmt, args...) _ERRMSG_NO_LOC("DBG1", PRINT_DBG, fmt, ##args) | |
196 | #define DBG2(fmt, args...) _ERRMSG("DBG2", PRINT_DBG2, fmt, ##args) | |
197 | #define DBG3(fmt, args...) _ERRMSG("DBG3", PRINT_DBG3, fmt, ##args) | |
198 | #define LOG(type, fmt, args...) \ | |
199 | do { \ | |
200 | switch (type) { \ | |
201 | case PRINT_ERR: \ | |
202 | ERR(fmt, ##args); \ | |
203 | break; \ | |
204 | case PRINT_WARN: \ | |
205 | WARN(fmt, ##args); \ | |
206 | break; \ | |
207 | case PRINT_BUG: \ | |
208 | BUG(fmt, ##args); \ | |
209 | break; \ | |
210 | case PRINT_MSG: \ | |
211 | MSG(fmt, ##args); \ | |
212 | break; \ | |
213 | case PRINT_DBG: \ | |
214 | DBG(fmt, ##args); \ | |
215 | break; \ | |
216 | case PRINT_DBG2: \ | |
217 | DBG2(fmt, ##args); \ | |
218 | break; \ | |
219 | case PRINT_DBG3: \ | |
220 | DBG3(fmt, ##args); \ | |
221 | break; \ | |
222 | default: \ | |
223 | abort(); \ | |
224 | } \ | |
225 | } while (0); | |
ffe60014 | 226 | |
28f23191 | 227 | #define _PERROR(fmt, args...) _ERRMSG("PERROR", PRINT_ERR, fmt, ##args) |
75dea654 | 228 | |
28f23191 JG |
229 | #if !defined(__GLIBC__) || \ |
230 | ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE)) | |
97e19046 | 231 | |
818d276f MD |
232 | /* |
233 | * Version using XSI strerror_r. | |
234 | */ | |
0c818c97 SM |
235 | #define PERROR(call, args...) \ |
236 | do { \ | |
237 | char _perror_buf[200]; \ | |
238 | strerror_r(errno, _perror_buf, sizeof(_perror_buf)); \ | |
239 | _PERROR(call ": %s", ##args, _perror_buf); \ | |
240 | } while (0); | |
818d276f MD |
241 | #else |
242 | /* | |
243 | * Version using GNU strerror_r, for linux with appropriate defines. | |
244 | */ | |
003f455d | 245 | const char *error_get_str(int32_t code); |
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 | |
003f455d JG |
255 | namespace lttng { |
256 | namespace logging { | |
257 | namespace details { | |
258 | [[noreturn]] void die_formatting_exception(const char *format, | |
259 | const std::exception& formatting_exception); | |
260 | } /* namespace details */ | |
261 | } /* namespace logging */ | |
262 | } /* namespace lttng */ | |
0038180d | 263 | |
003f455d JG |
264 | #define DBG_FMT(format_str, args...) \ |
265 | do { \ | |
266 | try { \ | |
267 | DBG("%s", fmt::format(format_str, ##args).c_str()); \ | |
268 | } catch (const std::exception& _formatting_exception) { \ | |
269 | lttng::logging::details::die_formatting_exception(format_str, \ | |
270 | _formatting_exception); \ | |
271 | } \ | |
272 | } while (0); | |
273 | ||
274 | #define WARN_FMT(format_str, args...) \ | |
275 | do { \ | |
276 | try { \ | |
277 | WARN("%s", fmt::format(format_str, ##args).c_str()); \ | |
278 | } catch (const std::exception& _formatting_exception) { \ | |
279 | lttng::logging::details::die_formatting_exception(format_str, \ | |
280 | _formatting_exception); \ | |
281 | } \ | |
282 | } while (0); | |
283 | ||
284 | #define ERR_FMT(format_str, args...) \ | |
285 | do { \ | |
286 | try { \ | |
287 | ERR("%s", fmt::format(format_str, ##args).c_str()); \ | |
288 | } catch (const std::exception& _formatting_exception) { \ | |
289 | lttng::logging::details::die_formatting_exception(format_str, \ | |
290 | _formatting_exception); \ | |
291 | } \ | |
292 | } while (0); | |
f73fabfd | 293 | |
a0b439da DG |
294 | /* |
295 | * Function that format the time and return the reference of log_time.str to | |
296 | * the caller. On error, an empty string is returned thus no time will be | |
297 | * printed in the log. | |
298 | */ | |
cd9adb8b | 299 | const char *log_add_time(); |
a0b439da | 300 | |
f5fb86c1 | 301 | /* Name must be a statically-allocated string. */ |
f5fb86c1 JG |
302 | void logger_set_thread_name(const char *name, bool set_pthread_name); |
303 | ||
db758600 | 304 | #endif /* _ERROR_H */ |