Error.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 $Header$
00004 
00005 */
00006 
00007 #include <execinfo.h>
00008 #include <stdarg.h>
00009 #include <unistd.h>
00010 #include <sys/types.h>
00011 
00012 #include "Syslog.h"
00013 #include "Forth.h"
00014 
00015 #include "Error.h"
00016 
00017 #define GET_MESSAGE(lastArg) \
00018   char msg[LIMIT_CSTRING_SIZE]; \
00019   va_list ap; \
00020   va_start(ap, lastArg); \
00021   vsnprintf(msg, sizeof(msg), format, ap); \
00022   va_end(ap);
00023 
00024 // Public methods.
00025 
00026 #ifdef DEBUG_MODE
00027 int Error::debug(const String &fileName, const unsigned int lineNum, const String &functionName, const char *format, ...)
00028 {
00029   String ts(String::timestamp());
00030 
00031   GET_MESSAGE(format);
00032 
00033   fprintf(stderr, "%s [%s] [file: '%s', line: %u, function: '%s']: %s\n",
00034           CSTRING(COLOR_DEBUG()), CSTRING(ts), CSTRING(fileName), lineNum, CSTRING(functionName), msg);
00035 
00036   EMIT(debug, ts, fileName, lineNum, functionName, msg);
00037 
00038   return OK;
00039 }
00040 #endif
00041 
00042 int Error::verror(const String &fileName, const unsigned int lineNum, const String &functionName, const char *format, va_list ap)
00043 {
00044   char msg[LIMIT_CSTRING_SIZE];
00045   String ts(String::timestamp());
00046 
00047   vsnprintf(msg, sizeof(msg), format, ap);
00048 
00049 #ifdef DEBUG_MODE
00050   fprintf(stderr, "%s [%s] [file: '%s', line: %u, function: '%s']: %s\n",
00051           CSTRING(COLOR_ERROR()), CSTRING(ts), CSTRING(fileName), lineNum, CSTRING(functionName), msg);
00052 #endif
00053 
00054   EMIT(error, ts, fileName, lineNum, functionName, msg);
00055 
00056   return FAILURE;
00057 }
00058 
00059 int Error::error(const String &fileName, const unsigned int lineNum, const String &functionName, const char *format, ...)
00060 {
00061   va_list ap;
00062   va_start(ap, format);
00063   verror(fileName, lineNum, functionName, format, ap);
00064   va_end(ap);
00065 
00066   return FAILURE;
00067 }
00068 
00069 int Error::errorFatal(const String &fileName, const unsigned int lineNum, const String &functionName, const char *format, ...)
00070 {
00071   String ts(String::timestamp());
00072 
00073   GET_MESSAGE(format);
00074 
00075   fprintf(stderr, "%s [%s] [file: '%s', line: %u, function: '%s']: %s\n",
00076           CSTRING(COLOR_ERROR_FATAL()), CSTRING(ts), CSTRING(fileName), lineNum, CSTRING(functionName), msg);
00077 
00078   EMIT(error, ts, fileName, lineNum, functionName, msg);
00079 
00080   printBackTrace();
00081   exit(-1);
00082 
00083   return FAILURE;
00084 }
00085 
00086 #ifdef DEBUG_MODE
00087 int Error::errorBackTrace(const String &fileName, const unsigned int lineNum, const String &functionName, const int code)
00088 {
00089   String ts(String::timestamp());
00090 
00091   if (code == OK)
00092     return code;
00093 
00094   fprintf(stderr, "%s [%s] [file: '%s', line: %u, function: '%s']: %s\n",
00095           CSTRING(COLOR_ERROR()), CSTRING(ts), CSTRING(fileName), lineNum, CSTRING(functionName), "ERROR BACKTRACE");
00096 
00097   EMIT(error, ts, fileName, lineNum, functionName, "ERROR BACKTRACE.");
00098 
00099   return code;
00100 }
00101 #endif
00102 
00103 int Error::warning(const String &fileName, const unsigned int lineNum, const String &functionName, const char *format, ...)
00104 {
00105   String ts(String::timestamp());
00106 
00107   GET_MESSAGE(format);
00108 
00109 #ifdef DEBUG_MODE
00110   fprintf(stderr, "%s [%s] [file: '%s', line: %u, function: '%s']: %s\n",
00111           CSTRING(COLOR_WARNING()), CSTRING(ts), CSTRING(fileName), lineNum, CSTRING(functionName), msg);
00112 #endif
00113 
00114   EMIT(warning, ts, fileName, lineNum, functionName, msg);
00115 
00116   return OK;
00117 }
00118 
00119 int Error::info(const String &fileName, const unsigned int lineNum, const String &functionName, const char *format, ...)
00120 {
00121   String ts(String::timestamp());
00122 
00123   GET_MESSAGE(format);
00124 
00125 #ifdef DEBUG_MODE
00126   fprintf(stderr, "%s [%s] [file: '%s', line: %u, function: '%s']: %s\n",
00127           CSTRING(COLOR_INFO()), CSTRING(ts), CSTRING(fileName), lineNum, CSTRING(functionName), msg);
00128 #endif
00129 
00130   EMIT(info, ts, fileName, lineNum, functionName, msg);
00131 
00132   return OK;
00133 }
00134 
00135 void Error::printBackTrace()
00136 {
00137   void *trace[LIMIT_BACKTRACE_DEPTH];
00138   int traceSize = backtrace(trace, LIMIT_BACKTRACE_DEPTH);
00139   char **messages = backtrace_symbols(trace, traceSize);
00140 
00141   printf("%s Obtained %zd stack frames.\n", CSTRING(COLOR_BACKTRACE()), traceSize);
00142   for (int i = 0; i < traceSize; ++i)
00143     printf("%s %s\n", CSTRING(COLOR_BACKTRACE()), messages[i]);
00144   free(messages);
00145 }
00146 
00147 // Private methods.
00148 
00149 Error::Error() : Object(), Singleton<Error>()
00150 {
00151 }
00152 
00153 Error::~Error()
00154 {
00155 }

Generated on Thu Sep 6 20:11:25 2007 for Pylon Application Platform by  doxygen 1.5.1