00001 #ifndef __ERROR_H__
00002 #define __ERROR_H__
00003
00004
00005
00006
00007
00008
00009
00010 #include <errno.h>
00011 #include <stdarg.h>
00012
00013 #include "Object.h"
00014 #include "Limits.h"
00015 #include "Messages.h"
00016 #include "SignalDeclaration.h"
00017 #include "SingletonDeclaration.h"
00018 #include "String.h"
00019
00020
00021 static const int OK = 0;
00022 static const int FAILURE = -1;
00023
00024 #define ERROR(...) ERR()->error(__FILE__, __LINE__, __PRETTY_FUNCTION__, ##__VA_ARGS__)
00025 #define WARNING(...) ERR()->warning(__FILE__, __LINE__, __PRETTY_FUNCTION__, ##__VA_ARGS__)
00026 #define ERROR_FATAL(...) ERR()->errorFatal(__FILE__, __LINE__, __PRETTY_FUNCTION__, ##__VA_ARGS__)
00027 #define INFO(...) ERR()->info(__FILE__, __LINE__, __PRETTY_FUNCTION__, ##__VA_ARGS__)
00028
00029 #ifdef DEBUG_MODE
00030 #define ERROR_BACKTRACE(code) ERR()->errorBackTrace(__FILE__, __LINE__, __PRETTY_FUNCTION__, code)
00031 #define DEBUG(...) ERR()->debug(__FILE__, __LINE__, __PRETTY_FUNCTION__, ##__VA_ARGS__)
00032 #define MARKER ERR()->debug(__FILE__, __LINE__, __PRETTY_FUNCTION__, "MARKER")
00033 #else
00034
00035 inline
00036 int ERROR_BACKTRACE(const int code)
00037 {
00038 return code;
00039 }
00040
00041 #define DEBUG(...)
00042 #define MARKER
00043 #endif
00044
00045 #define BACKTRACE Error::printBackTrace()
00046
00047 #define CHECK_POINTER(ptr) \
00048 if (!(ptr)) \
00049 return ERROR(MSG_NULL_POINTER);
00050
00051 #define CHECK_PARENT \
00052 { \
00053 int res; \
00054 if (FAILED(res = failureCode())) { \
00055 failure(ERROR_BACKTRACE(res)); \
00056 return; \
00057 } \
00058 }
00059
00060 #define CHECK_CREATION(ptr, className) \
00061 { \
00062 CHECK_POINTER(ptr); \
00063 int res; \
00064 if (FAILED(res = ptr->failureCode())) { \
00065 delete ptr; \
00066 return ERROR_BACKTRACE(res); \
00067 } \
00068 }
00069
00070 using namespace std;
00071
00072 inline bool SUCCEEDED(const int res)
00073 {
00074 return (res == OK);
00075 }
00076
00077 inline bool FAILED(const int res)
00078 {
00079 return (res != OK);
00080 }
00081
00082 class Error : public Object,
00083 public Singleton<Error>
00084 {
00085
00086 friend class Singleton<Error>;
00087
00088 PYLON_OBJECT
00089
00090 public:
00091
00092 #ifdef DEBUG_MODE
00093 int debug(const String &fileName, const unsigned int lineNum, const String &functionName, const char *format, ...) __attribute__ ((format (printf, 5, 6)));
00094 #endif
00095
00096 int verror(const String &fileName, const unsigned int lineNum, const String &functionName, const char *format, va_list ap);
00097 int error(const String &fileName, const unsigned int lineNum, const String &functionName, const char *format, ...) __attribute__ ((format (printf, 5, 6)));
00098 int errorFatal(const String &fileName, const unsigned int lineNum, const String &functionName, const char *format, ...) __attribute__ ((format (printf, 5, 6)));
00099
00100 #ifdef DEBUG_MODE
00101 int errorBackTrace(const String &fileName, const unsigned int lineNum, const String &functionName, const int code);
00102 #endif
00103
00104 int warning(const String &fileName, const unsigned int lineNum, const String &functionName, const char *format, ...) __attribute__ ((format (printf, 5, 6)));
00105
00106 int info(const String &fileName, const unsigned int lineNum, const String &functionName, const char *format, ...) __attribute__ ((format (printf, 5, 6)));
00107
00108 static void printBackTrace();
00109
00110 SIGNAL(debug, const String &, const String &, const unsigned int, const String &, const String &);
00111 SIGNAL(error, const String &, const String &, const unsigned int, const String &, const String &);
00112 SIGNAL(errorFatal, const String &, const String &, const unsigned int, const String &, const String &);
00113 SIGNAL(warning, const String &, const String &, const unsigned int, const String &, const String &);
00114 SIGNAL(info, const String &, const String &, const unsigned int, const String &, const String &);
00115
00116 private:
00117
00118 Error();
00119 virtual ~Error();
00120 };
00121
00122 inline
00123 Error *ERR()
00124 {
00125 return Error::instance();
00126 }
00127
00128 inline
00129 String COLOR_DEBUG()
00130 {
00131 return COLOR_STRING(" DEBUG ", YELLOW, BLUE);
00132 }
00133
00134 inline
00135 String COLOR_ERROR()
00136 {
00137 return COLOR_STRING(" ERROR ", YELLOW, RED);
00138 }
00139
00140 inline
00141 String COLOR_ERROR_FATAL()
00142 {
00143 return COLOR_STRING(" FATAL ERROR ", YELLOW, RED);
00144 }
00145
00146 inline
00147 String COLOR_WARNING()
00148 {
00149 return COLOR_STRING(" WARNING ", RED, WHITE);
00150 }
00151
00152 inline
00153 String COLOR_INFO()
00154 {
00155 return COLOR_STRING(" INFO ", BLACK, CYAN);
00156 }
00157
00158 inline
00159 String COLOR_DEFERRED()
00160 {
00161 return COLOR_STRING(" DEFERRED ", BLACK, GREEN);
00162 }
00163
00164 inline
00165 String COLOR_BACKTRACE()
00166 {
00167 return COLOR_STRING(" BACKTRACE ", YELLOW, RED);
00168 }
00169
00170 #endif
00171