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)
230 #if !defined(__GLIBC__) || \
231 ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE))
234 * Version using XSI strerror_r.
236 #define PERROR(call, args...) \
238 char _perror_buf[200]; \
239 strerror_r(errno, _perror_buf, sizeof(_perror_buf)); \
240 _PERROR(call ": %s", ##args, _perror_buf); \
244 * Version using GNU strerror_r, for linux with appropriate defines.
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); \
255 const char *error_get_str(int32_t code);
260 [[noreturn]] void die_formatting_exception(const char *format,
261 const std::exception& formatting_exception);
262 } /* namespace details */
263 } /* namespace logging */
264 } /* namespace lttng */
266 #define DBG_FMT(format_str, args...) \
269 DBG("%s", lttng::format(format_str, ##args).c_str()); \
270 } catch (const std::exception& _formatting_exception) { \
271 lttng::logging::details::die_formatting_exception(format_str, \
272 _formatting_exception); \
276 #define WARN_FMT(format_str, args...) \
279 WARN("%s", lttng::format(format_str, ##args).c_str()); \
280 } catch (const std::exception& _formatting_exception) { \
281 lttng::logging::details::die_formatting_exception(format_str, \
282 _formatting_exception); \
286 #define ERR_FMT(format_str, args...) \
289 ERR("%s", lttng::format(format_str, ##args).c_str()); \
290 } catch (const std::exception& _formatting_exception) { \
291 lttng::logging::details::die_formatting_exception(format_str, \
292 _formatting_exception); \
297 * Function that format the time and return the reference of log_time.str to
298 * the caller. On error, an empty string is returned thus no time will be
299 * printed in the log.
301 const char *log_add_time();
303 /* Name must be a statically-allocated string. */
304 void logger_set_thread_name(const char *name, bool set_pthread_name);
306 #endif /* _ERROR_H */