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