lib: compile liblttng-ctl as C++
[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
48a40005
SM
28#if defined(__cplusplus)
29extern "C" {
30#endif
31
1e9e234a
MJ
32/* Avoid conflict with Solaris <sys/regset.h> */
33#if defined(ERR) && defined(__sun__)
34#undef ERR
35#endif
36
57d0d501
DG
37/* Stringify the expansion of a define */
38#define XSTR(d) STR(d)
39#define STR(s) #s
40
a0b439da
DG
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 */
46struct log_time {
870bdaf9
JD
47 /* Format: 00:00:00.000000000 plus NULL byte. */
48 char str[19];
a0b439da 49};
4bd69c5f 50extern LTTNG_EXPORT DECLARE_URCU_TLS(struct log_time, error_log_time);
ca806b0b 51extern DECLARE_URCU_TLS(const char *, logger_thread_name);
a0b439da 52
97e19046
DG
53extern int lttng_opt_quiet;
54extern int lttng_opt_verbose;
c7e35b03 55extern int lttng_opt_mi;
fac6795d 56
ffe60014 57/* Error type. */
ce69c47f 58enum lttng_error_level {
ab530aaf
MD
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
ce69c47f 68static inline bool __lttng_print_check_opt(enum lttng_error_level type)
ab530aaf
MD
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
115void lttng_abort_on_error(void);
116
ce69c47f 117static inline void __lttng_print_check_abort(enum lttng_error_level type)
ab530aaf
MD
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}
fac6795d
DG
131
132/*
75dea654 133 * Macro for printing message depending on command line option and verbosity.
c7e35b03
JR
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.
fac6795d 139 */
ab530aaf
MD
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)
fac6795d 147
ffe60014 148/* Three level of debug. Use -v, -vv or -vvv for the levels */
f5fb86c1
JG
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)
ffe60014 169
f5fb86c1
JG
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)
e6142f2e 187
75dea654
DG
188#define MSG(fmt, args...) \
189 __lttng_print(PRINT_MSG, fmt "\n", ## args)
f37d259d
MD
190#define _MSG(fmt, args...) \
191 __lttng_print(PRINT_MSG, fmt, ## args)
75dea654 192#define ERR(fmt, args...) \
c66f2a27 193 __lttng_print(PRINT_ERR, "Error: " fmt "\n", ## args)
75dea654 194#define WARN(fmt, args...) \
cb1917a9 195 __lttng_print(PRINT_WARN, "Warning: " fmt "\n", ## args)
75dea654 196
ffe60014
DG
197#define BUG(fmt, args...) _ERRMSG("BUG", PRINT_BUG, fmt, ## args)
198
f5fb86c1
JG
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)
7d9ad880
JG
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: \
a0377dfe 228 abort(); \
7d9ad880
JG
229 } \
230 } while(0);
ffe60014
DG
231
232#define _PERROR(fmt, args...) _ERRMSG("PERROR", PRINT_ERR, fmt, ## args)
75dea654 233
b6dacfe2 234#if !defined(__GLIBC__) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE))
97e19046 235
818d276f
MD
236/*
237 * Version using XSI strerror_r.
238 */
0c818c97
SM
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);
818d276f
MD
245#else
246/*
247 * Version using GNU strerror_r, for linux with appropriate defines.
248 */
0c818c97
SM
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);
818d276f 257#endif
fac6795d 258
f73fabfd
DG
259const char *error_get_str(int32_t code);
260
a0b439da
DG
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 */
898ba132 266const char *log_add_time(void);
a0b439da 267
f5fb86c1 268/* Name must be a statically-allocated string. */
f5fb86c1
JG
269void logger_set_thread_name(const char *name, bool set_pthread_name);
270
48a40005
SM
271#if defined(__cplusplus)
272}
273#endif
274
db758600 275#endif /* _ERROR_H */
This page took 0.069547 seconds and 4 git commands to generate.