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