2 err_mac - convenient macros for error-checking
4 Copyright (C) 2007-2009 William Trevor King
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 3 of the
9 License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 The author may be contacted at <wking@drexel.edu> on the Internet, or
22 write to Trevor King, Drexel University, Physics Dept., 3141 Chestnut St.,
23 Philadelphia PA 19104, USA.
29 #include <stdio.h> /* for fprintf(), stderr */
40 static int err_mac_ans;
43 * For functions returning int, when you want an immediate return.
46 /* CHK() fails if (a != SUCCESS) */
49 if( (err_mac_ans = (a)) != SUCCESS ) { \
50 fprintf(stderr,"FAILURE: %s.%d. (%s) returned %d\n",__FILE__,__LINE__, #a, err_mac_ans); \
56 /* AST() fails if (a != 0) */
59 if( (err_mac_ans = (a)) == FALSE ) { \
60 fprintf(stderr, "ASSERT FAILED: %s.%d (%s) returned %d\n",__FILE__,__LINE__, #a, err_mac_ans); \
66 /* like AST(), but return E_CODE instead of the default FAILURE */
67 #define E_AST(a,e_code) \
70 fprintf(stderr,"ASSERT FAILED: %s.%d (%s)\n",__FILE__,__LINE__, #a); \
76 /* like E_AST(), but also fprintfs __VA_ARGS__ */
77 #define M_AST(a,e_code,...) \
80 fprintf(stderr,"ASSERT FAILED: %s.%d (%s) ",__FILE__,__LINE__, #a); \
81 fprintf(stderr, __VA_ARGS__); \
87 /* print a message and return E_CODE */
88 #define M_EXIT(e_code, ... ) \
90 fprintf(stderr,"EXIT: %s.%d ",__FILE__,__LINE__); \
91 fprintf(stderr, __VA_ARGS__ ); \
97 * When you don't want an immedate return, but would rather 'goto Error;'.
98 * These set the local variable RC with the return value before jumping.
103 if( (rc = (a)) != SUCCESS ) { \
104 fprintf(stderr,"FAILURE: %s.%d. (%s)\n",__FILE__,__LINE__, #a); \
112 if( (rc = (a)) == FALSE ) { \
113 fprintf(stderr, "ASSERT FAILED: %s.%d (%s)\n",__FILE__,__LINE__, #a); \
119 #define G_E_AST(a,e_code) \
122 fprintf(stderr,"ASSERT FAILED: %s.%d (%s)\n",__FILE__,__LINE__, #a); \
129 #define G_M_AST(a,e_code,...) \
132 fprintf(stderr,"ASSERT FAILED: %s.%d (%s) ",__FILE__,__LINE__, #a); \
133 fprintf(stderr, __VA_ARGS__); \
140 /* print a message and return E_CODE */
141 #define G_M_EXIT(e_code, ... ) \
143 fprintf(stderr,"EXIT: %s.%d ",__FILE__,__LINE__); \
144 fprintf(stderr, __VA_ARGS__ ); \
151 * And when you only want to print a message
155 if( (a) != SUCCESS ) { \
156 fprintf(stderr,"FAILURE: %s.%d. (%s)\n",__FILE__,__LINE__, #a); \
161 /* AST() fails if (a != 0) */
164 if( (a) == FALSE ) { \
165 fprintf(stderr, "ASSERT FAILED: %s.%d (%s)\n",__FILE__,__LINE__, #a); \
170 #endif /* _ERR_MAC_H */