Replace uses of the URCU tls helpers by the standard thread_local
[lttng-tools.git] / src / common / error.hpp
1 /*
2 * Copyright (C) 2011 EfficiOS Inc.
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #ifndef _ERROR_H
9 #define _ERROR_H
10
11 #include <common/compat/errno.hpp>
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>
18 #include <stdint.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <urcu/tls-compat.h>
22
23 #ifndef _GNU_SOURCE
24 #error "lttng-tools error.h needs _GNU_SOURCE"
25 #endif
26
27 #include <common/compat/tid.hpp>
28
29 #include <lttng/lttng-error.h>
30
31 /* Avoid conflict with Solaris <sys/regset.h> */
32 #if defined(ERR) && defined(__sun__)
33 #undef ERR
34 #endif
35
36 /* Stringify the expansion of a define */
37 #define XSTR(d) STR(d)
38 #define STR(s) #s
39
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 {
46 /* Format: 00:00:00.000000000 plus NULL byte. */
47 char str[19];
48 };
49
50 extern thread_local const char * logger_thread_name;
51
52 extern int lttng_opt_quiet;
53 extern int lttng_opt_verbose;
54 extern int lttng_opt_mi;
55
56 /* Error type. */
57 enum lttng_error_level {
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,
65 };
66
67 static inline bool __lttng_print_check_opt(enum lttng_error_level type)
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
114 C_LINKAGE void lttng_abort_on_error(void);
115
116 static inline void __lttng_print_check_abort(enum lttng_error_level type)
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 }
130
131 /*
132 * Macro for printing message depending on command line option and verbosity.
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.
138 */
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); \
145 } while (0)
146
147 /* Three level of debug. Use -v, -vv or -vvv for the levels */
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(), \
163 logger_thread_name ?: generic_name, \
164 ##args, \
165 __func__); \
166 } \
167 } while (0)
168
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(), \
183 logger_thread_name ?: generic_name, \
184 ##args); \
185 } \
186 } while (0)
187
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)
192
193 #define BUG(fmt, args...) _ERRMSG("BUG", PRINT_BUG, fmt, ##args)
194
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);
227
228 #define _PERROR(fmt, args...) _ERRMSG("PERROR", PRINT_ERR, fmt, ##args)
229
230 #if !defined(__GLIBC__) || \
231 ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE))
232
233 /*
234 * Version using XSI strerror_r.
235 */
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);
242 #else
243 /*
244 * Version using GNU strerror_r, for linux with appropriate defines.
245 */
246 const char *error_get_str(int32_t code);
247 #define PERROR(call, args...) \
248 do { \
249 char *_perror_buf; \
250 char _perror_tmp[200]; \
251 _perror_buf = strerror_r(errno, _perror_tmp, sizeof(_perror_tmp)); \
252 _PERROR(call ": %s", ##args, _perror_buf); \
253 } while (0);
254 #endif
255
256 namespace lttng {
257 namespace logging {
258 namespace details {
259 [[noreturn]] void die_formatting_exception(const char *format,
260 const std::exception& formatting_exception);
261 } /* namespace details */
262 } /* namespace logging */
263 } /* namespace lttng */
264
265 #define DBG_FMT(format_str, args...) \
266 do { \
267 try { \
268 DBG("%s", fmt::format(format_str, ##args).c_str()); \
269 } catch (const std::exception& _formatting_exception) { \
270 lttng::logging::details::die_formatting_exception(format_str, \
271 _formatting_exception); \
272 } \
273 } while (0);
274
275 #define WARN_FMT(format_str, args...) \
276 do { \
277 try { \
278 WARN("%s", fmt::format(format_str, ##args).c_str()); \
279 } catch (const std::exception& _formatting_exception) { \
280 lttng::logging::details::die_formatting_exception(format_str, \
281 _formatting_exception); \
282 } \
283 } while (0);
284
285 #define ERR_FMT(format_str, args...) \
286 do { \
287 try { \
288 ERR("%s", fmt::format(format_str, ##args).c_str()); \
289 } catch (const std::exception& _formatting_exception) { \
290 lttng::logging::details::die_formatting_exception(format_str, \
291 _formatting_exception); \
292 } \
293 } while (0);
294
295 /*
296 * Function that format the time and return the reference of log_time.str to
297 * the caller. On error, an empty string is returned thus no time will be
298 * printed in the log.
299 */
300 const char *log_add_time();
301
302 /* Name must be a statically-allocated string. */
303 void logger_set_thread_name(const char *name, bool set_pthread_name);
304
305 #endif /* _ERROR_H */
This page took 0.047474 seconds and 4 git commands to generate.