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