00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00031
00032 #ifdef HAVE_CONFIG_H
00033 #include "autoconfig.h"
00034 #endif
00035
00036 #include "exception.h"
00037
00038 #ifdef HAVE_EXECINFO_H
00039 #include <execinfo.h>
00040 #endif
00041
00042 using namespace zmm;
00043
00044 #define STRACE_TAG "_STRACE_"
00045
00046 Exception::Exception(String message, const char* file, int line, const char* function)
00047 {
00048 this->message = message;
00049 this->file = file;
00050 this->function = function;
00051 this->line = line;
00052 #if defined HAVE_BACKTRACE && defined HAVE_BACKTRACE_SYMBOLS
00053 void *b[100];
00054 int size = backtrace(b, 100);
00055
00056 stackTrace = Ref<Array<StringBase> >(new Array<StringBase>(size));
00057
00058 char **s = backtrace_symbols(b, size);
00059 for(int i = 0; i < size; i++)
00060 {
00061 Ref<StringBase> trace(new StringBase(s[i]));
00062 stackTrace->append(trace);
00063 }
00064 if (s)
00065 free(s);
00066 #endif
00067 }
00068
00069 Exception::Exception(String message)
00070 {
00071 this->message = message;
00072 this->file = nil;
00073 this->function = nil;
00074 this->line = -1;
00075 #if defined HAVE_BACKTRACE && defined HAVE_BACKTRACE_SYMBOLS
00076 void *b[100];
00077 int size = backtrace(b, 100);
00078
00079 stackTrace = Ref<Array<StringBase> >(new Array<StringBase>(size));
00080
00081 char **s = backtrace_symbols(b, size);
00082 for(int i = 0; i < size; i++)
00083 {
00084 Ref<StringBase> trace(new StringBase(s[i]));
00085 stackTrace->append(trace);
00086 }
00087 free(s);
00088 #endif
00089 }
00090
00091 String Exception::getMessage()
00092 {
00093 return message;
00094 }
00095
00096 Ref<Array<StringBase> > Exception::getStackTrace()
00097 {
00098 return stackTrace;
00099 }
00100
00101 #ifdef TOMBDEBUG
00102 void Exception::printStackTrace(FILE *file)
00103 {
00104 if (line >= 0)
00105 {
00106 fprintf(file, "Exception raised in [%s:%d] %s(): %s\n",
00107 this->file.c_str(), line, function.c_str(), message.c_str());
00108 }
00109 else
00110 {
00111 fprintf(file, "Exception: %s\n", message.c_str());
00112 }
00113 #if defined HAVE_BACKTRACE && defined HAVE_BACKTRACE_SYMBOLS
00114 for (int i = 0; i < stackTrace->size(); i++)
00115 {
00116 Ref<StringBase> trace = stackTrace->get(i);
00117 fprintf(file, "%s %i %s\n", STRACE_TAG, i, trace->data);
00118 fflush(file);
00119 }
00120 #endif // __CYGWIN__
00121 }
00122 #endif // TOMBDEBUG