SNAP Library 4.0, Developer Reference  2017-07-27 13:18:06
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
bd.cpp
Go to the documentation of this file.
1 #if SW_TRACE
2 #include <execinfo.h>
3 #endif
4 
6 // Mathmatical-Errors
7 #if defined(__BCPLUSPLUS__) && (__BCPLUSPLUS__==0x0530)
8 int std::_matherr(struct math_exception* e){
9  e->retval=0;
10  return 1;
11 }
12 #elif defined(GLib_GLIBC) || defined(GLib_BSD)
13 int _matherr(struct __exception* e){
14  e->retval=0;
15  return 1;
16 }
17 #elif defined(GLib_SOLARIS)
18 int _matherr(struct __math_exception* e){
19  e->retval=0;
20  return 1;
21 }
22 #elif defined(GLib_CYGWIN)
23 int matherr(struct __exception *e){
24  e->retval=0;
25  return 1;
26 }
27 #elif defined(GLib_MACOSX)
28 //int matherr(struct exception *e) {
29 // e->retval=0;
30 // return 1;
31 //}
32 #else
33 int _matherr(struct _exception* e){
34  e->retval=0;
35  return 1;
36 }
37 #endif
38 
40 // Messages
41 void WrNotify(const char* CaptionCStr, const char* NotifyCStr){
42 #if defined(__CONSOLE__) || defined(_CONSOLE)
43  printf("*** %s: %s\n", CaptionCStr, NotifyCStr);
44 #else
45  MessageBox(NULL, NotifyCStr, CaptionCStr, MB_OK);
46 #endif
47 }
48 
49 void SaveToErrLog(const char* MsgCStr){
50  int MxFNmLen=1000;
51  char* FNm=new char[MxFNmLen]; if (FNm==NULL){return;}
52  int FNmLen=GetModuleFileName(NULL, FNm, MxFNmLen); if (FNmLen==0){return;}
53  FNm[FNmLen++]='.'; FNm[FNmLen++]='E'; FNm[FNmLen++]='r'; FNm[FNmLen++]='r';
54  FNm[FNmLen++]=char(0);
55  time_t Time=time(NULL);
56  FILE* fOut=fopen(FNm, "a+b"); if (fOut==NULL){return;}
57  fprintf(fOut, "--------\r\n%s\r\n%s%s\r\n--------\r\n",
58  FNm, ctime(&Time), MsgCStr);
59  fclose(fOut);
60  delete[] FNm;
61 }
62 
63 #if SW_TRACE
64 void PrintBacktrace() {
65  // stack dump, works for g++
66  void *array[20];
67  size_t size;
68 
69  // flush stdout
70  fflush(0);
71 
72  // get the trace and print it to stdout
73  size = backtrace(array, 20);
74  backtrace_symbols_fd(array, size, 1);
75 }
76 
77 void Crash() {
78  int *p;
79  p = (int *) 0;
80  *p = 1234;
81 }
82 #endif
83 
85 // Assertions
87 
88 void ExeStop(
89  const char* MsgCStr, const char* ReasonCStr,
90  const char* CondCStr, const char* FNm, const int& LnN){
91  char ReasonMsgCStr[1000];
92 
93 #if SW_TRACE
94  PrintBacktrace();
95  Crash();
96 #endif
97 
98  // construct reason message
99  if (ReasonCStr==NULL){ReasonMsgCStr[0]=0;}
100  else {sprintf(ReasonMsgCStr, " [Reason:'%s']", ReasonCStr);}
101  // construct full message
102  char FullMsgCStr[1000];
103  if (MsgCStr==NULL){
104  if (CondCStr==NULL){
105  sprintf(FullMsgCStr, "Execution stopped%s!", ReasonMsgCStr);
106  } else {
107  sprintf(FullMsgCStr, "Execution stopped: %s%s, file %s, line %d",
108  CondCStr, ReasonMsgCStr, FNm, LnN);
109  }
110  } else {
111  if (CondCStr==NULL){
112  sprintf(FullMsgCStr, "%s\nExecution stopped!", MsgCStr);
113  } else {
114  sprintf(FullMsgCStr, "Message: %s%s\nExecution stopped: %s, file %s, line %d",
115  MsgCStr, ReasonMsgCStr, CondCStr, FNm, LnN);
116  }
117  }
118  // report full message to log file
119  SaveToErrLog(FullMsgCStr);
120 
121 #if defined(SW_NOABORT)
122  TExcept::Throw(FullMsgCStr);
123 #endif
124 
125  // report to screen & stop execution
126  bool Continue=false;
127  // call handler
129  Continue=!((*TOnExeStop::GetOnExeStopF())(FullMsgCStr));}
130  if (!Continue){
131  ErrNotify(FullMsgCStr);
132 #ifdef GLib_WIN32
133  abort();
134  //ExitProcess(1);
135 #else
136  exit(1);
137 #endif
138  }
139 }
void ExeStop(const char *MsgCStr, const char *ReasonCStr, const char *CondCStr, const char *FNm, const int &LnN)
Definition: bd.cpp:88
void ErrNotify(const char *NotifyCStr)
Definition: bd.h:74
int _matherr(struct _exception *e)
Definition: bd.cpp:33
static void Throw(const TStr &MsgStr)
Definition: ut.h:187
void SaveToErrLog(const char *MsgCStr)
Definition: bd.cpp:49
static TOnExeStopF OnExeStopF
Definition: bd.h:227
static bool IsOnExeStopF()
Definition: bd.h:229
void WrNotify(const char *CaptionCStr, const char *NotifyCStr)
Definition: bd.cpp:41
bool(* TOnExeStopF)(char *MsgCStr)
Definition: bd.h:226
static TOnExeStopF GetOnExeStopF()
Definition: bd.h:231