14b5e7c67ea1eda8f519f2cfe42a48cd32a3a7ca
[lttng-tools.git] / src / common / error.h
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
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.
7 *
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.
12 *
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.
16 */
17
18 #ifndef _ERROR_H
19 #define _ERROR_H
20
21 #include <errno.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 /* Stringify the expansion of a define */
26 #define XSTR(d) STR(d)
27 #define STR(s) #s
28
29 extern int opt_quiet;
30 extern int opt_verbose;
31
32 #define PRINT_ERR 0x1
33 #define PRINT_WARN 0x2
34 #define PRINT_BUG 0x3
35 #define PRINT_MSG 0x4
36 #define PRINT_DBG 0x10
37 #define PRINT_DBG2 0x20
38 #define PRINT_DBG3 0x30
39
40 /*
41 * Macro for printing message depending on command line option and verbosity.
42 */
43 #define __lttng_print(type, fmt, args...) \
44 do { \
45 if (opt_quiet == 0) { \
46 if (type == PRINT_MSG) { \
47 fprintf(stdout, fmt, ## args); \
48 } else if (((type & PRINT_DBG) && opt_verbose == 1) || \
49 ((type & (PRINT_DBG | PRINT_DBG2)) && \
50 opt_verbose == 2) || \
51 ((type & (PRINT_DBG | PRINT_DBG2 | PRINT_DBG3)) && \
52 opt_verbose == 3)) { \
53 fprintf(stderr, fmt, ## args); \
54 } else if (type & (PRINT_ERR | PRINT_WARN | PRINT_BUG)) { \
55 fprintf(stderr, fmt, ## args); \
56 } \
57 } \
58 } while (0);
59
60 #define MSG(fmt, args...) \
61 __lttng_print(PRINT_MSG, fmt "\n", ## args)
62 #define ERR(fmt, args...) \
63 __lttng_print(PRINT_ERR, "Error: " fmt "\n", ## args)
64 #define WARN(fmt, args...) \
65 __lttng_print(PRINT_WARN, "Warning: " fmt "\n", ## args)
66 #define BUG(fmt, args...) \
67 __lttng_print(PRINT_BUG, "BUG: " fmt "\n", ## args)
68
69 /* Three level of debug. Use -v, -vv or -vvv for the levels */
70 #define DBG(fmt, args...) __lttng_print(PRINT_DBG, "DEBUG1: " fmt \
71 " [in %s() at " __FILE__ ":" XSTR(__LINE__) "]\n", ## args, __func__)
72 #define DBG2(fmt, args...) __lttng_print(PRINT_DBG2, "DEBUG2: " fmt \
73 " [in %s() at " __FILE__ ":" XSTR(__LINE__) "]\n", ## args, __func__)
74 #define DBG3(fmt, args...) __lttng_print(PRINT_DBG3, "DEBUG3: " fmt \
75 " [in %s() at " __FILE__ ":" XSTR(__LINE__) "]\n", ## args, __func__)
76
77 #define _PERROR(fmt, args...) \
78 __lttng_print(PRINT_ERR, "perror " fmt "\n", ## args)
79
80 #if !defined(__linux__) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE))
81 /*
82 * Version using XSI strerror_r.
83 */
84 #define PERROR(call, args...) \
85 do { \
86 char buf[200]; \
87 strerror_r(errno, buf, sizeof(buf)); \
88 _PERROR(call ": %s", ## args, buf); \
89 } while(0);
90 #else
91 /*
92 * Version using GNU strerror_r, for linux with appropriate defines.
93 */
94 #define PERROR(call, args...) \
95 do { \
96 char *buf; \
97 char tmp[200]; \
98 buf = strerror_r(errno, tmp, sizeof(tmp)); \
99 _PERROR(call ": %s", ## args, buf); \
100 } while(0);
101 #endif
102
103 #endif /* _ERROR_H */
This page took 0.041951 seconds and 3 git commands to generate.