Cleanup: rotation-thread: enforce conding standard following fix
[lttng-tools.git] / src / common / error.hpp
1 /*
2 * Copyright (C) 2011 EfficiOS Inc.
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #ifndef _ERROR_H
9 #define _ERROR_H
10
11 #include <common/compat/errno.hpp>
12 #include <common/compat/time.hpp>
13 #include <common/format.hpp>
14 #include <common/macros.hpp>
15 #include <common/string-utils/format.hpp>
16
17 #include <stdbool.h>
18 #include <stdint.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <urcu/tls-compat.h>
22
23 #ifndef _GNU_SOURCE
24 #error "lttng-tools error.h needs _GNU_SOURCE"
25 #endif
26
27 #include <lttng/lttng-error.h>
28 #include <common/compat/tid.hpp>
29
30 /* Avoid conflict with Solaris <sys/regset.h> */
31 #if defined(ERR) && defined(__sun__)
32 #undef ERR
33 #endif
34
35 /* Stringify the expansion of a define */
36 #define XSTR(d) STR(d)
37 #define STR(s) #s
38
39 /*
40 * Contains the string of the log entry time. This is used as a thread local
41 * storage so we don't race between thread and also avoid memory allocation
42 * every time a log is fired.
43 */
44 struct log_time {
45 /* Format: 00:00:00.000000000 plus NULL byte. */
46 char str[19];
47 };
48 extern LTTNG_EXPORT DECLARE_URCU_TLS(struct log_time, error_log_time);
49 extern DECLARE_URCU_TLS(const char *, logger_thread_name);
50
51 extern int lttng_opt_quiet;
52 extern int lttng_opt_verbose;
53 extern int lttng_opt_mi;
54
55 /* Error type. */
56 enum lttng_error_level {
57 PRINT_ERR = 0,
58 PRINT_BUG = 1,
59 PRINT_WARN = 2,
60 PRINT_MSG = 3,
61 PRINT_DBG = 4,
62 PRINT_DBG2 = 5,
63 PRINT_DBG3 = 6,
64 };
65
66 static inline bool __lttng_print_check_opt(enum lttng_error_level type)
67 {
68 /* lttng_opt_mi and lttng_opt_quiet. */
69 switch (type) {
70 case PRINT_DBG3:
71 case PRINT_DBG2:
72 case PRINT_DBG:
73 case PRINT_MSG:
74 if (lttng_opt_mi) {
75 return false;
76 }
77 /* Fall-through. */
78 case PRINT_WARN:
79 case PRINT_BUG:
80 case PRINT_ERR:
81 if (lttng_opt_quiet) {
82 return false;
83 }
84 }
85
86 /* lttng_opt_verbose */
87 switch (type) {
88 case PRINT_DBG3:
89 if (lttng_opt_verbose < 3) {
90 return false;
91 }
92 break;
93 case PRINT_DBG2:
94 if (lttng_opt_verbose < 2) {
95 return false;
96 }
97 break;
98 case PRINT_DBG:
99 if (lttng_opt_verbose < 1) {
100 return false;
101 }
102 break;
103 case PRINT_MSG:
104 case PRINT_WARN:
105 case PRINT_BUG:
106 case PRINT_ERR:
107 break;
108 }
109
110 return true;
111 }
112
113 C_LINKAGE void lttng_abort_on_error(void);
114
115 static inline void __lttng_print_check_abort(enum lttng_error_level type)
116 {
117 switch (type) {
118 case PRINT_DBG3:
119 case PRINT_DBG2:
120 case PRINT_DBG:
121 case PRINT_MSG:
122 case PRINT_WARN:
123 break;
124 case PRINT_BUG:
125 case PRINT_ERR:
126 lttng_abort_on_error();
127 }
128 }
129
130 /*
131 * Macro for printing message depending on command line option and verbosity.
132 *
133 * Machine interface:
134 * We use lttng_opt_mi to suppress all normal msg to stdout. We don't
135 * want any nested msg to show up when printing mi to stdout(if it's the case).
136 * All warnings and errors should be printed to stderr as normal.
137 */
138 #define __lttng_print(type, fmt, args...) \
139 do { \
140 if (__lttng_print_check_opt(type)) { \
141 fprintf((type) == PRINT_MSG ? stdout : stderr, fmt, ## args); \
142 } \
143 __lttng_print_check_abort(type); \
144 } while (0)
145
146 /* Three level of debug. Use -v, -vv or -vvv for the levels */
147 #define _ERRMSG(msg, type, fmt, args...) \
148 do { \
149 if (caa_unlikely(__lttng_print_check_opt(type))) { \
150 char generic_name[MAX_INT_DEC_LEN(long) + \
151 MAX_INT_DEC_LEN(long)]; \
152 \
153 snprintf(generic_name, sizeof(generic_name), \
154 "%ld/%ld", (long) getpid(), \
155 (long) lttng_gettid()); \
156 \
157 __lttng_print(type, \
158 msg " - %s [%s]: " fmt \
159 " (in %s() at " __FILE__ \
160 ":" XSTR(__LINE__) ")\n", \
161 log_add_time(), \
162 URCU_TLS(logger_thread_name) ?: \
163 generic_name, \
164 ##args, __func__); \
165 } \
166 } while (0)
167
168 #define _ERRMSG_NO_LOC(msg, type, fmt, args...) \
169 do { \
170 if (caa_unlikely(__lttng_print_check_opt(type))) { \
171 char generic_name[MAX_INT_DEC_LEN(long) + \
172 MAX_INT_DEC_LEN(long)]; \
173 \
174 snprintf(generic_name, sizeof(generic_name), \
175 "%ld/%ld", (long) getpid(), \
176 (long) lttng_gettid()); \
177 \
178 __lttng_print(type, msg " - %s [%s]: " fmt "\n", \
179 log_add_time(), \
180 URCU_TLS(logger_thread_name) ?: \
181 generic_name, \
182 ##args); \
183 } \
184 } while (0)
185
186 #define MSG(fmt, args...) \
187 __lttng_print(PRINT_MSG, fmt "\n", ## args)
188 #define _MSG(fmt, args...) \
189 __lttng_print(PRINT_MSG, fmt, ## args)
190 #define ERR(fmt, args...) \
191 __lttng_print(PRINT_ERR, "Error: " fmt "\n", ## args)
192 #define WARN(fmt, args...) \
193 __lttng_print(PRINT_WARN, "Warning: " fmt "\n", ## args)
194
195 #define BUG(fmt, args...) _ERRMSG("BUG", PRINT_BUG, fmt, ## args)
196
197 #define DBG(fmt, args...) _ERRMSG("DBG1", PRINT_DBG, fmt, ## args)
198 #define DBG_NO_LOC(fmt, args...) _ERRMSG_NO_LOC("DBG1", PRINT_DBG, fmt, ## args)
199 #define DBG2(fmt, args...) _ERRMSG("DBG2", PRINT_DBG2, fmt, ## args)
200 #define DBG3(fmt, args...) _ERRMSG("DBG3", PRINT_DBG3, fmt, ## args)
201 #define LOG(type, fmt, args...) \
202 do { \
203 switch (type) { \
204 case PRINT_ERR: \
205 ERR(fmt, ## args); \
206 break; \
207 case PRINT_WARN: \
208 WARN(fmt, ## args); \
209 break; \
210 case PRINT_BUG: \
211 BUG(fmt, ## args); \
212 break; \
213 case PRINT_MSG: \
214 MSG(fmt, ## args); \
215 break; \
216 case PRINT_DBG: \
217 DBG(fmt, ## args); \
218 break; \
219 case PRINT_DBG2: \
220 DBG2(fmt, ## args); \
221 break; \
222 case PRINT_DBG3: \
223 DBG3(fmt, ## args); \
224 break; \
225 default: \
226 abort(); \
227 } \
228 } while(0);
229
230 #define _PERROR(fmt, args...) _ERRMSG("PERROR", PRINT_ERR, fmt, ## args)
231
232 #if !defined(__GLIBC__) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE))
233
234 /*
235 * Version using XSI strerror_r.
236 */
237 #define PERROR(call, args...) \
238 do { \
239 char _perror_buf[200]; \
240 strerror_r(errno, _perror_buf, sizeof(_perror_buf)); \
241 _PERROR(call ": %s", ##args, _perror_buf); \
242 } while (0);
243 #else
244 /*
245 * Version using GNU strerror_r, for linux with appropriate defines.
246 */
247 #define PERROR(call, args...) \
248 do { \
249 char *_perror_buf; \
250 char _perror_tmp[200]; \
251 _perror_buf = strerror_r( \
252 errno, _perror_tmp, sizeof(_perror_tmp)); \
253 _PERROR(call ": %s", ##args, _perror_buf); \
254 } while (0);
255 #endif
256
257 #define DBG_FMT(format_str, args...) DBG("%s", fmt::format(format_str, ##args).c_str())
258 #define WARN_FMT(format_str, args...) WARN("%s", fmt::format(format_str, ##args).c_str())
259 #define ERR_FMT(format_str, args...) ERR("%s", fmt::format(format_str, ##args).c_str())
260
261 const char *error_get_str(int32_t code);
262
263 /*
264 * Function that format the time and return the reference of log_time.str to
265 * the caller. On error, an empty string is returned thus no time will be
266 * printed in the log.
267 */
268 const char *log_add_time();
269
270 /* Name must be a statically-allocated string. */
271 void logger_set_thread_name(const char *name, bool set_pthread_name);
272
273 #endif /* _ERROR_H */
This page took 0.035439 seconds and 5 git commands to generate.