2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: LGPL-2.1-only
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>
21 #include <urcu/tls-compat.h>
24 #error "lttng-tools error.h needs _GNU_SOURCE"
27 #include <common/compat/tid.hpp>
29 #include <lttng/lttng-error.h>
31 /* Avoid conflict with Solaris <sys/regset.h> */
32 #if defined(ERR) && defined(__sun__)
36 /* Stringify the expansion of a define */
37 #define XSTR(d) STR(d)
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.
46 /* Format: 00:00:00.000000000 plus NULL byte. */
50 extern thread_local const char *logger_thread_name;
52 extern int lttng_opt_quiet;
53 extern int lttng_opt_verbose;
54 extern int lttng_opt_mi;
57 enum lttng_error_level {
67 static inline bool __lttng_print_check_opt(enum lttng_error_level type)
69 /* lttng_opt_mi and lttng_opt_quiet. */
82 if (lttng_opt_quiet) {
87 /* lttng_opt_verbose */
90 if (lttng_opt_verbose < 3) {
95 if (lttng_opt_verbose < 2) {
100 if (lttng_opt_verbose < 1) {
114 C_LINKAGE void lttng_abort_on_error(void);
116 static inline void __lttng_print_check_abort(enum lttng_error_level type)
127 lttng_abort_on_error();
132 * Macro for printing message depending on command line option and verbosity.
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.
139 #define __lttng_print(type, fmt, args...) \
141 if (__lttng_print_check_opt(type)) { \
142 fprintf((type) == PRINT_MSG ? stdout : stderr, fmt, ##args); \
144 __lttng_print_check_abort(type); \
147 /* Three level of debug. Use -v, -vv or -vvv for the levels */
148 #define _ERRMSG(msg, type, fmt, args...) \
150 if (caa_unlikely(__lttng_print_check_opt(type))) { \
151 char generic_name[MAX_INT_DEC_LEN(long) + MAX_INT_DEC_LEN(long)]; \
153 snprintf(generic_name, \
154 sizeof(generic_name), \
157 (long) lttng_gettid()); \
159 __lttng_print(type, \
160 msg " - %s [%s]: " fmt " (in %s() at " __FILE__ \
161 ":" XSTR(__LINE__) ")\n", \
163 logger_thread_name ?: generic_name, \
169 #define _ERRMSG_NO_LOC(msg, type, fmt, args...) \
171 if (caa_unlikely(__lttng_print_check_opt(type))) { \
172 char generic_name[MAX_INT_DEC_LEN(long) + MAX_INT_DEC_LEN(long)]; \
174 snprintf(generic_name, \
175 sizeof(generic_name), \
178 (long) lttng_gettid()); \
180 __lttng_print(type, \
181 msg " - %s [%s]: " fmt "\n", \
183 logger_thread_name ?: generic_name, \
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)
193 #define BUG(fmt, args...) _ERRMSG("BUG", PRINT_BUG, fmt, ##args)
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...) \
228 #define _PERROR(fmt, args...) _ERRMSG("PERROR", PRINT_ERR, fmt, ##args)
229 #define _PWARN(fmt, args...) _ERRMSG("PWARN", PRINT_WARN, fmt, ##args)
231 #if !defined(__GLIBC__) || \
232 ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE))
235 * Version using XSI strerror_r.
237 #define PERROR(call, args...) \
239 char _perror_buf[200]; \
240 strerror_r(errno, _perror_buf, sizeof(_perror_buf)); \
241 _PERROR(call ": %s", ##args, _perror_buf); \
244 #define PWARN(call, args...) \
246 char _perror_buf[200]; \
247 strerror_r(errno, _perror_buf, sizeof(_perror_buf)); \
248 _PWARN(call ": %s", ##args, _perror_buf); \
252 * Version using GNU strerror_r, for linux with appropriate defines.
254 #define PERROR(call, args...) \
257 char _perror_tmp[200]; \
258 _perror_buf = strerror_r(errno, _perror_tmp, sizeof(_perror_tmp)); \
259 _PERROR(call ": %s", ##args, _perror_buf); \
261 #define PWARN(call, args...) \
264 char _perror_tmp[200]; \
265 _perror_buf = strerror_r(errno, _perror_tmp, sizeof(_perror_tmp)); \
266 _PWARN(call ": %s", ##args, _perror_buf); \
270 const char *error_get_str(int32_t code);
275 [[noreturn]] void die_formatting_exception(const char *format,
276 const std::exception& formatting_exception);
277 } /* namespace details */
278 } /* namespace logging */
279 } /* namespace lttng */
281 #define DBG_FMT(format_str, args...) \
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); \
291 #define WARN_FMT(format_str, args...) \
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); \
301 #define ERR_FMT(format_str, args...) \
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); \
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.
316 const char *log_add_time();
318 /* Name must be a statically-allocated string. */
319 void logger_set_thread_name(const char *name, bool set_pthread_name);
321 #endif /* _ERROR_H */