2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: LGPL-2.1-only
11 #include <common/compat/errno.hpp>
16 #include <urcu/tls-compat.h>
17 #include <common/compat/time.hpp>
18 #include <common/string-utils/format.hpp>
19 #include <common/macros.hpp>
22 #error "lttng-tools error.h needs _GNU_SOURCE"
25 #include <lttng/lttng-error.h>
26 #include <common/compat/tid.hpp>
28 /* Avoid conflict with Solaris <sys/regset.h> */
29 #if defined(ERR) && defined(__sun__)
33 /* Stringify the expansion of a define */
34 #define XSTR(d) STR(d)
38 * Contains the string of the log entry time. This is used as a thread local
39 * storage so we don't race between thread and also avoid memory allocation
40 * every time a log is fired.
43 /* Format: 00:00:00.000000000 plus NULL byte. */
46 extern LTTNG_EXPORT DECLARE_URCU_TLS(struct log_time, error_log_time);
47 extern DECLARE_URCU_TLS(const char *, logger_thread_name);
49 extern int lttng_opt_quiet;
50 extern int lttng_opt_verbose;
51 extern int lttng_opt_mi;
54 enum lttng_error_level {
64 static inline bool __lttng_print_check_opt(enum lttng_error_level type)
66 /* lttng_opt_mi and lttng_opt_quiet. */
79 if (lttng_opt_quiet) {
84 /* lttng_opt_verbose */
87 if (lttng_opt_verbose < 3) {
92 if (lttng_opt_verbose < 2) {
97 if (lttng_opt_verbose < 1) {
111 C_LINKAGE void lttng_abort_on_error(void);
113 static inline void __lttng_print_check_abort(enum lttng_error_level type)
124 lttng_abort_on_error();
129 * Macro for printing message depending on command line option and verbosity.
132 * We use lttng_opt_mi to suppress all normal msg to stdout. We don't
133 * want any nested msg to show up when printing mi to stdout(if it's the case).
134 * All warnings and errors should be printed to stderr as normal.
136 #define __lttng_print(type, fmt, args...) \
138 if (__lttng_print_check_opt(type)) { \
139 fprintf((type) == PRINT_MSG ? stdout : stderr, fmt, ## args); \
141 __lttng_print_check_abort(type); \
144 /* Three level of debug. Use -v, -vv or -vvv for the levels */
145 #define _ERRMSG(msg, type, fmt, args...) \
147 if (caa_unlikely(__lttng_print_check_opt(type))) { \
148 char generic_name[MAX_INT_DEC_LEN(long) + \
149 MAX_INT_DEC_LEN(long)]; \
151 snprintf(generic_name, sizeof(generic_name), \
152 "%ld/%ld", (long) getpid(), \
153 (long) lttng_gettid()); \
155 __lttng_print(type, \
156 msg " - %s [%s]: " fmt \
157 " (in %s() at " __FILE__ \
158 ":" XSTR(__LINE__) ")\n", \
160 URCU_TLS(logger_thread_name) ?: \
166 #define _ERRMSG_NO_LOC(msg, type, fmt, args...) \
168 if (caa_unlikely(__lttng_print_check_opt(type))) { \
169 char generic_name[MAX_INT_DEC_LEN(long) + \
170 MAX_INT_DEC_LEN(long)]; \
172 snprintf(generic_name, sizeof(generic_name), \
173 "%ld/%ld", (long) getpid(), \
174 (long) lttng_gettid()); \
176 __lttng_print(type, msg " - %s [%s]: " fmt "\n", \
178 URCU_TLS(logger_thread_name) ?: \
184 #define MSG(fmt, args...) \
185 __lttng_print(PRINT_MSG, fmt "\n", ## args)
186 #define _MSG(fmt, args...) \
187 __lttng_print(PRINT_MSG, fmt, ## args)
188 #define ERR(fmt, args...) \
189 __lttng_print(PRINT_ERR, "Error: " fmt "\n", ## args)
190 #define WARN(fmt, args...) \
191 __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...) \
206 WARN(fmt, ## args); \
218 DBG2(fmt, ## args); \
221 DBG3(fmt, ## args); \
228 #define _PERROR(fmt, args...) _ERRMSG("PERROR", PRINT_ERR, fmt, ## args)
230 #if !defined(__GLIBC__) || ((_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 #define PERROR(call, args...) \
248 char _perror_tmp[200]; \
249 _perror_buf = strerror_r( \
250 errno, _perror_tmp, sizeof(_perror_tmp)); \
251 _PERROR(call ": %s", ##args, _perror_buf); \
255 const char *error_get_str(int32_t code);
258 * Function that format the time and return the reference of log_time.str to
259 * the caller. On error, an empty string is returned thus no time will be
260 * printed in the log.
262 const char *log_add_time(void);
264 /* Name must be a statically-allocated string. */
265 void logger_set_thread_name(const char *name, bool set_pthread_name);
267 #endif /* _ERROR_H */