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