Add lttng_dynamic_buffer util
[lttng-tools.git] / src / common / error.h
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
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 *
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.
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.
16 */
17
18 #ifndef _ERROR_H
19 #define _ERROR_H
20
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdint.h>
24 #include <string.h>
25 #include <stdbool.h>
26 #include <urcu/tls-compat.h>
27 #include <common/compat/time.h>
28
29 #ifndef _GNU_SOURCE
30 #error "lttng-tools error.h needs _GNU_SOURCE"
31 #endif
32
33 #include <lttng/lttng-error.h>
34 #include <common/compat/tid.h>
35
36 /* Avoid conflict with Solaris <sys/regset.h> */
37 #if defined(ERR) && defined(__sun__)
38 #undef ERR
39 #endif
40
41 /* Stringify the expansion of a define */
42 #define XSTR(d) STR(d)
43 #define STR(s) #s
44
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 */
50 struct log_time {
51 /* Format: 00:00:00.000000 plus NULL byte. */
52 char str[16];
53 };
54 extern DECLARE_URCU_TLS(struct log_time, error_log_time);
55
56 extern int lttng_opt_quiet;
57 extern int lttng_opt_verbose;
58 extern int lttng_opt_mi;
59
60 /* Error type. */
61 enum lttng_error_level {
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
71 static inline bool __lttng_print_check_opt(enum lttng_error_level type)
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
118 void lttng_abort_on_error(void);
119
120 static inline void __lttng_print_check_abort(enum lttng_error_level type)
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 }
134
135 /*
136 * Macro for printing message depending on command line option and verbosity.
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.
142 */
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)
150
151 /* Three level of debug. Use -v, -vv or -vvv for the levels */
152 #define _ERRMSG(msg, type, fmt, args...) __lttng_print(type, msg \
153 " - %s [%ld/%ld]: " fmt " (in %s() at " __FILE__ ":" XSTR(__LINE__) ")\n", \
154 log_add_time(), (long) getpid(), (long) gettid(), ## args, __func__)
155
156 #define MSG(fmt, args...) \
157 __lttng_print(PRINT_MSG, fmt "\n", ## args)
158 #define _MSG(fmt, args...) \
159 __lttng_print(PRINT_MSG, fmt, ## args)
160 #define ERR(fmt, args...) \
161 __lttng_print(PRINT_ERR, "Error: " fmt "\n", ## args)
162 #define WARN(fmt, args...) \
163 __lttng_print(PRINT_WARN, "Warning: " fmt "\n", ## args)
164
165 #define BUG(fmt, args...) _ERRMSG("BUG", PRINT_BUG, fmt, ## args)
166
167 #define DBG(fmt, args...) _ERRMSG("DEBUG1", PRINT_DBG, fmt, ## args)
168 #define DBG2(fmt, args...) _ERRMSG("DEBUG2", PRINT_DBG2, fmt, ## args)
169 #define DBG3(fmt, args...) _ERRMSG("DEBUG3", PRINT_DBG3, fmt, ## args)
170 #define LOG(type, fmt, args...) \
171 do { \
172 switch (type) { \
173 case PRINT_ERR: \
174 ERR(fmt, ## args); \
175 break; \
176 case PRINT_WARN: \
177 WARN(fmt, ## args); \
178 break; \
179 case PRINT_BUG: \
180 BUG(fmt, ## args); \
181 break; \
182 case PRINT_MSG: \
183 MSG(fmt, ## args); \
184 break; \
185 case PRINT_DBG: \
186 DBG(fmt, ## args); \
187 break; \
188 case PRINT_DBG2: \
189 DBG2(fmt, ## args); \
190 break; \
191 case PRINT_DBG3: \
192 DBG3(fmt, ## args); \
193 break; \
194 default: \
195 assert(0); \
196 } \
197 } while(0);
198
199 #define _PERROR(fmt, args...) _ERRMSG("PERROR", PRINT_ERR, fmt, ## args)
200
201 #if !defined(__GLIBC__) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE))
202
203 /*
204 * Version using XSI strerror_r.
205 */
206 #define PERROR(call, args...) \
207 do { \
208 char buf[200]; \
209 strerror_r(errno, buf, sizeof(buf)); \
210 _PERROR(call ": %s", ## args, buf); \
211 } while(0);
212 #else
213 /*
214 * Version using GNU strerror_r, for linux with appropriate defines.
215 */
216 #define PERROR(call, args...) \
217 do { \
218 char *buf; \
219 char tmp[200]; \
220 buf = strerror_r(errno, tmp, sizeof(tmp)); \
221 _PERROR(call ": %s", ## args, buf); \
222 } while(0);
223 #endif
224
225 const char *error_get_str(int32_t code);
226
227 /*
228 * Function that format the time and return the reference of log_time.str to
229 * the caller. On error, an empty string is returned thus no time will be
230 * printed in the log.
231 */
232 const char *log_add_time();
233
234 #endif /* _ERROR_H */
This page took 0.035179 seconds and 5 git commands to generate.