]> arthur.barton.de Git - netatalk.git/blob - include/atalk/errchk.h
More error checking
[netatalk.git] / include / atalk / errchk.h
1 /*
2    Copyright (c) 2010 Frank Lahm <franklahm@gmail.com>
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 as published by
6    the Free Software Foundation; either version 2 of the License, or
7    (at your option) any later version.
8  
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13  */
14
15 #ifndef ERRCHECK_H
16 #define ERRCHECK_H
17
18 #define EC_INIT int ret = 0
19 #define EC_STATUS(a) ret = (a)
20 #define EC_CLEANUP cleanup
21 #define EC_EXIT return ret
22
23 /* 
24  * Check out doc/DEVELOPER for more infos.
25  *
26  * We have these macros:
27  * EC_ZERO, EC_ZERO_LOG, EC_ZERO_LOG_ERR, EC_ZERO_CUSTOM
28  * EC_NEG1, EC_NEG1_LOG, EC_NEG1_LOG_ERR, EC_NEG1_CUSTOM
29  * EC_NULL, EC_NULL_LOG, EC_NULL_LOG_ERR, EC_NULL_CUSTOM
30  *
31  * A boileplate function template is:
32
33    int func(void)
34    {
35        EC_INIT;
36
37        ...your code here...
38
39        EC_STATUS(0);
40
41    EC_CLEANUP:
42        EC_EXIT;
43    }
44  */
45
46 /* check for return val 0 which is ok, every other is an error, prints errno */
47 #define EC_ZERO_LOG(a)                          \
48     do {                                        \
49         if ((a) != 0) {                                                 \
50             LOG(log_error, logtype_default, "%s failed: %s" #a, strerror(errno)); \
51             ret = -1;                                                   \
52             goto cleanup;                                               \
53         } \
54     } while (0)
55
56 #define EC_ZERO_LOG_ERR(a, b)                   \
57     do { \
58         if ((a) != 0) {                                                 \
59             LOG(log_error, logtype_default, "%s failed: %s" #a, strerror(errno)); \
60             ret = (b);                                                  \
61             goto cleanup;                                               \
62         }                                                               \
63     } while (0)
64
65 /* check for return val 0 which is ok, every other is an error */
66 #define EC_ZERO(a) \
67     do { \
68         if ((a) != 0) { \
69             ret = -1; \
70             goto cleanup; \
71         } \
72     } while (0)
73
74 /* check for return val 0 which is ok, every other is an error */
75 #define EC_ZERO_CUSTOM(a) \
76     (ret = (a)) != 0
77
78 /* check for return val 0 which is ok, every other is an error, prints errno */
79 #define EC_NEG1_LOG(a) \
80     do { \
81         if ((a) == -1) { \
82             LOG(log_error, logtype_default, "%s failed: %s" #a, strerror(errno)); \
83             ret = -1; \
84             goto cleanup; \
85         } \
86     } while (0)
87
88 #define EC_NEG1_LOG_ERR(a, b)                   \
89     do {                                        \
90         if ((a) == -1) {                                                \
91             LOG(log_error, logtype_default, "%s failed: %s" #a, strerror(errno)); \
92             ret = b;                                                    \
93             goto cleanup;                                               \
94         }                                                               \
95     } while (0)
96
97 /* check for return val 0 which is ok, every other is an error */
98 #define EC_NEG1(a) \
99     do { \
100         if ((a) == -1) { \
101             ret = -1; \
102             goto cleanup; \
103         } \
104     } while (0)
105
106 #define EC_NEG1_CUSTOM(a) \
107     (ret = (a)) == -1
108
109 /* check for return val != NULL, prints errno */
110 #define EC_NULL_LOG(a) \
111     do { \
112         if ((a) == NULL) { \
113             LOG(log_error, logtype_default, "%s failed: %s" #a, strerror(errno)); \
114             ret = -1; \
115             goto cleanup; \
116         } \
117     } while (0)
118
119 #define EC_NULL_LOG_ERR(a, b)                   \
120     do {                                        \
121         if ((a) == NULL) {                                              \
122             LOG(log_error, logtype_default, "%s failed: %s" #a, strerror(errno)); \
123             ret = b;                                                    \
124             goto cleanup;                                               \
125         }                                                               \
126     } while (0)
127
128 /* check for return val != NULL */
129 #define EC_NULL(a) \
130     do { \
131         if ((a) == NULL) { \
132             ret = -1; \
133             goto cleanup; \
134         } \
135     } while (0)
136
137 #define EC_NULL_CUSTOM(a) \
138     (ret = (a)) == NULL
139
140 #endif /* ERRCHECK_H */