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