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.
26 #include <stdio.h> /* for fprintf(), stderr */
37 static int err_mac_ans;
40 * For functions returning int, when you want an immediate return.
43 /* CHK() fails if (a != SUCCESS) */
46 if( (err_mac_ans = (a)) != SUCCESS ) { \
47 fprintf(stderr,"FAILURE: %s.%d. (%s) returned %d\n",__FILE__,__LINE__, #a, err_mac_ans); \
53 /* AST() fails if (a != 0) */
56 if( (err_mac_ans = (a)) == FALSE ) { \
57 fprintf(stderr, "ASSERT FAILED: %s.%d (%s) returned %d\n",__FILE__,__LINE__, #a, err_mac_ans); \
63 /* like AST(), but return E_CODE instead of the default FAILURE */
64 #define E_AST(a,e_code) \
67 fprintf(stderr,"ASSERT FAILED: %s.%d (%s)\n",__FILE__,__LINE__, #a); \
73 /* like E_AST(), but also fprintfs __VA_ARGS__ */
74 #define M_AST(a,e_code,...) \
77 fprintf(stderr,"ASSERT FAILED: %s.%d (%s) ",__FILE__,__LINE__, #a); \
78 fprintf(stderr, __VA_ARGS__); \
84 /* print a message and return E_CODE */
85 #define M_EXIT(e_code, ... ) \
87 fprintf(stderr,"EXIT: %s.%d ",__FILE__,__LINE__); \
88 fprintf(stderr, __VA_ARGS__ ); \
94 * When you don't want an immedate return, but would rather 'goto Error;'.
95 * These set the local variable RC with the return value before jumping.
100 if( (rc = (a)) != SUCCESS ) { \
101 fprintf(stderr,"FAILURE: %s.%d. (%s)\n",__FILE__,__LINE__, #a); \
109 if( (rc = (a)) == FALSE ) { \
110 fprintf(stderr, "ASSERT FAILED: %s.%d (%s)\n",__FILE__,__LINE__, #a); \
116 #define G_E_AST(a,e_code) \
119 fprintf(stderr,"ASSERT FAILED: %s.%d (%s)\n",__FILE__,__LINE__, #a); \
126 #define G_M_AST(a,e_code,...) \
129 fprintf(stderr,"ASSERT FAILED: %s.%d (%s) ",__FILE__,__LINE__, #a); \
130 fprintf(stderr, __VA_ARGS__); \
137 /* print a message and return E_CODE */
138 #define G_M_EXIT(e_code, ... ) \
140 fprintf(stderr,"EXIT: %s.%d ",__FILE__,__LINE__); \
141 fprintf(stderr, __VA_ARGS__ ); \
148 * And when you only want to print a message
152 if( (a) != SUCCESS ) { \
153 fprintf(stderr,"FAILURE: %s.%d. (%s)\n",__FILE__,__LINE__, #a); \
158 /* AST() fails if (a != 0) */
161 if( (a) == FALSE ) { \
162 fprintf(stderr, "ASSERT FAILED: %s.%d (%s)\n",__FILE__,__LINE__, #a); \