8bb0de15cfa70f0c621bae207932274903a65c70
2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
4 * SPDX-License-Identifier: GPL-2.0-only
11 #include <common/compat/errno.h>
16 #include <urcu/tls-compat.h>
17 #include <common/compat/time.h>
18 #include <common/string-utils/format.h>
19 #include <common/macros.h>
22 #error "lttng-tools error.h needs _GNU_SOURCE"
25 #include <lttng/lttng-error.h>
26 #include <common/compat/tid.h>
28 #if defined(__cplusplus)
32 /* Avoid conflict with Solaris <sys/regset.h> */
33 #if defined(ERR) && defined(__sun__)
37 /* Stringify the expansion of a define */
38 #define XSTR(d) STR(d)
42 * Contains the string of the log entry time. This is used as a thread local
43 * storage so we don't race between thread and also avoid memory allocation
44 * every time a log is fired.
47 /* Format: 00:00:00.000000000 plus NULL byte. */
50 extern LTTNG_EXPORT
DECLARE_URCU_TLS(struct log_time
, error_log_time
);
51 extern DECLARE_URCU_TLS(const char *, logger_thread_name
);
53 extern int lttng_opt_quiet
;
54 extern int lttng_opt_verbose
;
55 extern int lttng_opt_mi
;
58 enum lttng_error_level
{
68 static inline bool __lttng_print_check_opt(enum lttng_error_level type
)
70 /* lttng_opt_mi and lttng_opt_quiet. */
83 if (lttng_opt_quiet
) {
88 /* lttng_opt_verbose */
91 if (lttng_opt_verbose
< 3) {
96 if (lttng_opt_verbose
< 2) {
101 if (lttng_opt_verbose
< 1) {
115 void lttng_abort_on_error(void);
117 static inline void __lttng_print_check_abort(enum lttng_error_level type
)
128 lttng_abort_on_error();
133 * Macro for printing message depending on command line option and verbosity.
136 * We use lttng_opt_mi to suppress all normal msg to stdout. We don't
137 * want any nested msg to show up when printing mi to stdout(if it's the case).
138 * All warnings and errors should be printed to stderr as normal.
140 #define __lttng_print(type, fmt, args...) \
142 if (__lttng_print_check_opt(type)) { \
143 fprintf((type) == PRINT_MSG ? stdout : stderr, fmt, ## args); \
145 __lttng_print_check_abort(type); \
148 /* Three level of debug. Use -v, -vv or -vvv for the levels */
149 #define _ERRMSG(msg, type, fmt, args...) \
151 if (caa_unlikely(__lttng_print_check_opt(type))) { \
152 char generic_name[MAX_INT_DEC_LEN(long) + \
153 MAX_INT_DEC_LEN(long)]; \
155 snprintf(generic_name, sizeof(generic_name), \
156 "%ld/%ld", (long) getpid(), \
157 (long) lttng_gettid()); \
159 __lttng_print(type, \
160 msg " - %s [%s]: " fmt \
161 " (in %s() at " __FILE__ \
162 ":" XSTR(__LINE__) ")\n", \
164 URCU_TLS(logger_thread_name) ?: \
170 #define _ERRMSG_NO_LOC(msg, type, fmt, args...) \
172 if (caa_unlikely(__lttng_print_check_opt(type))) { \
173 char generic_name[MAX_INT_DEC_LEN(long) + \
174 MAX_INT_DEC_LEN(long)]; \
176 snprintf(generic_name, sizeof(generic_name), \
177 "%ld/%ld", (long) getpid(), \
178 (long) lttng_gettid()); \
180 __lttng_print(type, msg " - %s [%s]: " fmt "\n", \
182 URCU_TLS(logger_thread_name) ?: \
188 #define MSG(fmt, args...) \
189 __lttng_print(PRINT_MSG, fmt "\n", ## args)
190 #define _MSG(fmt, args...) \
191 __lttng_print(PRINT_MSG, fmt, ## args)
192 #define ERR(fmt, args...) \
193 __lttng_print(PRINT_ERR, "Error: " fmt "\n", ## args)
194 #define WARN(fmt, args...) \
195 __lttng_print(PRINT_WARN, "Warning: " fmt "\n", ## args)
197 #define BUG(fmt, args...) _ERRMSG("BUG", PRINT_BUG, fmt, ## args)
199 #define DBG(fmt, args...) _ERRMSG("DBG1", PRINT_DBG, fmt, ## args)
200 #define DBG_NO_LOC(fmt, args...) _ERRMSG_NO_LOC("DBG1", PRINT_DBG, fmt, ## args)
201 #define DBG2(fmt, args...) _ERRMSG("DBG2", PRINT_DBG2, fmt, ## args)
202 #define DBG3(fmt, args...) _ERRMSG("DBG3", PRINT_DBG3, fmt, ## args)
203 #define LOG(type, fmt, args...) \
210 WARN(fmt, ## args); \
222 DBG2(fmt, ## args); \
225 DBG3(fmt, ## args); \
232 #define _PERROR(fmt, args...) _ERRMSG("PERROR", PRINT_ERR, fmt, ## args)
234 #if !defined(__GLIBC__) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE))
237 * Version using XSI strerror_r.
239 #define PERROR(call, args...) \
241 char _perror_buf[200]; \
242 strerror_r(errno, _perror_buf, sizeof(_perror_buf)); \
243 _PERROR(call ": %s", ##args, _perror_buf); \
247 * Version using GNU strerror_r, for linux with appropriate defines.
249 #define PERROR(call, args...) \
252 char _perror_tmp[200]; \
253 _perror_buf = strerror_r( \
254 errno, _perror_tmp, sizeof(_perror_tmp)); \
255 _PERROR(call ": %s", ##args, _perror_buf); \
259 const char *error_get_str(int32_t code
);
262 * Function that format the time and return the reference of log_time.str to
263 * the caller. On error, an empty string is returned thus no time will be
264 * printed in the log.
266 const char *log_add_time(void);
268 /* Name must be a statically-allocated string. */
269 void logger_set_thread_name(const char *name
, bool set_pthread_name
);
271 #if defined(__cplusplus)
275 #endif /* _ERROR_H */
This page took 0.035327 seconds and 4 git commands to generate.