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