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> |
fac6795d | 25 | |
4c462e79 MD |
26 | #ifndef _GNU_SOURCE |
27 | #error "lttng-tools error.h needs _GNU_SOURCE" | |
28 | #endif | |
29 | ||
f73fabfd | 30 | #include <lttng/lttng-error.h> |
ffe60014 | 31 | #include <common/compat/tid.h> |
f73fabfd | 32 | |
57d0d501 DG |
33 | /* Stringify the expansion of a define */ |
34 | #define XSTR(d) STR(d) | |
35 | #define STR(s) #s | |
36 | ||
97e19046 DG |
37 | extern int lttng_opt_quiet; |
38 | extern int lttng_opt_verbose; | |
fac6795d | 39 | |
ffe60014 | 40 | /* Error type. */ |
75dea654 DG |
41 | #define PRINT_ERR 0x1 |
42 | #define PRINT_WARN 0x2 | |
43 | #define PRINT_BUG 0x3 | |
44 | #define PRINT_MSG 0x4 | |
45 | #define PRINT_DBG 0x10 | |
46 | #define PRINT_DBG2 0x20 | |
47 | #define PRINT_DBG3 0x30 | |
fac6795d DG |
48 | |
49 | /* | |
75dea654 | 50 | * Macro for printing message depending on command line option and verbosity. |
fac6795d | 51 | */ |
97e19046 DG |
52 | #define __lttng_print(type, fmt, args...) \ |
53 | do { \ | |
54 | if (lttng_opt_quiet == 0 && type == PRINT_MSG) { \ | |
55 | fprintf(stdout, fmt, ## args); \ | |
56 | } else if (lttng_opt_quiet == 0 && \ | |
57 | (((type & PRINT_DBG) && lttng_opt_verbose == 1) || \ | |
58 | ((type & (PRINT_DBG | PRINT_DBG2)) && \ | |
59 | lttng_opt_verbose == 2) || \ | |
60 | ((type & (PRINT_DBG | PRINT_DBG2 | PRINT_DBG3)) && \ | |
61 | lttng_opt_verbose == 3))) { \ | |
62 | fprintf(stderr, fmt, ## args); \ | |
63 | } else if (lttng_opt_quiet == 0 && (type & (PRINT_WARN))) { \ | |
64 | fprintf(stderr, fmt, ## args); \ | |
65 | } else if (type & (PRINT_ERR | PRINT_BUG)) { \ | |
66 | fprintf(stderr, fmt, ## args); \ | |
67 | } \ | |
3ce7388b | 68 | } while (0); |
fac6795d | 69 | |
ffe60014 DG |
70 | /* Three level of debug. Use -v, -vv or -vvv for the levels */ |
71 | #define _ERRMSG(msg, type, fmt, args...) __lttng_print(type, msg \ | |
72 | " [%ld/%ld]: " fmt " (in %s() at " __FILE__ ":" XSTR(__LINE__) ")\n", \ | |
73 | (long) getpid(), (long) gettid(), ## args, __func__) | |
74 | ||
75dea654 DG |
75 | #define MSG(fmt, args...) \ |
76 | __lttng_print(PRINT_MSG, fmt "\n", ## args) | |
f37d259d MD |
77 | #define _MSG(fmt, args...) \ |
78 | __lttng_print(PRINT_MSG, fmt, ## args) | |
75dea654 DG |
79 | #define ERR(fmt, args...) \ |
80 | __lttng_print(PRINT_ERR, "Error: " fmt "\n", ## args) | |
81 | #define WARN(fmt, args...) \ | |
82 | __lttng_print(PRINT_WARN, "Warning: " fmt "\n", ## args) | |
75dea654 | 83 | |
ffe60014 DG |
84 | #define BUG(fmt, args...) _ERRMSG("BUG", PRINT_BUG, fmt, ## args) |
85 | ||
86 | #define DBG(fmt, args...) _ERRMSG("DEBUG1", PRINT_DBG, fmt, ## args) | |
87 | #define DBG2(fmt, args...) _ERRMSG("DEBUG2", PRINT_DBG2, fmt, ## args) | |
88 | #define DBG3(fmt, args...) _ERRMSG("DEBUG3", PRINT_DBG3, fmt, ## args) | |
89 | ||
90 | #define _PERROR(fmt, args...) _ERRMSG("PERROR", PRINT_ERR, fmt, ## args) | |
75dea654 | 91 | |
818d276f | 92 | #if !defined(__linux__) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE)) |
97e19046 | 93 | |
818d276f MD |
94 | /* |
95 | * Version using XSI strerror_r. | |
96 | */ | |
97 | #define PERROR(call, args...) \ | |
98 | do { \ | |
99 | char buf[200]; \ | |
100 | strerror_r(errno, buf, sizeof(buf)); \ | |
101 | _PERROR(call ": %s", ## args, buf); \ | |
102 | } while(0); | |
103 | #else | |
104 | /* | |
105 | * Version using GNU strerror_r, for linux with appropriate defines. | |
106 | */ | |
75dea654 | 107 | #define PERROR(call, args...) \ |
818d276f | 108 | do { \ |
75dea654 DG |
109 | char *buf; \ |
110 | char tmp[200]; \ | |
111 | buf = strerror_r(errno, tmp, sizeof(tmp)); \ | |
112 | _PERROR(call ": %s", ## args, buf); \ | |
113 | } while(0); | |
818d276f | 114 | #endif |
fac6795d | 115 | |
f73fabfd DG |
116 | const char *error_get_str(int32_t code); |
117 | ||
db758600 | 118 | #endif /* _ERROR_H */ |