MOAB: Mesh Oriented datABase  (version 5.2.1)
ErrorHandler.hpp
Go to the documentation of this file.
00001 #ifndef MOAB_ERROR_HANDLER_HPP
00002 #define MOAB_ERROR_HANDLER_HPP
00003 
00004 #ifdef _WIN32
00005 #define __func__ __FUNCTION__
00006 #endif
00007 
00008 #include "moab/Types.hpp"
00009 
00010 #include <sstream>
00011 #include <cstring>
00012 
00013 namespace moab
00014 {
00015 
00016 //! ErrorType - passed to the error handling routines indicating whether this is a new error
00017 //! (globally fatal or per-processor relevant) to be created, or an existing one to be handled
00018 enum ErrorType
00019 {
00020     MB_ERROR_TYPE_NEW_GLOBAL = 0,
00021     MB_ERROR_TYPE_NEW_LOCAL  = 1,
00022     MB_ERROR_TYPE_EXISTING   = 2
00023 };
00024 
00025 //! Initialize MOAB error handler (e.g. create a utility object for printing error output)
00026 void MBErrorHandler_Init();
00027 
00028 //! Finalize MOAB error handler (e.g. delete the utility object for printing error output)
00029 void MBErrorHandler_Finalize();
00030 
00031 //! Indicates whether MBErrorHandler_Init has been called
00032 bool MBErrorHandler_Initialized();
00033 
00034 //! Get information about the last error
00035 void MBErrorHandler_GetLastError( std::string& error );
00036 
00037 //! Routine that is called to create a new error or handle an existing one
00038 ErrorCode MBError( int line, const char* func, const char* file, const char* dir, ErrorCode err_code,
00039                    const char* err_msg, ErrorType err_type );
00040 
00041 }  // namespace moab
00042 
00043 #define __FILENAME__ ( strrchr( __FILE__, '/' ) ? strrchr( __FILE__, '/' ) + 1 : __FILE__ )
00044 
00045 #define MBSTRINGIFY_( X ) #X
00046 #define MBSTRINGIFY( X )  MBSTRINGIFY_( X )
00047 
00048 #ifdef LOCDIR
00049 #define __MBSDIR__ MBSTRINGIFY( LOCDIR )
00050 #else
00051 #define __MBSDIR__ ""
00052 #endif
00053 
00054 //! Set a new error with the given error message (a string or a stream) and return the given error
00055 //! code Used in functions which return ErrorCode
00056 #define MB_SET_ERR( err_code, err_msg )                                                                       \
00057     do                                                                                                        \
00058     {                                                                                                         \
00059         std::ostringstream err_ostr;                                                                          \
00060         err_ostr << err_msg;                                                                                  \
00061         return moab::MBError( __LINE__, __func__, __FILENAME__, __MBSDIR__, err_code, err_ostr.str().c_str(), \
00062                               moab::MB_ERROR_TYPE_NEW_LOCAL );                                                \
00063     } while( false )
00064 
00065 //! Set a new error with the given error message (a string or a stream) and return
00066 //! Used in functions which return void types (or have no return types at all, e.g. constructors)
00067 #define MB_SET_ERR_RET( err_msg )                                                                              \
00068     do                                                                                                         \
00069     {                                                                                                          \
00070         std::ostringstream err_ostr;                                                                           \
00071         err_ostr << err_msg;                                                                                   \
00072         moab::MBError( __LINE__, __func__, __FILENAME__, __MBSDIR__, moab::MB_FAILURE, err_ostr.str().c_str(), \
00073                        moab::MB_ERROR_TYPE_NEW_LOCAL );                                                        \
00074         return;                                                                                                \
00075     } while( false )
00076 
00077 //! Set a new error with the given error message (a string or a stream) and return the given value
00078 //! Used in functions which return any data type
00079 #define MB_SET_ERR_RET_VAL( err_msg, ret_val )                                                                 \
00080     do                                                                                                         \
00081     {                                                                                                          \
00082         std::ostringstream err_ostr;                                                                           \
00083         err_ostr << err_msg;                                                                                   \
00084         moab::MBError( __LINE__, __func__, __FILENAME__, __MBSDIR__, moab::MB_FAILURE, err_ostr.str().c_str(), \
00085                        moab::MB_ERROR_TYPE_NEW_LOCAL );                                                        \
00086         return ret_val;                                                                                        \
00087     } while( false )
00088 
00089 //! Set a new error with the given error message (a string or a stream) and continue
00090 //! Used in functions which return any data type
00091 #define MB_SET_ERR_CONT( err_msg )                                                                             \
00092     do                                                                                                         \
00093     {                                                                                                          \
00094         std::ostringstream err_ostr;                                                                           \
00095         err_ostr << err_msg;                                                                                   \
00096         moab::MBError( __LINE__, __func__, __FILENAME__, __MBSDIR__, moab::MB_FAILURE, err_ostr.str().c_str(), \
00097                        moab::MB_ERROR_TYPE_NEW_LOCAL );                                                        \
00098     } while( false )
00099 
00100 //! Similar to MB_SET_ERR except that the error is considered globally fatal
00101 #define MB_SET_GLB_ERR( err_code, err_msg )                                                                   \
00102     do                                                                                                        \
00103     {                                                                                                         \
00104         std::ostringstream err_ostr;                                                                          \
00105         err_ostr << err_msg;                                                                                  \
00106         return moab::MBError( __LINE__, __func__, __FILENAME__, __MBSDIR__, err_code, err_ostr.str().c_str(), \
00107                               moab::MB_ERROR_TYPE_NEW_GLOBAL );                                               \
00108     } while( false )
00109 
00110 //! Similar to MB_SET_ERR_RET except that the error is considered globally fatal
00111 #define MB_SET_GLB_ERR_RET( err_msg )                                                                          \
00112     do                                                                                                         \
00113     {                                                                                                          \
00114         std::ostringstream err_ostr;                                                                           \
00115         err_ostr << (err_msg);                                                                                   \
00116         moab::MBError( __LINE__, __func__, __FILENAME__, __MBSDIR__, moab::MB_FAILURE, err_ostr.str().c_str(), \
00117                        moab::MB_ERROR_TYPE_NEW_GLOBAL );                                                       \
00118         return;                                                                                                \
00119     } while( false )
00120 
00121 //! Similar to MB_SET_ERR_RET_VAL except that the error is considered globally fatal
00122 #define MB_SET_GLB_ERR_RET_VAL( err_msg, ret_val )                                                             \
00123     do                                                                                                         \
00124     {                                                                                                          \
00125         std::ostringstream err_ostr;                                                                           \
00126         err_ostr << (err_msg);                                                                                   \
00127         moab::MBError( __LINE__, __func__, __FILENAME__, __MBSDIR__, moab::MB_FAILURE, err_ostr.str().c_str(), \
00128                        moab::MB_ERROR_TYPE_NEW_GLOBAL );                                                       \
00129         return ret_val;                                                                                        \
00130     } while( false )
00131 
00132 //! Similar to MB_SET_ERR_CONT except that the error is considered globally fatal
00133 #define MB_SET_GLB_ERR_CONT( err_msg )                                                                         \
00134     do                                                                                                         \
00135     {                                                                                                          \
00136         std::ostringstream err_ostr;                                                                           \
00137         err_ostr << (err_msg);                                                                                   \
00138         moab::MBError( __LINE__, __func__, __FILENAME__, __MBSDIR__, moab::MB_FAILURE, err_ostr.str().c_str(), \
00139                        moab::MB_ERROR_TYPE_NEW_GLOBAL );                                                       \
00140     } while( false )
00141 
00142 //! Check error code, if not MB_SUCCESS, call the error handler and return the given error code
00143 //! Used in functions which return ErrorCode
00144 #define MB_CHK_ERR( err_code )                                                                \
00145     do                                                                                        \
00146     {                                                                                         \
00147         if( moab::MB_SUCCESS != (err_code) )                                                    \
00148             return moab::MBError( __LINE__, __func__, __FILENAME__, __MBSDIR__, err_code, "", \
00149                                   moab::MB_ERROR_TYPE_EXISTING );                             \
00150     } while( false )
00151 
00152 //! Check error code, if not MB_SUCCESS, call the error handler and return
00153 //! Used in functions which return void types (or have no return types at all, e.g. constructors)
00154 #define MB_CHK_ERR_RET( err_code )                                                                                     \
00155     do                                                                                                                 \
00156     {                                                                                                                  \
00157         if( moab::MB_SUCCESS != (err_code) )                                                                             \
00158         {                                                                                                              \
00159             moab::MBError( __LINE__, __func__, __FILENAME__, __MBSDIR__, err_code, "", moab::MB_ERROR_TYPE_EXISTING ); \
00160             return;                                                                                                    \
00161         }                                                                                                              \
00162     } while( false )
00163 
00164 //! Check error code, if not MB_SUCCESS, call the error handler and return the given value
00165 //! Used in functions which return any data type
00166 #define MB_CHK_ERR_RET_VAL( err_code, ret_val )                                                                        \
00167     do                                                                                                                 \
00168     {                                                                                                                  \
00169         if( moab::MB_SUCCESS != (err_code) )                                                                             \
00170         {                                                                                                              \
00171             moab::MBError( __LINE__, __func__, __FILENAME__, __MBSDIR__, err_code, "", moab::MB_ERROR_TYPE_EXISTING ); \
00172             return ret_val;                                                                                            \
00173         }                                                                                                              \
00174     } while( false )
00175 
00176 //! Check error code, if not MB_SUCCESS, call the error handler and continue
00177 //! Used in functions which return any data type
00178 #define MB_CHK_ERR_CONT( err_code )                                                                                    \
00179     do                                                                                                                 \
00180     {                                                                                                                  \
00181         if( moab::MB_SUCCESS != (err_code) )                                                                             \
00182         {                                                                                                              \
00183             moab::MBError( __LINE__, __func__, __FILENAME__, __MBSDIR__, err_code, "", moab::MB_ERROR_TYPE_EXISTING ); \
00184         }                                                                                                              \
00185     } while( false )
00186 
00187 //! Check error code, if not MB_SUCCESS, set a new error with the given error message and return the
00188 //! given error code Used in functions which return ErrorCode
00189 #define MB_CHK_SET_ERR( err_code, err_msg )                                 \
00190     do                                                                      \
00191     {                                                                       \
00192         if( moab::MB_SUCCESS != (err_code) ) MB_SET_ERR( err_code, err_msg ); \
00193     } while( false )
00194 
00195 //! Check error code, if not MB_SUCCESS, set a new error with the given error message and return
00196 //! Used in functions which return void types (or have no return types at all, e.g. constructors)
00197 #define MB_CHK_SET_ERR_RET( err_code, err_msg )                       \
00198     do                                                                \
00199     {                                                                 \
00200         if( moab::MB_SUCCESS != (err_code) ) MB_SET_ERR_RET( err_msg ); \
00201     } while( false )
00202 
00203 //! Check error code, if not MB_SUCCESS, set a new error with the given error message and return the
00204 //! given value Used in functions which return any data type
00205 #define MB_CHK_SET_ERR_RET_VAL( err_code, err_msg, ret_val )                       \
00206     do                                                                             \
00207     {                                                                              \
00208         if( moab::MB_SUCCESS != (err_code) ) MB_SET_ERR_RET_VAL( err_msg, ret_val ); \
00209     } while( false )
00210 
00211 //! Check error code, if not MB_SUCCESS, set a new error with the given error message and continue
00212 //! Used in functions which return any data type
00213 #define MB_CHK_SET_ERR_CONT( err_code, err_msg )                       \
00214     do                                                                 \
00215     {                                                                  \
00216         if( moab::MB_SUCCESS != (err_code) ) MB_SET_ERR_CONT( err_msg ); \
00217     } while( false )
00218 
00219 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines