SNAP Library 2.2, Developer Reference  2014-03-11 19:15:55
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
bd.cpp
Go to the documentation of this file.
00001 #if SW_TRACE
00002 #include <execinfo.h>
00003 #endif
00004 
00006 // Mathmatical-Errors
00007 #if defined(__BCPLUSPLUS__) && (__BCPLUSPLUS__==0x0530)
00008 int std::_matherr(struct math_exception* e){
00009   e->retval=0;
00010   return 1;
00011 }
00012 #elif defined(GLib_GLIBC) || defined(GLib_BSD)
00013 int _matherr(struct __exception* e){
00014   e->retval=0;
00015   return 1;
00016 }
00017 #elif defined(GLib_SOLARIS)
00018 int _matherr(struct __math_exception* e){
00019   e->retval=0;
00020   return 1;
00021 }
00022 #elif defined(GLib_CYGWIN)
00023 int matherr(struct __exception *e){
00024   e->retval=0;
00025   return 1;
00026 }
00027 #elif defined(GLib_MACOSX)
00028 //int matherr(struct exception *e) {
00029 //  e->retval=0;
00030 //  return 1;
00031 //}
00032 #else
00033 int _matherr(struct _exception* e){
00034   e->retval=0;
00035   return 1;
00036 }
00037 #endif
00038 
00040 // Messages
00041 void WrNotify(const char* CaptionCStr, const char* NotifyCStr){
00042 #if defined(__CONSOLE__) || defined(_CONSOLE)
00043   printf("*** %s: %s\n", CaptionCStr, NotifyCStr);
00044 #else
00045   MessageBox(NULL, NotifyCStr, CaptionCStr, MB_OK);
00046 #endif
00047 }
00048 
00049 void SaveToErrLog(const char* MsgCStr){
00050   int MxFNmLen=1000;
00051   char* FNm=new char[MxFNmLen]; if (FNm==NULL){return;}
00052   int FNmLen=GetModuleFileName(NULL, FNm, MxFNmLen); if (FNmLen==0){return;}
00053   FNm[FNmLen++]='.'; FNm[FNmLen++]='E'; FNm[FNmLen++]='r'; FNm[FNmLen++]='r';
00054   FNm[FNmLen++]=char(0);
00055   time_t Time=time(NULL);
00056   FILE* fOut=fopen(FNm, "a+b"); if (fOut==NULL){return;}
00057   fprintf(fOut, "--------\r\n%s\r\n%s%s\r\n--------\r\n",
00058    FNm, ctime(&Time), MsgCStr);
00059   fclose(fOut);
00060   delete[] FNm;
00061 }
00062 
00063 #if SW_TRACE
00064 void PrintBacktrace() {
00065   // stack dump, works for g++
00066   void *array[20];
00067   size_t size;
00068 
00069   // flush stdout
00070   fflush(0);
00071 
00072   // get the trace and print it to stdout
00073   size = backtrace(array, 20);
00074   backtrace_symbols_fd(array, size, 1);
00075 }
00076 
00077 void Crash() {
00078   char *p;
00079   p = (char *) 0;
00080   *p = 1234;
00081 }
00082 #endif
00083 
00085 // Assertions
00086 TOnExeStop::TOnExeStopF TOnExeStop::OnExeStopF=NULL;
00087 
00088 void ExeStop(
00089  const char* MsgCStr, const char* ReasonCStr,
00090  const char* CondCStr, const char* FNm, const int& LnN){
00091   char ReasonMsgCStr[1000];
00092 
00093 #if SW_TRACE
00094   PrintBacktrace();
00095   Crash();
00096 #endif
00097 
00098   // construct reason message
00099   if (ReasonCStr==NULL){ReasonMsgCStr[0]=0;}
00100   else {sprintf(ReasonMsgCStr, " [Reason:'%s']", ReasonCStr);}
00101   // construct full message
00102   char FullMsgCStr[1000];
00103   if (MsgCStr==NULL){
00104     if (CondCStr==NULL){
00105       sprintf(FullMsgCStr, "Execution stopped%s!", ReasonMsgCStr);
00106     } else {
00107       sprintf(FullMsgCStr, "Execution stopped: %s%s, file %s, line %d",
00108        CondCStr, ReasonMsgCStr, FNm, LnN);
00109     }
00110   } else {
00111     if (CondCStr==NULL){
00112       sprintf(FullMsgCStr, "%s\nExecution stopped!", MsgCStr);
00113     } else {
00114       sprintf(FullMsgCStr, "Message: %s%s\nExecution stopped: %s, file %s, line %d",
00115        MsgCStr, ReasonMsgCStr, CondCStr, FNm, LnN);
00116     }
00117   }
00118   // report full message to log file
00119   SaveToErrLog(FullMsgCStr);
00120 
00121 #if defined(SW_NOABORT)
00122   TExcept::Throw(FullMsgCStr);
00123 #endif
00124 
00125   // report to screen & stop execution
00126   bool Continue=false;
00127   // call handler
00128   if (TOnExeStop::IsOnExeStopF()){
00129     Continue=!((*TOnExeStop::GetOnExeStopF())(FullMsgCStr));}
00130   if (!Continue){
00131     ErrNotify(FullMsgCStr);
00132 #ifdef GLib_WIN32
00133     abort();
00134     //ExitProcess(1);
00135 #else
00136     exit(1);
00137 #endif
00138   }
00139 }