btrfs-progs: move message helpers out of utils
[platform/upstream/btrfs-progs.git] / messages.h
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public
4  * License v2 as published by the Free Software Foundation.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
9  * General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public
12  * License along with this program; if not, write to the
13  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14  * Boston, MA 021110-1307, USA.
15  */
16
17 #ifndef __BTRFS_MESSAGES_H__
18 #define __BTRFS_MESSAGES_H__
19
20 #if DEBUG_VERBOSE_ERROR
21 #define PRINT_VERBOSE_ERROR     fprintf(stderr, "%s:%d:", __FILE__, __LINE__)
22 #else
23 #define PRINT_VERBOSE_ERROR
24 #endif
25
26 #if DEBUG_TRACE_ON_ERROR
27 #define PRINT_TRACE_ON_ERROR    print_trace()
28 #else
29 #define PRINT_TRACE_ON_ERROR
30 #endif
31
32 #if DEBUG_ABORT_ON_ERROR
33 #define DO_ABORT_ON_ERROR       abort()
34 #else
35 #define DO_ABORT_ON_ERROR
36 #endif
37
38 #define error(fmt, ...)                                                 \
39         do {                                                            \
40                 PRINT_TRACE_ON_ERROR;                                   \
41                 PRINT_VERBOSE_ERROR;                                    \
42                 __error((fmt), ##__VA_ARGS__);                          \
43                 DO_ABORT_ON_ERROR;                                      \
44         } while (0)
45
46 #define error_on(cond, fmt, ...)                                        \
47         do {                                                            \
48                 if ((cond))                                             \
49                         PRINT_TRACE_ON_ERROR;                           \
50                 if ((cond))                                             \
51                         PRINT_VERBOSE_ERROR;                            \
52                 __error_on((cond), (fmt), ##__VA_ARGS__);               \
53                 if ((cond))                                             \
54                         DO_ABORT_ON_ERROR;                              \
55         } while (0)
56
57 #define warning(fmt, ...)                                               \
58         do {                                                            \
59                 PRINT_TRACE_ON_ERROR;                                   \
60                 PRINT_VERBOSE_ERROR;                                    \
61                 __warning((fmt), ##__VA_ARGS__);                        \
62         } while (0)
63
64 #define warning_on(cond, fmt, ...)                                      \
65         do {                                                            \
66                 if ((cond))                                             \
67                         PRINT_TRACE_ON_ERROR;                           \
68                 if ((cond))                                             \
69                         PRINT_VERBOSE_ERROR;                            \
70                 __warning_on((cond), (fmt), ##__VA_ARGS__);             \
71         } while (0)
72
73 __attribute__ ((format (printf, 1, 2)))
74 static inline void __warning(const char *fmt, ...)
75 {
76         va_list args;
77
78         fputs("WARNING: ", stderr);
79         va_start(args, fmt);
80         vfprintf(stderr, fmt, args);
81         va_end(args);
82         fputc('\n', stderr);
83 }
84
85 __attribute__ ((format (printf, 1, 2)))
86 static inline void __error(const char *fmt, ...)
87 {
88         va_list args;
89
90         fputs("ERROR: ", stderr);
91         va_start(args, fmt);
92         vfprintf(stderr, fmt, args);
93         va_end(args);
94         fputc('\n', stderr);
95 }
96
97 __attribute__ ((format (printf, 2, 3)))
98 static inline int __warning_on(int condition, const char *fmt, ...)
99 {
100         va_list args;
101
102         if (!condition)
103                 return 0;
104
105         fputs("WARNING: ", stderr);
106         va_start(args, fmt);
107         vfprintf(stderr, fmt, args);
108         va_end(args);
109         fputc('\n', stderr);
110
111         return 1;
112 }
113
114 __attribute__ ((format (printf, 2, 3)))
115 static inline int __error_on(int condition, const char *fmt, ...)
116 {
117         va_list args;
118
119         if (!condition)
120                 return 0;
121
122         fputs("ERROR: ", stderr);
123         va_start(args, fmt);
124         vfprintf(stderr, fmt, args);
125         va_end(args);
126         fputc('\n', stderr);
127
128         return 1;
129 }
130
131 #endif