Commit | Line | Data |
---|---|---|
826d496d MD |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
fac6795d | 3 | * |
db758600 DG |
4 | * This program is free software; you can redistribute it and/or modify it |
5 | * under the terms of the GNU General Public License as published by the Free | |
6 | * Software Foundation; only version 2 of the License. | |
fac6795d | 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. | |
fac6795d | 12 | * |
db758600 DG |
13 | * You should have received a copy of the GNU General Public License along with |
14 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple | |
15 | * Place - Suite 330, Boston, MA 02111-1307, 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> |
fac6795d | 23 | |
57d0d501 DG |
24 | /* Stringify the expansion of a define */ |
25 | #define XSTR(d) STR(d) | |
26 | #define STR(s) #s | |
27 | ||
fac6795d DG |
28 | extern int opt_quiet; |
29 | extern int opt_verbose; | |
30 | ||
75dea654 DG |
31 | #define PRINT_ERR 0x1 |
32 | #define PRINT_WARN 0x2 | |
33 | #define PRINT_BUG 0x3 | |
34 | #define PRINT_MSG 0x4 | |
35 | #define PRINT_DBG 0x10 | |
36 | #define PRINT_DBG2 0x20 | |
37 | #define PRINT_DBG3 0x30 | |
fac6795d DG |
38 | |
39 | /* | |
75dea654 | 40 | * Macro for printing message depending on command line option and verbosity. |
fac6795d | 41 | */ |
75dea654 DG |
42 | #define __lttng_print(type, fmt, args...) \ |
43 | do { \ | |
44 | if (opt_quiet == 0) { \ | |
1fea938d DG |
45 | if (type == PRINT_MSG) { \ |
46 | fprintf(stdout, fmt, ## args); \ | |
47 | } else if (((type & PRINT_DBG) && opt_verbose == 1) || \ | |
75dea654 DG |
48 | ((type & (PRINT_DBG | PRINT_DBG2)) && \ |
49 | opt_verbose == 2) || \ | |
50 | ((type & (PRINT_DBG | PRINT_DBG2 | PRINT_DBG3)) && \ | |
51 | opt_verbose == 3)) { \ | |
94484837 | 52 | fprintf(stderr, fmt, ## args); \ |
75dea654 DG |
53 | } else if (type & (PRINT_ERR | PRINT_WARN | PRINT_BUG)) { \ |
54 | fprintf(stderr, fmt, ## args); \ | |
55 | } \ | |
56 | } \ | |
3ce7388b | 57 | } while (0); |
fac6795d | 58 | |
75dea654 DG |
59 | #define MSG(fmt, args...) \ |
60 | __lttng_print(PRINT_MSG, fmt "\n", ## args) | |
61 | #define ERR(fmt, args...) \ | |
62 | __lttng_print(PRINT_ERR, "Error: " fmt "\n", ## args) | |
63 | #define WARN(fmt, args...) \ | |
64 | __lttng_print(PRINT_WARN, "Warning: " fmt "\n", ## args) | |
65 | #define BUG(fmt, args...) \ | |
66 | __lttng_print(PRINT_BUG, "BUG: " fmt "\n", ## args) | |
67 | ||
68 | /* Three level of debug. Use -v, -vv or -vvv for the levels */ | |
69 | #define DBG(fmt, args...) __lttng_print(PRINT_DBG, "DEBUG1: " fmt \ | |
70 | " [in %s() at " __FILE__ ":" XSTR(__LINE__) "]\n", ## args, __func__) | |
71 | #define DBG2(fmt, args...) __lttng_print(PRINT_DBG2, "DEBUG2: " fmt \ | |
72 | " [in %s() at " __FILE__ ":" XSTR(__LINE__) "]\n", ## args, __func__) | |
73 | #define DBG3(fmt, args...) __lttng_print(PRINT_DBG3, "DEBUG3: " fmt \ | |
74 | " [in %s() at " __FILE__ ":" XSTR(__LINE__) "]\n", ## args, __func__) | |
75 | ||
76 | #define _PERROR(fmt, args...) \ | |
77 | __lttng_print(PRINT_ERR, "perror " fmt "\n", ## args) | |
78 | ||
79 | #define PERROR(call, args...) \ | |
80 | do { \ | |
81 | char *buf; \ | |
82 | char tmp[200]; \ | |
83 | buf = strerror_r(errno, tmp, sizeof(tmp)); \ | |
84 | _PERROR(call ": %s", ## args, buf); \ | |
85 | } while(0); | |
fac6795d | 86 | |
db758600 | 87 | #endif /* _ERROR_H */ |