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