sessiond: clarify the role of notification credentials
[lttng-tools.git] / src / common / error.h
... / ...
CommitLineData
1/*
2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8#ifndef _ERROR_H
9#define _ERROR_H
10
11#include <errno.h>
12#include <stdio.h>
13#include <stdint.h>
14#include <string.h>
15#include <stdbool.h>
16#include <urcu/tls-compat.h>
17#include <common/compat/time.h>
18
19#ifndef _GNU_SOURCE
20#error "lttng-tools error.h needs _GNU_SOURCE"
21#endif
22
23#include <lttng/lttng-error.h>
24#include <common/compat/tid.h>
25
26/* Avoid conflict with Solaris <sys/regset.h> */
27#if defined(ERR) && defined(__sun__)
28#undef ERR
29#endif
30
31/* Stringify the expansion of a define */
32#define XSTR(d) STR(d)
33#define STR(s) #s
34
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 {
41 /* Format: 00:00:00.000000000 plus NULL byte. */
42 char str[19];
43};
44extern DECLARE_URCU_TLS(struct log_time, error_log_time);
45
46extern int lttng_opt_quiet;
47extern int lttng_opt_verbose;
48extern int lttng_opt_mi;
49
50/* Error type. */
51enum lttng_error_level {
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
61static inline bool __lttng_print_check_opt(enum lttng_error_level type)
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
110static inline void __lttng_print_check_abort(enum lttng_error_level type)
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}
124
125/*
126 * Macro for printing message depending on command line option and verbosity.
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.
132 */
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)
140
141/* Three level of debug. Use -v, -vv or -vvv for the levels */
142#define _ERRMSG(msg, type, fmt, args...) __lttng_print(type, msg \
143 " - %s [%ld/%ld]: " fmt " (in %s() at " __FILE__ ":" XSTR(__LINE__) ")\n", \
144 log_add_time(), (long) getpid(), (long) lttng_gettid(), ## args, __func__)
145
146#define _ERRMSG_NO_LOC(msg, type, fmt, args...) __lttng_print(type, msg \
147 " - %s [%ld/%ld]: " fmt "\n", \
148 log_add_time(), (long) getpid(), (long) lttng_gettid(), ## args)
149
150#define MSG(fmt, args...) \
151 __lttng_print(PRINT_MSG, fmt "\n", ## args)
152#define _MSG(fmt, args...) \
153 __lttng_print(PRINT_MSG, fmt, ## args)
154#define ERR(fmt, args...) \
155 __lttng_print(PRINT_ERR, "Error: " fmt "\n", ## args)
156#define WARN(fmt, args...) \
157 __lttng_print(PRINT_WARN, "Warning: " fmt "\n", ## args)
158
159#define BUG(fmt, args...) _ERRMSG("BUG", PRINT_BUG, fmt, ## args)
160
161#define DBG(fmt, args...) _ERRMSG("DEBUG1", PRINT_DBG, fmt, ## args)
162#define DBG_NO_LOC(fmt, args...) _ERRMSG_NO_LOC("DEBUG1", PRINT_DBG, fmt, ## args)
163#define DBG2(fmt, args...) _ERRMSG("DEBUG2", PRINT_DBG2, fmt, ## args)
164#define DBG3(fmt, args...) _ERRMSG("DEBUG3", PRINT_DBG3, fmt, ## args)
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);
193
194#define _PERROR(fmt, args...) _ERRMSG("PERROR", PRINT_ERR, fmt, ## args)
195
196#if !defined(__GLIBC__) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE))
197
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 */
211#define PERROR(call, args...) \
212 do { \
213 char *buf; \
214 char tmp[200]; \
215 buf = strerror_r(errno, tmp, sizeof(tmp)); \
216 _PERROR(call ": %s", ## args, buf); \
217 } while(0);
218#endif
219
220const char *error_get_str(int32_t code);
221
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 */
227const char *log_add_time(void);
228
229#endif /* _ERROR_H */
This page took 0.023577 seconds and 4 git commands to generate.