lttng: Indicate file path and error reason when open_config fails
[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 #define _PWARN(fmt, args...) _ERRMSG("PWARN", PRINT_WARN, fmt, ##args)
230
231 #if !defined(__GLIBC__) || \
232 ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE))
233
234 /*
235 * Version using XSI strerror_r.
236 */
237 #define PERROR(call, args...) \
238 do { \
239 char _perror_buf[200]; \
240 strerror_r(errno, _perror_buf, sizeof(_perror_buf)); \
241 _PERROR(call ": %s", ##args, _perror_buf); \
242 } while (0);
243
244 #define PWARN(call, args...) \
245 do { \
246 char _perror_buf[200]; \
247 strerror_r(errno, _perror_buf, sizeof(_perror_buf)); \
248 _PWARN(call ": %s", ##args, _perror_buf); \
249 } while (0);
250 #else
251 /*
252 * Version using GNU strerror_r, for linux with appropriate defines.
253 */
254 #define PERROR(call, args...) \
255 do { \
256 char *_perror_buf; \
257 char _perror_tmp[200]; \
258 _perror_buf = strerror_r(errno, _perror_tmp, sizeof(_perror_tmp)); \
259 _PERROR(call ": %s", ##args, _perror_buf); \
260 } while (0);
261 #define PWARN(call, args...) \
262 do { \
263 char *_perror_buf; \
264 char _perror_tmp[200]; \
265 _perror_buf = strerror_r(errno, _perror_tmp, sizeof(_perror_tmp)); \
266 _PWARN(call ": %s", ##args, _perror_buf); \
267 } while (0);
268 #endif
269
270 const char *error_get_str(int32_t code);
271
272 namespace lttng {
273 namespace logging {
274 namespace details {
275 [[noreturn]] void die_formatting_exception(const char *format,
276 const std::exception& formatting_exception);
277 } /* namespace details */
278 } /* namespace logging */
279 } /* namespace lttng */
280
281 #define DBG_FMT(format_str, args...) \
282 do { \
283 try { \
284 DBG("%s", lttng::format(format_str, ##args).c_str()); \
285 } catch (const std::exception& _formatting_exception) { \
286 lttng::logging::details::die_formatting_exception(format_str, \
287 _formatting_exception); \
288 } \
289 } while (0);
290
291 #define WARN_FMT(format_str, args...) \
292 do { \
293 try { \
294 WARN("%s", lttng::format(format_str, ##args).c_str()); \
295 } catch (const std::exception& _formatting_exception) { \
296 lttng::logging::details::die_formatting_exception(format_str, \
297 _formatting_exception); \
298 } \
299 } while (0);
300
301 #define ERR_FMT(format_str, args...) \
302 do { \
303 try { \
304 ERR("%s", lttng::format(format_str, ##args).c_str()); \
305 } catch (const std::exception& _formatting_exception) { \
306 lttng::logging::details::die_formatting_exception(format_str, \
307 _formatting_exception); \
308 } \
309 } while (0);
310
311 /*
312 * Function that format the time and return the reference of log_time.str to
313 * the caller. On error, an empty string is returned thus no time will be
314 * printed in the log.
315 */
316 const char *log_add_time();
317
318 /* Name must be a statically-allocated string. */
319 void logger_set_thread_name(const char *name, bool set_pthread_name);
320
321 #endif /* _ERROR_H */
This page took 0.036155 seconds and 5 git commands to generate.