Fix: do not print error and bug messages when quiet (-q) is present
[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
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
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
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #ifndef _ERROR_H
19 #define _ERROR_H
20
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdint.h>
24 #include <string.h>
25 #include <urcu/tls-compat.h>
26 #include <time.h>
27
28 #ifndef _GNU_SOURCE
29 #error "lttng-tools error.h needs _GNU_SOURCE"
30 #endif
31
32 #include <lttng/lttng-error.h>
33 #include <common/compat/tid.h>
34
35 /* Stringify the expansion of a define */
36 #define XSTR(d) STR(d)
37 #define STR(s) #s
38
39 /*
40 * Contains the string of the log entry time. This is used as a thread local
41 * storage so we don't race between thread and also avoid memory allocation
42 * every time a log is fired.
43 */
44 struct log_time {
45 /* Format: 00:00:00.000000 plus NULL byte. */
46 char str[16];
47 };
48 extern DECLARE_URCU_TLS(struct log_time, error_log_time);
49
50 extern int lttng_opt_quiet;
51 extern int lttng_opt_verbose;
52 extern int lttng_opt_mi;
53
54 /* Error type. */
55 #define PRINT_ERR 0x1
56 #define PRINT_WARN 0x2
57 #define PRINT_BUG 0x3
58 #define PRINT_MSG 0x4
59 #define PRINT_DBG 0x10
60 #define PRINT_DBG2 0x20
61 #define PRINT_DBG3 0x30
62
63 /*
64 * Macro for printing message depending on command line option and verbosity.
65 *
66 * Machine interface:
67 * We use lttng_opt_mi to suppress all normal msg to stdout. We don't
68 * want any nested msg to show up when printing mi to stdout(if it's the case).
69 * All warnings and errors should be printed to stderr as normal.
70 */
71 #define __lttng_print(type, fmt, args...) \
72 do { \
73 if (lttng_opt_quiet == 0 && lttng_opt_mi == 0 && \
74 type == PRINT_MSG) { \
75 fprintf(stdout, fmt, ## args); \
76 } else if (lttng_opt_quiet == 0 && lttng_opt_mi == 0 && \
77 (((type & PRINT_DBG) && lttng_opt_verbose == 1) || \
78 ((type & (PRINT_DBG | PRINT_DBG2)) && \
79 lttng_opt_verbose == 2) || \
80 ((type & (PRINT_DBG | PRINT_DBG2 | PRINT_DBG3)) && \
81 lttng_opt_verbose == 3))) { \
82 fprintf(stderr, fmt, ## args); \
83 } else if (lttng_opt_quiet == 0 && \
84 (type & (PRINT_WARN | PRINT_ERR | PRINT_BUG))) { \
85 fprintf(stderr, fmt, ## args); \
86 } \
87 } while (0);
88
89 /* Three level of debug. Use -v, -vv or -vvv for the levels */
90 #define _ERRMSG(msg, type, fmt, args...) __lttng_print(type, msg \
91 " - %s [%ld/%ld]: " fmt " (in %s() at " __FILE__ ":" XSTR(__LINE__) ")\n", \
92 log_add_time(), (long) getpid(), (long) gettid(), ## args, __func__)
93
94 #define MSG(fmt, args...) \
95 __lttng_print(PRINT_MSG, fmt "\n", ## args)
96 #define _MSG(fmt, args...) \
97 __lttng_print(PRINT_MSG, fmt, ## args)
98 #define ERR(fmt, args...) \
99 __lttng_print(PRINT_ERR, "Error: " fmt "\n", ## args)
100 #define WARN(fmt, args...) \
101 __lttng_print(PRINT_ERR, "Warning: " fmt "\n", ## args)
102
103 #define BUG(fmt, args...) _ERRMSG("BUG", PRINT_BUG, fmt, ## args)
104
105 #define DBG(fmt, args...) _ERRMSG("DEBUG1", PRINT_DBG, fmt, ## args)
106 #define DBG2(fmt, args...) _ERRMSG("DEBUG2", PRINT_DBG2, fmt, ## args)
107 #define DBG3(fmt, args...) _ERRMSG("DEBUG3", PRINT_DBG3, fmt, ## args)
108
109 #define _PERROR(fmt, args...) _ERRMSG("PERROR", PRINT_ERR, fmt, ## args)
110
111 #if !defined(__linux__) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE))
112
113 /*
114 * Version using XSI strerror_r.
115 */
116 #define PERROR(call, args...) \
117 do { \
118 char buf[200]; \
119 strerror_r(errno, buf, sizeof(buf)); \
120 _PERROR(call ": %s", ## args, buf); \
121 } while(0);
122 #else
123 /*
124 * Version using GNU strerror_r, for linux with appropriate defines.
125 */
126 #define PERROR(call, args...) \
127 do { \
128 char *buf; \
129 char tmp[200]; \
130 buf = strerror_r(errno, tmp, sizeof(tmp)); \
131 _PERROR(call ": %s", ## args, buf); \
132 } while(0);
133 #endif
134
135 const char *error_get_str(int32_t code);
136
137 /*
138 * Function that format the time and return the reference of log_time.str to
139 * the caller. On error, an empty string is returned thus no time will be
140 * printed in the log.
141 */
142 const char *log_add_time();
143
144 #endif /* _ERROR_H */
This page took 0.032708 seconds and 4 git commands to generate.