1 #ifndef MOAB_ERROR_HANDLER_HPP
2 #define MOAB_ERROR_HANDLER_HPP
3
4 #ifdef _WIN32
5 #define __func__ __FUNCTION__
6 #endif
7
8 #include "moab/Types.hpp"
9
10 #include <sstream>
11 #include <cstring>
12
13 namespace moab
14 {
15
16 //! ErrorType - passed to the error handling routines indicating whether this is a new error
17 //! (globally fatal or per-processor relevant) to be created, or an existing one to be handled
18 enum ErrorType
19 {
20 MB_ERROR_TYPE_NEW_GLOBAL = 0,
21 MB_ERROR_TYPE_NEW_LOCAL = 1,
22 MB_ERROR_TYPE_EXISTING = 2
23 };
24
25 //! Initialize MOAB error handler (e.g. create a utility object for printing error output)
26 void MBErrorHandler_Init();
27
28 //! Finalize MOAB error handler (e.g. delete the utility object for printing error output)
29 void MBErrorHandler_Finalize();
30
31 //! Indicates whether MBErrorHandler_Init has been called
32 bool MBErrorHandler_Initialized();
33
34 //! Get information about the last error
35 void MBErrorHandler_GetLastError( std::string& error );
36
37 //! Routine that is called to create a new error or handle an existing one
38 ErrorCode MBError( int line,
39 const char* func,
40 const char* file,
41 const char* dir,
42 ErrorCode err_code,
43 const char* err_msg,
44 ErrorType err_type );
45
46 } // namespace moab
47
48 #define __FILENAME__ ( strrchr( __FILE__, '/' ) ? strrchr( __FILE__, '/' ) + 1 : __FILE__ )
49
50 #define MBSTRINGIFY_( X ) #X
51 #define MBSTRINGIFY( X ) MBSTRINGIFY_( X )
52
53 #ifdef LOCDIR
54 #define __MBSDIR__ MBSTRINGIFY( LOCDIR )
55 #else
56 #define __MBSDIR__ ""
57 #endif
58
59 //! Set a new error with the given error message (a string or a stream) and return the given error
60 //! code Used in functions which return ErrorCode
61 #define MB_SET_ERR( err_code, err_msg ) \
62 do \
63 { \
64 std::ostringstream err_ostr; \
65 err_ostr << err_msg; \
66 return moab::MBError( __LINE__, __func__, __FILENAME__, __MBSDIR__, err_code, err_ostr.str().c_str(), \
67 moab::MB_ERROR_TYPE_NEW_LOCAL ); \
68 } while( false )
69
70 //! Set a new error with the given error message (a string or a stream) and return
71 //! Used in functions which return void types (or have no return types at all, e.g. constructors)
72 #define MB_SET_ERR_RET( err_msg ) \
73 do \
74 { \
75 std::ostringstream err_ostr; \
76 err_ostr << err_msg; \
77 moab::MBError( __LINE__, __func__, __FILENAME__, __MBSDIR__, moab::MB_FAILURE, err_ostr.str().c_str(), \
78 moab::MB_ERROR_TYPE_NEW_LOCAL ); \
79 return; \
80 } while( false )
81
82 //! Set a new error with the given error message (a string or a stream) and return the given value
83 //! Used in functions which return any data type
84 #define MB_SET_ERR_RET_VAL( err_msg, ret_val ) \
85 do \
86 { \
87 std::ostringstream err_ostr; \
88 err_ostr << err_msg; \
89 moab::MBError( __LINE__, __func__, __FILENAME__, __MBSDIR__, moab::MB_FAILURE, err_ostr.str().c_str(), \
90 moab::MB_ERROR_TYPE_NEW_LOCAL ); \
91 return ret_val; \
92 } while( false )
93
94 //! Set a new error with the given error message (a string or a stream) and continue
95 //! Used in functions which return any data type
96 #define MB_SET_ERR_CONT( err_msg ) \
97 do \
98 { \
99 std::ostringstream err_ostr; \
100 err_ostr << err_msg; \
101 moab::MBError( __LINE__, __func__, __FILENAME__, __MBSDIR__, moab::MB_FAILURE, err_ostr.str().c_str(), \
102 moab::MB_ERROR_TYPE_NEW_LOCAL ); \
103 } while( false )
104
105 //! Similar to MB_SET_ERR except that the error is considered globally fatal
106 #define MB_SET_GLB_ERR( err_code, err_msg ) \
107 do \
108 { \
109 std::ostringstream err_ostr; \
110 err_ostr << err_msg; \
111 return moab::MBError( __LINE__, __func__, __FILENAME__, __MBSDIR__, err_code, err_ostr.str().c_str(), \
112 moab::MB_ERROR_TYPE_NEW_GLOBAL ); \
113 } while( false )
114
115 //! Similar to MB_SET_ERR_RET except that the error is considered globally fatal
116 #define MB_SET_GLB_ERR_RET( err_msg ) \
117 do \
118 { \
119 std::ostringstream err_ostr; \
120 err_ostr << ( err_msg ); \
121 moab::MBError( __LINE__, __func__, __FILENAME__, __MBSDIR__, moab::MB_FAILURE, err_ostr.str().c_str(), \
122 moab::MB_ERROR_TYPE_NEW_GLOBAL ); \
123 return; \
124 } while( false )
125
126 //! Similar to MB_SET_ERR_RET_VAL except that the error is considered globally fatal
127 #define MB_SET_GLB_ERR_RET_VAL( err_msg, ret_val ) \
128 do \
129 { \
130 std::ostringstream err_ostr; \
131 err_ostr << ( err_msg ); \
132 moab::MBError( __LINE__, __func__, __FILENAME__, __MBSDIR__, moab::MB_FAILURE, err_ostr.str().c_str(), \
133 moab::MB_ERROR_TYPE_NEW_GLOBAL ); \
134 return ret_val; \
135 } while( false )
136
137 //! Similar to MB_SET_ERR_CONT except that the error is considered globally fatal
138 #define MB_SET_GLB_ERR_CONT( err_msg ) \
139 do \
140 { \
141 std::ostringstream err_ostr; \
142 err_ostr << ( err_msg ); \
143 moab::MBError( __LINE__, __func__, __FILENAME__, __MBSDIR__, moab::MB_FAILURE, err_ostr.str().c_str(), \
144 moab::MB_ERROR_TYPE_NEW_GLOBAL ); \
145 } while( false )
146
147 //! Check error code, if not MB_SUCCESS, call the error handler and return the given error code
148 //! Used in functions which return ErrorCode
149 #define MB_CHK_ERR( err_code ) \
150 do \
151 { \
152 if( moab::MB_SUCCESS != ( err_code ) ) \
153 return moab::MBError( __LINE__, __func__, __FILENAME__, __MBSDIR__, err_code, "", \
154 moab::MB_ERROR_TYPE_EXISTING ); \
155 } while( false )
156
157 //! Check error code, if not MB_SUCCESS, call the error handler and return
158 //! Used in functions which return void types (or have no return types at all, e.g. constructors)
159 #define MB_CHK_ERR_RET( err_code ) \
160 do \
161 { \
162 if( moab::MB_SUCCESS != ( err_code ) ) \
163 { \
164 moab::MBError( __LINE__, __func__, __FILENAME__, __MBSDIR__, err_code, "", moab::MB_ERROR_TYPE_EXISTING ); \
165 return; \
166 } \
167 } while( false )
168
169 //! Check error code, if not MB_SUCCESS, call the error handler and return the given value
170 //! Used in functions which return any data type
171 #define MB_CHK_ERR_RET_VAL( err_code, ret_val ) \
172 do \
173 { \
174 if( moab::MB_SUCCESS != ( err_code ) ) \
175 { \
176 moab::MBError( __LINE__, __func__, __FILENAME__, __MBSDIR__, err_code, "", moab::MB_ERROR_TYPE_EXISTING ); \
177 return ret_val; \
178 } \
179 } while( false )
180
181 //! Check error code, if not MB_SUCCESS, call the error handler and continue
182 //! Used in functions which return any data type
183 #define MB_CHK_ERR_CONT( err_code ) \
184 do \
185 { \
186 if( moab::MB_SUCCESS != ( err_code ) ) \
187 { \
188 moab::MBError( __LINE__, __func__, __FILENAME__, __MBSDIR__, err_code, "", moab::MB_ERROR_TYPE_EXISTING ); \
189 } \
190 } while( false )
191
192 //! Check error code, if not MB_SUCCESS, set a new error with the given error message and return the
193 //! given error code Used in functions which return ErrorCode
194 #define MB_CHK_SET_ERR( err_code, err_msg ) \
195 do \
196 { \
197 if( moab::MB_SUCCESS != ( err_code ) ) MB_SET_ERR( err_code, err_msg ); \
198 } while( false )
199
200 //! Check error code, if not MB_SUCCESS, set a new error with the given error message and return
201 //! Used in functions which return void types (or have no return types at all, e.g. constructors)
202 #define MB_CHK_SET_ERR_RET( err_code, err_msg ) \
203 do \
204 { \
205 if( moab::MB_SUCCESS != ( err_code ) ) MB_SET_ERR_RET( err_msg ); \
206 } while( false )
207
208 //! Check error code, if not MB_SUCCESS, set a new error with the given error message and return the
209 //! given value Used in functions which return any data type
210 #define MB_CHK_SET_ERR_RET_VAL( err_code, err_msg, ret_val ) \
211 do \
212 { \
213 if( moab::MB_SUCCESS != ( err_code ) ) MB_SET_ERR_RET_VAL( err_msg, ret_val ); \
214 } while( false )
215
216 //! Check error code, if not MB_SUCCESS, set a new error with the given error message and continue
217 //! Used in functions which return any data type
218 #define MB_CHK_SET_ERR_CONT( err_code, err_msg ) \
219 do \
220 { \
221 if( moab::MB_SUCCESS != ( err_code ) ) MB_SET_ERR_CONT( err_msg ); \
222 } while( false )
223
224 #endif