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. */
49 extern DECLARE_URCU_TLS(const char *, logger_thread_name);
51 extern int lttng_opt_quiet;
52 extern int lttng_opt_verbose;
53 extern int lttng_opt_mi;
56 enum lttng_error_level {
66 static inline bool __lttng_print_check_opt(enum lttng_error_level type)
68 /* lttng_opt_mi and lttng_opt_quiet. */
81 if (lttng_opt_quiet) {
86 /* lttng_opt_verbose */
89 if (lttng_opt_verbose < 3) {
94 if (lttng_opt_verbose < 2) {
99 if (lttng_opt_verbose < 1) {
113 C_LINKAGE void lttng_abort_on_error(void);
115 static inline void __lttng_print_check_abort(enum lttng_error_level type)
126 lttng_abort_on_error();
131 * Macro for printing message depending on command line option and verbosity.
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.
138 #define __lttng_print(type, fmt, args...) \
140 if (__lttng_print_check_opt(type)) { \
141 fprintf((type) == PRINT_MSG ? stdout : stderr, fmt, ##args); \
143 __lttng_print_check_abort(type); \
146 /* Three level of debug. Use -v, -vv or -vvv for the levels */
147 #define _ERRMSG(msg, type, fmt, args...) \
149 if (caa_unlikely(__lttng_print_check_opt(type))) { \
150 char generic_name[MAX_INT_DEC_LEN(long) + MAX_INT_DEC_LEN(long)]; \
152 snprintf(generic_name, \
153 sizeof(generic_name), \
156 (long) lttng_gettid()); \
158 __lttng_print(type, \
159 msg " - %s [%s]: " fmt " (in %s() at " __FILE__ \
160 ":" XSTR(__LINE__) ")\n", \
162 URCU_TLS(logger_thread_name) ?: generic_name, \
168 #define _ERRMSG_NO_LOC(msg, type, fmt, args...) \
170 if (caa_unlikely(__lttng_print_check_opt(type))) { \
171 char generic_name[MAX_INT_DEC_LEN(long) + MAX_INT_DEC_LEN(long)]; \
173 snprintf(generic_name, \
174 sizeof(generic_name), \
177 (long) lttng_gettid()); \
179 __lttng_print(type, \
180 msg " - %s [%s]: " fmt "\n", \
182 URCU_TLS(logger_thread_name) ?: generic_name, \
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)
192 #define BUG(fmt, args...) _ERRMSG("BUG", PRINT_BUG, fmt, ##args)
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...) \
227 #define _PERROR(fmt, args...) _ERRMSG("PERROR", PRINT_ERR, fmt, ##args)
229 #if !defined(__GLIBC__) || \
230 ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE))
233 * Version using XSI strerror_r.
235 #define PERROR(call, args...) \
237 char _perror_buf[200]; \
238 strerror_r(errno, _perror_buf, sizeof(_perror_buf)); \
239 _PERROR(call ": %s", ##args, _perror_buf); \
243 * Version using GNU strerror_r, for linux with appropriate defines.
245 const char *error_get_str(int32_t code);
246 #define PERROR(call, args...) \
249 char _perror_tmp[200]; \
250 _perror_buf = strerror_r(errno, _perror_tmp, sizeof(_perror_tmp)); \
251 _PERROR(call ": %s", ##args, _perror_buf); \
258 [[noreturn]] void die_formatting_exception(const char *format,
259 const std::exception& formatting_exception);
260 } /* namespace details */
261 } /* namespace logging */
262 } /* namespace lttng */
264 #define DBG_FMT(format_str, args...) \
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); \
274 #define WARN_FMT(format_str, args...) \
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); \
284 #define ERR_FMT(format_str, args...) \
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); \
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.
299 const char *log_add_time();
301 /* Name must be a statically-allocated string. */
302 void logger_set_thread_name(const char *name, bool set_pthread_name);
304 #endif /* _ERROR_H */