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_FAIL do { ret = -1; goto cleanup; } while (0) 21#define EC_CLEANUP cleanup 22#define EC_EXIT return ret 23 24/* 25 * Check out doc/DEVELOPER for more infos. 26 * 27 * We have these macros: 28 * EC_ZERO, EC_ZERO_LOG, EC_ZERO_LOGSTR, EC_ZERO_LOG_ERR, EC_ZERO_CUSTOM 29 * EC_NEG1, EC_NEG1_LOG, EC_NEG1_LOGSTR, EC_NEG1_LOG_ERR, EC_NEG1_CUSTOM 30 * EC_NULL, EC_NULL_LOG, EC_NULL_LOGSTR, EC_NULL_LOG_ERR, EC_NULL_CUSTOM 31 * 32 * A boileplate function template is: 33 34 int func(void) 35 { 36 EC_INIT; 37 38 ...your code here... 39 40 EC_STATUS(0); 41 42 EC_CLEANUP: 43 EC_EXIT; 44 } 45 */ 46 47/* check for return val 0 which is ok, every other is an error, prints errno */ 48#define EC_ZERO_LOG(a) \ 49 do { \ 50 if ((a) != 0) { \ 51 LOG(log_error, logtype_default, "%s failed: %s", #a, strerror(errno)); \ 52 ret = -1; \ 53 goto cleanup; \ 54 } \ 55 } while (0) 56 57#define EC_ZERO_LOGSTR(a, b, ...) \ 58 do { \ 59 if ((a) != 0) { \ 60 LOG(log_error, logtype_default, b, __VA_ARGS__); \ 61 ret = -1; \ 62 goto cleanup; \ 63 } \ 64 } while (0) 65 66#define EC_ZERO_LOG_ERR(a, b) \ 67 do { \ 68 if ((a) != 0) { \ 69 LOG(log_error, logtype_default, "%s failed: %s", #a, strerror(errno)); \ 70 ret = (b); \ 71 goto cleanup; \ 72 } \ 73 } while (0) 74 75#define EC_ZERO(a) \ 76 do { \ 77 if ((a) != 0) { \ 78 ret = -1; \ 79 goto cleanup; \ 80 } \ 81 } while (0) 82 83#define EC_ZERO_ERR(a,b ) \ 84 do { \ 85 if ((a) != 0) { \ 86 ret = b; \ 87 goto cleanup; \ 88 } \ 89 } while (0) 90 91/* check for return val 0 which is ok, every other is an error, prints errno */ 92#define EC_NEG1_LOG(a) \ 93 do { \ 94 if ((a) == -1) { \ 95 LOG(log_error, logtype_default, "%s failed: %s", #a, strerror(errno)); \ 96 ret = -1; \ 97 goto cleanup; \ 98 } \ 99 } while (0) 100 101#define EC_NEG1_LOGSTR(a, b, ...) \ 102 do { \ 103 if ((a) == -1) { \ 104 LOG(log_error, logtype_default, b, __VA_ARGS__); \ 105 ret = -1; \ 106 goto cleanup; \ 107 } \ 108 } while (0) 109 110#define EC_NEG1_LOG_ERR(a, b) \ 111 do { \ 112 if ((a) == -1) { \ 113 LOG(log_error, logtype_default, "%s failed: %s", #a, strerror(errno)); \ 114 ret = b; \ 115 goto cleanup; \ 116 } \ 117 } while (0) 118 119#define EC_NEG1(a) \ 120 do { \ 121 if ((a) == -1) { \ 122 ret = -1; \ 123 goto cleanup; \ 124 } \ 125 } while (0) 126 127/* check for return val != NULL, prints errno */ 128#define EC_NULL_LOG(a) \ 129 do { \ 130 if ((a) == NULL) { \ 131 LOG(log_error, logtype_default, "%s failed: %s", #a, strerror(errno)); \ 132 ret = -1; \ 133 goto cleanup; \ 134 } \ 135 } while (0) 136 137#define EC_NULL_LOGSTR(a, b, ...) \ 138 do { \ 139 if ((a) == NULL) { \ 140 LOG(log_error, logtype_default, b , __VA_ARGS__); \ 141 ret = -1; \ 142 goto cleanup; \ 143 } \ 144 } while (0) 145 146#define EC_NULL_LOG_ERR(a, b) \ 147 do { \ 148 if ((a) == NULL) { \ 149 LOG(log_error, logtype_default, "%s failed: %s", #a, strerror(errno)); \ 150 ret = b; \ 151 goto cleanup; \ 152 } \ 153 } while (0) 154 155#define EC_NULL(a) \ 156 do { \ 157 if ((a) == NULL) { \ 158 ret = -1; \ 159 goto cleanup; \ 160 } \ 161 } while (0) 162 163#endif /* ERRCHECK_H */ 164