docs: Add supported versions and fix-backport policy
[lttng-tools.git] / src / common / error.h
1 /*
2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #ifndef _ERROR_H
9 #define _ERROR_H
10
11 #include <common/compat/errno.h>
12 #include <stdio.h>
13 #include <stdint.h>
14 #include <string.h>
15 #include <stdbool.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>
20
21 #ifndef _GNU_SOURCE
22 #error "lttng-tools error.h needs _GNU_SOURCE"
23 #endif
24
25 #include <lttng/lttng-error.h>
26 #include <common/compat/tid.h>
27
28 #if defined(__cplusplus)
29 extern "C" {
30 #endif
31
32 /* Avoid conflict with Solaris <sys/regset.h> */
33 #if defined(ERR) && defined(__sun__)
34 #undef ERR
35 #endif
36
37 /* Stringify the expansion of a define */
38 #define XSTR(d) STR(d)
39 #define STR(s) #s
40
41 /*
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.
45 */
46 struct log_time {
47 /* Format: 00:00:00.000000000 plus NULL byte. */
48 char str[19];
49 };
50 extern DECLARE_URCU_TLS(struct log_time, error_log_time);
51 extern DECLARE_URCU_TLS(const char *, logger_thread_name);
52
53 extern int lttng_opt_quiet;
54 extern int lttng_opt_verbose;
55 extern int lttng_opt_mi;
56
57 /* Error type. */
58 enum lttng_error_level {
59 PRINT_ERR = 0,
60 PRINT_BUG = 1,
61 PRINT_WARN = 2,
62 PRINT_MSG = 3,
63 PRINT_DBG = 4,
64 PRINT_DBG2 = 5,
65 PRINT_DBG3 = 6,
66 };
67
68 static inline bool __lttng_print_check_opt(enum lttng_error_level type)
69 {
70 /* lttng_opt_mi and lttng_opt_quiet. */
71 switch (type) {
72 case PRINT_DBG3:
73 case PRINT_DBG2:
74 case PRINT_DBG:
75 case PRINT_MSG:
76 if (lttng_opt_mi) {
77 return false;
78 }
79 /* Fall-through. */
80 case PRINT_WARN:
81 case PRINT_BUG:
82 case PRINT_ERR:
83 if (lttng_opt_quiet) {
84 return false;
85 }
86 }
87
88 /* lttng_opt_verbose */
89 switch (type) {
90 case PRINT_DBG3:
91 if (lttng_opt_verbose < 3) {
92 return false;
93 }
94 break;
95 case PRINT_DBG2:
96 if (lttng_opt_verbose < 2) {
97 return false;
98 }
99 break;
100 case PRINT_DBG:
101 if (lttng_opt_verbose < 1) {
102 return false;
103 }
104 break;
105 case PRINT_MSG:
106 case PRINT_WARN:
107 case PRINT_BUG:
108 case PRINT_ERR:
109 break;
110 }
111
112 return true;
113 }
114
115 void lttng_abort_on_error(void);
116
117 static inline void __lttng_print_check_abort(enum lttng_error_level type)
118 {
119 switch (type) {
120 case PRINT_DBG3:
121 case PRINT_DBG2:
122 case PRINT_DBG:
123 case PRINT_MSG:
124 case PRINT_WARN:
125 break;
126 case PRINT_BUG:
127 case PRINT_ERR:
128 lttng_abort_on_error();
129 }
130 }
131
132 /*
133 * Macro for printing message depending on command line option and verbosity.
134 *
135 * Machine interface:
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.
139 */
140 #define __lttng_print(type, fmt, args...) \
141 do { \
142 if (__lttng_print_check_opt(type)) { \
143 fprintf((type) == PRINT_MSG ? stdout : stderr, fmt, ## args); \
144 } \
145 __lttng_print_check_abort(type); \
146 } while (0)
147
148 /* Three level of debug. Use -v, -vv or -vvv for the levels */
149 #define _ERRMSG(msg, type, fmt, args...) \
150 do { \
151 if (caa_unlikely(__lttng_print_check_opt(type))) { \
152 char generic_name[MAX_INT_DEC_LEN(long) + \
153 MAX_INT_DEC_LEN(long)]; \
154 \
155 snprintf(generic_name, sizeof(generic_name), \
156 "%ld/%ld", (long) getpid(), \
157 (long) lttng_gettid()); \
158 \
159 __lttng_print(type, \
160 msg " - %s [%s]: " fmt \
161 " (in %s() at " __FILE__ \
162 ":" XSTR(__LINE__) ")\n", \
163 log_add_time(), \
164 URCU_TLS(logger_thread_name) ?: \
165 generic_name, \
166 ##args, __func__); \
167 } \
168 } while (0)
169
170 #define _ERRMSG_NO_LOC(msg, type, fmt, args...) \
171 do { \
172 if (caa_unlikely(__lttng_print_check_opt(type))) { \
173 char generic_name[MAX_INT_DEC_LEN(long) + \
174 MAX_INT_DEC_LEN(long)]; \
175 \
176 snprintf(generic_name, sizeof(generic_name), \
177 "%ld/%ld", (long) getpid(), \
178 (long) lttng_gettid()); \
179 \
180 __lttng_print(type, msg " - %s [%s]: " fmt "\n", \
181 log_add_time(), \
182 URCU_TLS(logger_thread_name) ?: \
183 generic_name, \
184 ##args); \
185 } \
186 } while (0)
187
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)
196
197 #define BUG(fmt, args...) _ERRMSG("BUG", PRINT_BUG, fmt, ## args)
198
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...) \
204 do { \
205 switch (type) { \
206 case PRINT_ERR: \
207 ERR(fmt, ## args); \
208 break; \
209 case PRINT_WARN: \
210 WARN(fmt, ## args); \
211 break; \
212 case PRINT_BUG: \
213 BUG(fmt, ## args); \
214 break; \
215 case PRINT_MSG: \
216 MSG(fmt, ## args); \
217 break; \
218 case PRINT_DBG: \
219 DBG(fmt, ## args); \
220 break; \
221 case PRINT_DBG2: \
222 DBG2(fmt, ## args); \
223 break; \
224 case PRINT_DBG3: \
225 DBG3(fmt, ## args); \
226 break; \
227 default: \
228 abort(); \
229 } \
230 } while(0);
231
232 #define _PERROR(fmt, args...) _ERRMSG("PERROR", PRINT_ERR, fmt, ## args)
233
234 #if !defined(__GLIBC__) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE))
235
236 /*
237 * Version using XSI strerror_r.
238 */
239 #define PERROR(call, args...) \
240 do { \
241 char _perror_buf[200]; \
242 strerror_r(errno, _perror_buf, sizeof(_perror_buf)); \
243 _PERROR(call ": %s", ##args, _perror_buf); \
244 } while (0);
245 #else
246 /*
247 * Version using GNU strerror_r, for linux with appropriate defines.
248 */
249 #define PERROR(call, args...) \
250 do { \
251 char *_perror_buf; \
252 char _perror_tmp[200]; \
253 _perror_buf = strerror_r( \
254 errno, _perror_tmp, sizeof(_perror_tmp)); \
255 _PERROR(call ": %s", ##args, _perror_buf); \
256 } while (0);
257 #endif
258
259 const char *error_get_str(int32_t code);
260
261 /*
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.
265 */
266 const char *log_add_time(void);
267
268 /* Name must be a statically-allocated string. */
269 void logger_set_thread_name(const char *name, bool set_pthread_name);
270
271 #if defined(__cplusplus)
272 }
273 #endif
274
275 #endif /* _ERROR_H */
This page took 0.034449 seconds and 4 git commands to generate.