LAL  7.1.7.1-56c71ae
LALStatusMacros.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2007 Jolien Creighton
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 * You should have received a copy of the GNU General Public License
15 * along with with program; see the file COPYING. If not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * MA 02110-1301 USA
18 */
19 
20 /* ---------- SEE LALStatusMacros.dox for doxygen documentation ---------- */
21 
22 #ifndef _LALSTATUSMACROS_H
23 #define _LALSTATUSMACROS_H
24 
25 #include <lal/LALConfig.h>
26 #ifdef NDEBUG
27 #ifndef LAL_NDEBUG
28 #define LAL_NDEBUG
29 #endif
30 #endif
31 
32 #include <stdlib.h>
33 #include <string.h>
34 
35 #include <lal/LALDebugLevel.h>
36 #include <lal/LALMalloc.h>
37 #include <lal/LALDatatypes.h>
38 #include <lal/LALError.h>
39 #include <lal/XLALError.h>
40 
41 #ifdef __cplusplus
42 extern "C" {
43 #elif 0
44 } /* so that editors will match preceding brace */
45 #endif
46 
47 extern const int lalNoDebug;
48 
49 #define LAL_EXLAL 16384
50 #define LAL_MSGEXLAL "Failure in an XLAL routine"
51 #define ABORTXLAL(sp) ABORT(sp,LAL_EXLAL,LAL_MSGEXLAL)
52 #define XLAL_CHECK_LAL(sp, assertion, ...) _XLAL_CHECK_IMPL_(ABORTXLAL(sp), assertion, __VA_ARGS__)
53 
54 #ifndef NOLALMACROS
55 
56 #define INITSTATUS( statusptr ) \
57  do { if ( (statusptr) ) \
58  { \
59  INT4 level_ = (statusptr)->level ; \
60  INT4 statp_ = (statusptr)->statusPtr ? 1 : 0 ; \
61  memset( (statusptr), 0, sizeof( LALStatus ) ); /* possible memory leak */ \
62  (statusptr)->level = level_ > 0 ? level_ : 1 ; \
63  (statusptr)->Id = "$Id$"; \
64  (statusptr)->function = __func__; \
65  SETSTATUSFILELINE( statusptr ); \
66  (void) LALTrace( statusptr, 0 ); \
67  if ( statp_ ) \
68  ABORT( statusptr, -2, "INITSTATUS: non-null status pointer" ); \
69  else if ( xlalErrno ) \
70  ABORT( statusptr, -16, "INITSTATUS: non-zero xlalErrno" ); \
71  } \
72  else \
73  lalAbortHook( "Abort: function %s, file %s, line %d, %s\n" \
74  " Null status pointer passed to function\n", \
75  __func__, __FILE__, __LINE__, "$Id$" ); \
76  } while ( 0 )
77 
78 #define RETURN( statusptr ) \
79  do { if ( 1 ) \
80  { \
81  SETSTATUSFILELINE( statusptr ); \
82  if ( (statusptr)->statusCode ) \
83  (void) LALError( statusptr, "RETURN:" ); \
84  (void) LALTrace( statusptr, 1 ); \
85  if ( xlalErrno ) \
86  ABORT( statusptr, -32, "RETURN: untrapped XLAL error" ); \
87  return; \
88  } \
89  } while ( 0 )
90 
91 #define ATTATCHSTATUSPTR(statusptr) \
92  do { if ( !(statusptr)->statusPtr ) \
93  { \
94  (statusptr)->statusPtr = (LALStatus *)LALCalloc( 1, sizeof( LALStatus ) );\
95  if ( !(statusptr)->statusPtr ) \
96  ABORT( statusptr, -4, "ATTATCHSTATUSPTR: memory allocation error" ); \
97  (statusptr)->statusPtr->level = (statusptr)->level + 1; \
98  } \
99  else \
100  ABORT( statusptr, -2, "ATTATCHSTATUSPTR: non-null status pointer" ); \
101  } while ( 0 )
102 
103 #define DETATCHSTATUSPTR( statusptr ) \
104  do { if ( (statusptr)->statusPtr ) \
105  { \
106  FREESTATUSPTR( statusptr ); \
107  (statusptr)->statusCode = 0; \
108  (statusptr)->statusDescription = NULL; \
109  } \
110  else \
111  ABORT( statusptr, -8, "DETATCHSTATUSPTR: null status pointer" ); \
112  } while ( 0 )
113 
114 #define ABORT( statusptr, code, mesg ) \
115  do { if ( (statusptr)->statusPtr ) FREESTATUSPTR( statusptr ); \
116  SETSTATUS( statusptr, code, mesg ); \
117  if ( code ) \
118  (void) LALError( statusptr, "ABORT:" ); \
119  (void) LALTrace( statusptr, 1 ); \
120  return; \
121  } while ( 0 )
122 
123 #ifdef LAL_NDEBUG
124 #define ASSERT( assertion, statusptr, code, mesg )
125 #else
126 #define ASSERT( assertion, statusptr, code, mesg ) \
127  do { if ( !(assertion) ) \
128  { \
129  if ( (statusptr)->statusPtr ) \
130  FREESTATUSPTR( statusptr ); \
131  SETSTATUS( statusptr, code, mesg ); \
132  (void) LALError( statusptr, "Assertion \"" #assertion "\" failed:" ); \
133  (void) LALTrace( statusptr, 1 ); \
134  return; \
135  } \
136  } while ( 0 )
137 #endif
138 
139 #define TRY( func, statusptr ) \
140  do { if ( (func), (statusptr)->statusPtr->statusCode ) \
141  { \
142  SETSTATUS( statusptr, -1, "Recursive error" ); \
143  (void) LALError( statusptr, "Function call \"" #func "\" failed:" ); \
144  (void) LALTrace( statusptr, 1 ); \
145  return; \
146  } \
147  } while ( 0 )
148 
149 #define CHECKSTATUSPTR( statusptr ) \
150  do { if ( (statusptr)->statusPtr->statusCode ) \
151  { \
152  SETSTATUS( statusptr, -1, "Recursive error" ); \
153  (void) LALError( statusptr, "CHECKSTATUSPTR:" ); \
154  (void) LALTrace( statusptr, 1 ); \
155  return; \
156  } \
157  } while ( 0 )
158 
159 #define FREESTATUSPTR( statusptr ) \
160  do \
161  { \
162  LALStatus *next_ = (statusptr)->statusPtr->statusPtr; \
163  LALFree( (statusptr)->statusPtr ); \
164  (statusptr)->statusPtr = next_; \
165  } \
166  while ( (statusptr)->statusPtr )
167 
168 #define REPORTSTATUS( statusptr ) \
169  do \
170  { \
171  LALStatus *ptr_; \
172  for ( ptr_ = (statusptr); ptr_; ptr_ = ptr_->statusPtr ) \
173  { \
174  LALPrintError( "\nLevel %i: %s\n", ptr_->level, ptr_->Id ); \
175  if ( ptr_->statusCode ) \
176  LALPrintError( "\tStatus code %i: %s\n", ptr_->statusCode, \
177  ptr_->statusDescription ); \
178  else \
179  LALPrintError( "\tStatus code 0: Nominal\n" ); \
180  LALPrintError( "\tfunction %s, file %s, line %i\n", \
181  ptr_->function, ptr_->file, ptr_->line ); \
182  } \
183  } while ( 0 )
184 
185 #else /* NOLALMACROS */
186 
187 #define INITSTATUS( statusptr ) \
188  do { if ( LALInitStatus( statusptr, __func__, "$Id$", __FILE__, __LINE__ ) ) return; } while ( 0 )
189 
190 #define RETURN( statusptr ) \
191  do { if ( LALPrepareReturn( statusptr, __FILE__, __LINE__ ), 1 ) return; } while ( 0 )
192 
193 #define ATTATCHSTATUSPTR( statusptr ) \
194  do { if ( LALAttatchStatusPtr( statusptr, __FILE__, __LINE__ ) ) return; } while ( 0 )
195 
196 #define DETATCHSTATUSPTR( statusptr ) \
197  do { if ( LALDetatchStatusPtr( statusptr, __FILE__, __LINE__ ) ) return; } while ( 0 )
198 
199 #define ABORT( statusptr, code, mesg ) \
200  do { if ( LALPrepareAbort( statusptr, code, mesg, __FILE__, __LINE__ ), 1 ) return; } while ( 0 )
201 
202 #ifdef LAL_NDEBUG
203 #define ASSERT( assertion, statusptr, code, mesg )
204 #else
205 #define ASSERT( assertion, statusptr, code, mesg ) \
206  do { if ( !(assertion) ) \
207  { \
208  LALPrepareAssertFail( statusptr, code, mesg, \
209  "Assertion \"" #assertion "\" failed:", \
210  __FILE__, __LINE__ ); \
211  return; \
212  } \
213  } while ( 0 )
214 #endif
215 
216 #define TRY( func, statusptr ) \
217  do \
218  { \
219  (func); \
220  if ( LALCheckStatusPtr( statusptr, "Function call \"" #func "\" failed:", \
221  __FILE__, __LINE__ ) ) \
222  return; \
223  } \
224  while ( 0 )
225 
226 #define CHECKSTATUSPTR( statusptr ) \
227  do { if ( LALCheckStatusPtr( statusptr, "CHECKSTATUSPTR:", __FILE__, __LINE__ ) ) return; } while ( 0 )
228 
229 #endif /* NOLALMACROS */
230 
231 /* these just have to be macros... */
232 
233 #define BEGINFAIL( statusptr ) \
234 do { \
235  if ( !(statusptr) ) \
236  ABORT( statusptr, -8, "BEGINFAIL: null status pointer" ); \
237  if ( !( (statusptr)->statusPtr ) ) \
238  ABORT( statusptr, -8, "BEGINFAIL: null status pointer pointer" ); \
239  if ( (statusptr)->statusPtr->statusCode ) { \
240  LALStatus *statusPtrSave_ = (statusptr)->statusPtr; \
241  (statusptr)->statusPtr = NULL; \
242  ATTATCHSTATUSPTR( statusptr ); \
243  do
244 
245 #define ENDFAIL( statusptr ) \
246  while ( 0 ); \
247  DETATCHSTATUSPTR( statusptr ); \
248  (statusptr)->statusPtr = statusPtrSave_; \
249  SETSTATUS( statusptr, -1, "Recursive error" ); \
250  (void) LALError( statusptr, "ENDFAIL:" ); \
251  (void) LALTrace( statusptr, 1 ); \
252  return; \
253  } \
254 } while ( 0 )
255 
256 #define SETSTATUSFILELINE( statusptr ) \
257  ( ( void ) ( (statusptr)->file = __FILE__, (statusptr)->line = __LINE__ ) )
258 
259 #define SETSTATUS( statusptr, code, mesg ) \
260  ( SETSTATUSFILELINE( statusptr ), \
261  (statusptr)->statusDescription = (mesg), \
262  (statusptr)->statusCode = (code) )
263 
264 
265 #if 0
266 { /* so that editors will match succeeding brace */
267 #elif defined(__cplusplus)
268 }
269 #endif
270 #endif /* _LALSTATUSMACROS_H */
const int lalNoDebug
Definition: LALMisc.c:28