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