00001 #ifndef _i_iError_h_
00002 #define _i_iError_h_
00003
00004 #ifdef _MSC_VER
00005 # pragma warning( push )
00006 # pragma warning( disable : 4786 ) // 'identifier' : identifier was truncated to 'number' characters in the debug information
00007
00008
00009
00010 #endif
00011 #ifdef __INTEL_COMPILER
00012 # pragma warning( disable : 985 ) // == C4786
00013 #endif^
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00085 #include <cstring>
00086 #include <cstdarg>
00087
00088 #ifndef __FUNCTION__
00089 # define __FUNCTION__ "unknown"
00090 #endif
00091
00092 #ifdef WIN32
00093 # ifdef IERROR_EXPORTS
00094 # define IERROR_API __declspec(dllexport)
00095 # else
00096 # define IERROR_API __declspec(dllimport)
00097 # ifdef _DEBUG
00098 # pragma comment(lib, "iError_d.lib")
00099 # else
00100 # ifdef DEBUG
00101 # pragma comment(lib, "iError_d.lib")
00102 # else
00103 # pragma comment(lib, "iError.lib")
00104 # endif
00105 # endif
00106 # endif
00107 #else
00108 # define IERROR_API
00109 #endif
00110
00111 #define ERR_MAX_PARAM 8
00112 #define ERR_MAX_STACK_DEPTH 32
00113 #define ERR_MAX_DESCRIPTION 1024
00114 #define ERR_MAX_DEBUG_DESCRIPTION 128
00115 #define ERR_MAX_DEBUG_INFO ERR_MAX_STACK_DEPTH*ERR_MAX_DEBUG_DESCRIPTION
00116
00118
00124 #define iLastError iERR::iErrorHandler::Current()->LastError
00125
00130 #define iThrow iERR::iErrorHandler::Current()->Throw
00131
00136 #define iRethrow() iERR::iErrorHandler::Current()->Rethrow(HERE)
00137
00143 #define iReset iERR::iErrorHandler::Current()->Reset
00144
00149 #define iHandleLastError() iERR::iErrorHandler::Current()->Handle(HERE)
00150
00155 #define iAssert(exp) if (!(exp)) iThrow(iError::iASSERTION,__FUNCTION__,__FILE__,__LINE__,&iERR::iERR_ASSERT, #exp)
00156
00161 #define INFO(err) iError::iINFO,__FUNCTION__,__FILE__,__LINE__,&err
00162 #define WARNING(err) iError::iWARNING,__FUNCTION__,__FILE__,__LINE__,&err
00163 #define CRITICAL(err) iError::iCRITICAL,__FUNCTION__,__FILE__,__LINE__,&err
00164 #define EXCEPTION(err) iError::iEXCEPTION,__FUNCTION__,__FILE__,__LINE__,&err
00165
00169 #define HERE __FUNCTION__,__FILE__,__LINE__
00170
00172 namespace iERR {
00173
00174 class IERROR_API iErrorHandler;
00175 class IERROR_API iStdErrorHandler;
00176 class IERROR_API iError;
00177 class IERROR_API iErrorCode;
00178 class IERROR_API CDescriptionParam;
00179
00180
00181 extern IERROR_API iErrorCode UNKNOWN;
00182 extern IERROR_API iErrorCode INTERNAL;
00183 extern IERROR_API iErrorCode GENERIC;
00184 extern IERROR_API iErrorCode SYSTEM;
00185 extern IERROR_API iErrorCode iERR_ASSERT;
00186
00187
00188
00189
00190 class CDescriptionParam
00191 {
00192 public:
00196 union UDescriptionParam
00197 {
00198 int i;
00199 long l;
00200 double d;
00201 void *p;
00202 };
00203
00204 CDescriptionParam() : m_String(0) {};
00205 ~CDescriptionParam() {if (m_String) {delete[] m_String;};};
00206 void SetInt(int v) {m_Value.i=v;};
00207 void SetLong(long v) {m_Value.l=v;};
00208 void SetDouble(double v) {m_Value.d=v;};
00209 void SetVoid(void* v) {m_Value.p=v;};
00210 void SetString(const char* v) {if (m_String) {delete[] m_String;}; if (v) {m_String=new char[strlen(v)+1]; strcpy(m_String, v);} else {m_String=new char[1]; m_String[0]='\0';}};
00211 int GetInt() const {return m_Value.i;};
00212 long GetLong() const {return m_Value.l;};
00213 double GetDouble() const {return m_Value.d;};
00214 void* GetVoid() const {return m_Value.p;};
00215 const char * GetString() const {return m_String;};
00216
00217 protected:
00218 UDescriptionParam m_Value;
00219 char *m_String;
00220 };
00221
00236 class iErrorCode
00237 {
00238 public:
00246 iErrorCode(
00247 const char *description,
00248 int errCode=0
00249 );
00250
00251 ~iErrorCode();
00252
00256 const char * GetDescription() const {return m_Description;};
00257
00261 int GetErrorCode() const {return m_ErrorCode;};
00262
00267 operator long() const {return m_ErrorCode;};
00268
00273 const char *GetSig() const {return m_Sig;};
00274
00275 protected:
00276 char m_Sig[ERR_MAX_PARAM];
00277
00278 private:
00279 const char *m_Description;
00280 int m_ErrorCode;
00281 };
00282
00287 class iError
00288 {
00289 public:
00290 friend class iErrorHandler;
00291
00301 typedef enum {iINFO=0, iWARNING=1, iCRITICAL=2, iEXCEPTION=3, iASSERTION=4} ErrorLevel;
00302
00306 iError();
00307
00311 ErrorLevel GetErrorLevel() const;
00312
00316 iErrorCode* GetErrorCode() const;
00317
00321 const char *GetDescription();
00322
00326 const char *GetDebugInfo();
00327
00332 operator long() const;
00333
00334 protected:
00338 void SetErrorCode(ErrorLevel errLevel, iErrorCode* errCode);
00339
00343 void SetIntegerParam(int idx, int value);
00344
00348 void SetDoubleParam(int idx, double value);
00349
00353 void SetStringParam(int idx, const char* value);
00354
00358 void SetVoidParam(int idx, void* value);
00359
00363 void AddDebugInfo(
00364 const char* functionName,
00365 const char* fileName,
00366 int lineNr
00367 );
00368
00369 private:
00371 ErrorLevel m_ErrorLevel;
00372
00378 iErrorCode* m_ErrorCode;
00379
00381 const char* m_File[ERR_MAX_STACK_DEPTH];
00382
00384 const char* m_Function[ERR_MAX_STACK_DEPTH];
00385
00387 int m_Line[ERR_MAX_STACK_DEPTH];
00388
00390 int m_LastDebugInfo;
00391
00393 char m_Description[ERR_MAX_DESCRIPTION];
00394
00396 char m_DebugInfo[ERR_MAX_DEBUG_INFO];
00397
00399 CDescriptionParam m_Param[ERR_MAX_PARAM];
00400
00401 };
00402
00410 class iErrorHandler
00411 {
00412 public:
00413 friend class iError;
00414
00418 iErrorHandler();
00419
00423 virtual ~iErrorHandler();
00424
00441 virtual int Throw(
00442 iError::ErrorLevel errLevel,
00443 const char *functionName,
00444 const char *file,
00445 int line,
00446 iErrorCode* errCode,
00447 ...
00448 );
00449
00460 virtual bool Rethrow(
00461 const char *functionName=NULL,
00462 const char *file=NULL,
00463 int line=0
00464 );
00465
00476 virtual bool Handle(
00477 const char *functionName=NULL,
00478 const char *file=NULL,
00479 int line=0
00480 );
00481
00487 virtual bool Reset();
00488
00500 virtual iError* LastError(bool reset=false);
00501
00506 iErrorHandler* SetCurrent();
00507
00511 static iErrorHandler* Current();
00512
00513 protected:
00517 virtual void _Throw() = 0;
00518
00522 virtual void _Handle() = 0;
00523
00528 iError* m_Last;
00529
00530 private:
00531
00536 iError* m_LastError;
00537
00538
00540 static int m_HandlerCount;
00541 };
00542
00556 class iStdErrorHandler : public iErrorHandler
00557 {
00558 public:
00560 iStdErrorHandler();
00561
00563 void ShowDebugInfo(bool);
00564
00571 iError::ErrorLevel SetNotificationLevel(iError::ErrorLevel newLevel);
00572
00574 static iStdErrorHandler m_Default;
00575
00576 protected:
00577 virtual void _Throw();
00578 virtual void _Handle();
00579
00580 bool m_ShowDebugInfo;
00581 iError::ErrorLevel m_NotificationLevel;
00582 };
00583
00584 };
00585
00586 #ifdef _MSC_VER
00587 #pragma warning( pop )
00588 #endif
00589
00590 #endif