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