Internal logging lazy-initialization
[lttng-ust.git] / src / common / logging.h
... / ...
CommitLineData
1/*
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
4 * Copyright (C) 2009 Pierre-Marc Fournier
5 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 */
7
8#ifndef _UST_COMMON_LOGGING_H
9#define _UST_COMMON_LOGGING_H
10
11#include <string.h>
12#include <sys/types.h>
13#include <errno.h>
14#include <stdarg.h>
15#include <stdbool.h>
16#include <stdio.h>
17
18#include <lttng/ust-utils.h>
19#include <urcu/compiler.h>
20#include <urcu/system.h>
21
22#include "common/patient.h"
23#include "common/compat/tid.h"
24#include "common/safe-snprintf.h"
25
26enum lttng_ust_log_level {
27 LTTNG_UST_LOG_LEVEL_UNKNOWN = 0,
28 LTTNG_UST_LOG_LEVEL_SILENT,
29 LTTNG_UST_LOG_LEVEL_DEBUG,
30};
31
32extern int lttng_ust_log_level /* enum lttng_ust_log_level */
33 __attribute__((visibility("hidden")));
34
35/*
36 * Initialize the global log level from the "LTTNG_UST_DEBUG" environment
37 * variable.
38 *
39 * This could end up being called concurrently by multiple threads but doesn't
40 * require a mutex since the input is invariant across threads and the result
41 * will be the same.
42 *
43 * Return the current log level to save the caller a second read of the global
44 * log level.
45 */
46int lttng_ust_logging_init(void)
47 __attribute__((visibility("hidden")));
48
49#ifdef LTTNG_UST_DEBUG
50static inline
51bool lttng_ust_logging_debug_enabled(void)
52{
53 return true;
54}
55#else /* #ifdef LTTNG_UST_DEBUG */
56static inline
57bool lttng_ust_logging_debug_enabled(void)
58{
59 int current_log_level;
60
61 current_log_level = CMM_LOAD_SHARED(lttng_ust_log_level);
62
63 /* If the global log level is unknown, lazy-initialize it. */
64 if (caa_unlikely(current_log_level == LTTNG_UST_LOG_LEVEL_UNKNOWN))
65 current_log_level = lttng_ust_logging_init();
66
67 return current_log_level == LTTNG_UST_LOG_LEVEL_DEBUG;
68}
69#endif /* #ifdef LTTNG_UST_DEBUG */
70
71/*
72 * The default component for error messages.
73 */
74#ifndef UST_COMPONENT
75#define UST_COMPONENT libust
76#endif
77
78#define LTTNG_UST_LOG_MAX_LEN 512
79
80/*
81 * We sometimes print in the tracing path, and tracing can occur in
82 * signal handlers, so we must use a print method which is signal safe.
83 */
84/* Can't use dynamic allocation. Limit ourselves to LTTNG_UST_LOG_MAX_LEN chars. */
85/* Add end of string in case of buffer overflow. */
86#define sigsafe_print_err(fmt, args...) \
87do { \
88 if (lttng_ust_logging_debug_enabled()) { \
89 char ____buf[LTTNG_UST_LOG_MAX_LEN]; \
90 int ____saved_errno; \
91 \
92 ____saved_errno = errno; /* signal-safety */ \
93 ust_safe_snprintf(____buf, sizeof(____buf), fmt, ## args); \
94 ____buf[sizeof(____buf) - 1] = 0; \
95 ust_patient_write(STDERR_FILENO, ____buf, strlen(____buf)); \
96 errno = ____saved_errno; /* signal-safety */ \
97 fflush(stderr); \
98 } \
99} while (0)
100
101#define LTTNG_UST_STR_COMPONENT lttng_ust_stringify(UST_COMPONENT)
102
103#define ERRMSG(fmt, args...) \
104 do { \
105 sigsafe_print_err(LTTNG_UST_STR_COMPONENT "[%ld/%ld]: " fmt " (in %s() at " __FILE__ ":" lttng_ust_stringify(__LINE__) ")\n", \
106 (long) getpid(), \
107 (long) lttng_gettid(), \
108 ## args, __func__); \
109 } while(0)
110
111
112#define DBG(fmt, args...) ERRMSG(fmt, ## args)
113#define DBG_raw(fmt, args...) sigsafe_print_err(fmt, ## args)
114#define WARN(fmt, args...) ERRMSG("Warning: " fmt, ## args)
115#define ERR(fmt, args...) ERRMSG("Error: " fmt, ## args)
116#define BUG(fmt, args...) ERRMSG("BUG: " fmt, ## args)
117
118#if !defined(__GLIBC__) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE))
119/*
120 * Version using XSI strerror_r.
121 */
122#define PERROR(call, args...) \
123 do { \
124 if (lttng_ust_logging_debug_enabled()) { \
125 char perror_buf[200] = "Error in strerror_r()"; \
126 strerror_r(errno, perror_buf, \
127 sizeof(perror_buf)); \
128 ERRMSG("Error: " call ": %s", ## args, \
129 perror_buf); \
130 } \
131 } while(0)
132#else
133/*
134 * Version using GNU strerror_r, for linux with appropriate defines.
135 */
136#define PERROR(call, args...) \
137 do { \
138 if (lttng_ust_logging_debug_enabled()) { \
139 char *perror_buf; \
140 char perror_tmp[200]; \
141 perror_buf = strerror_r(errno, perror_tmp, \
142 sizeof(perror_tmp)); \
143 ERRMSG("Error: " call ": %s", ## args, \
144 perror_buf); \
145 } \
146 } while(0)
147#endif
148
149#define BUG_ON(condition) \
150 do { \
151 if (caa_unlikely(condition)) \
152 ERR("condition not respected (BUG) on line %s:%d", __FILE__, __LINE__); \
153 } while(0)
154#define WARN_ON(condition) \
155 do { \
156 if (caa_unlikely(condition)) \
157 WARN("condition not respected on line %s:%d", __FILE__, __LINE__); \
158 } while(0)
159#define WARN_ON_ONCE(condition) WARN_ON(condition)
160
161#endif /* _UST_COMMON_LOGGING_H */
This page took 0.042677 seconds and 4 git commands to generate.