2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2009 Pierre-Marc Fournier
5 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 #ifndef _UST_COMMON_LOGGING_H
9 #define _UST_COMMON_LOGGING_H
12 #include <sys/types.h>
18 #include <lttng/ust-utils.h>
20 #include "common/patient.h"
21 #include "common/compat/tid.h"
22 #include "common/safe-snprintf.h"
24 enum lttng_ust_log_level
{
25 LTTNG_UST_LOG_LEVEL_UNKNOWN
= 0,
26 LTTNG_UST_LOG_LEVEL_NORMAL
,
27 LTTNG_UST_LOG_LEVEL_DEBUG
,
30 extern volatile enum lttng_ust_log_level lttng_ust_log_level
31 __attribute__((visibility("hidden")));
33 void lttng_ust_logging_init(void)
34 __attribute__((visibility("hidden")));
36 #ifdef LTTNG_UST_DEBUG
37 static inline bool lttng_ust_logging_debug_enabled(void)
41 #else /* #ifdef LTTNG_UST_DEBUG */
42 static inline bool lttng_ust_logging_debug_enabled(void)
44 return lttng_ust_log_level
== LTTNG_UST_LOG_LEVEL_DEBUG
;
46 #endif /* #else #ifdef LTTNG_UST_DEBUG */
49 * The default component for error messages.
52 #define UST_COMPONENT libust
55 #define LTTNG_UST_LOG_MAX_LEN 512
58 * We sometimes print in the tracing path, and tracing can occur in
59 * signal handlers, so we must use a print method which is signal safe.
61 /* Can't use dynamic allocation. Limit ourselves to LTTNG_UST_LOG_MAX_LEN chars. */
62 /* Add end of string in case of buffer overflow. */
63 #define sigsafe_print_err(fmt, args...) \
65 if (lttng_ust_logging_debug_enabled()) { \
66 char ____buf[LTTNG_UST_LOG_MAX_LEN]; \
67 int ____saved_errno; \
69 ____saved_errno = errno; /* signal-safety */ \
70 ust_safe_snprintf(____buf, sizeof(____buf), fmt, ## args); \
71 ____buf[sizeof(____buf) - 1] = 0; \
72 ust_patient_write(STDERR_FILENO, ____buf, strlen(____buf)); \
73 errno = ____saved_errno; /* signal-safety */ \
78 #define LTTNG_UST_STR_COMPONENT lttng_ust_stringify(UST_COMPONENT)
80 #define ERRMSG(fmt, args...) \
82 sigsafe_print_err(LTTNG_UST_STR_COMPONENT "[%ld/%ld]: " fmt " (in %s() at " __FILE__ ":" lttng_ust_stringify(__LINE__) ")\n", \
84 (long) lttng_gettid(), \
89 #define DBG(fmt, args...) ERRMSG(fmt, ## args)
90 #define DBG_raw(fmt, args...) sigsafe_print_err(fmt, ## args)
91 #define WARN(fmt, args...) ERRMSG("Warning: " fmt, ## args)
92 #define ERR(fmt, args...) ERRMSG("Error: " fmt, ## args)
93 #define BUG(fmt, args...) ERRMSG("BUG: " fmt, ## args)
95 #if !defined(__GLIBC__) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE))
97 * Version using XSI strerror_r.
99 #define PERROR(call, args...) \
101 if (lttng_ust_logging_debug_enabled()) { \
102 char perror_buf[200] = "Error in strerror_r()"; \
103 strerror_r(errno, perror_buf, \
104 sizeof(perror_buf)); \
105 ERRMSG("Error: " call ": %s", ## args, \
111 * Version using GNU strerror_r, for linux with appropriate defines.
113 #define PERROR(call, args...) \
115 if (lttng_ust_logging_debug_enabled()) { \
117 char perror_tmp[200]; \
118 perror_buf = strerror_r(errno, perror_tmp, \
119 sizeof(perror_tmp)); \
120 ERRMSG("Error: " call ": %s", ## args, \
126 #define BUG_ON(condition) \
128 if (caa_unlikely(condition)) \
129 ERR("condition not respected (BUG) on line %s:%d", __FILE__, __LINE__); \
131 #define WARN_ON(condition) \
133 if (caa_unlikely(condition)) \
134 WARN("condition not respected on line %s:%d", __FILE__, __LINE__); \
136 #define WARN_ON_ONCE(condition) WARN_ON(condition)
138 #endif /* _UST_COMMON_LOGGING_H */