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