trunk 20080912
[gitenigma.git] / lib / base / eerror.cpp
blob2926bbf8318a2ff2d55e673b3a87c6d475e7d4ec
1 #include <lib/base/eerror.h>
2 #include <stdarg.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
7 #include <lib/gui/emessage.h>
9 #ifdef MEMLEAK_CHECK
10 AllocList *allocList;
11 pthread_mutex_t memLock =
12 PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
14 void DumpUnfreed()
16 AllocList::iterator i;
17 unsigned int totalSize = 0;
19 if(!allocList)
20 return;
22 size_t len = 1024;
23 char *buffer = (char*)malloc(1024);
24 for(i = allocList->begin(); i != allocList->end(); i++)
26 unsigned int tmp;
27 printf("%s\tLINE %d\tADDRESS %p\t%d unfreed\ttype %d (btcount %d)\n",
28 i->second.file, i->second.line, (void*)i->second.address, i->second.size, i->second.type, i->second.btcount);
29 totalSize += i->second.size;
30 char **bt_string = backtrace_symbols( i->second.backtrace, i->second.btcount );
31 for ( tmp=0; tmp < i->second.btcount; tmp++ )
33 if ( bt_string[tmp] )
35 char *beg = strchr(bt_string[tmp], '(');
36 if ( beg )
38 std::string tmp1(beg+1);
39 int pos1=tmp1.find('+'), pos2=tmp1.find(')');
40 if ( pos1 != std::string::npos && pos2 != std::string::npos )
42 std::string tmp2(tmp1.substr(pos1,(pos2-pos1)));
43 tmp1.erase(pos1);
44 if (tmp1.length())
46 int state;
47 abi::__cxa_demangle(tmp1.c_str(), buffer, &len, &state);
48 if (!state)
49 printf("%d %s%s\n", tmp, buffer,tmp2.c_str());
50 else
51 printf("%d %s\n", tmp, bt_string[tmp]);
55 else
56 printf("%d %s\n", tmp, bt_string[tmp]);
59 free(bt_string);
60 printf("\n");
62 free(buffer);
64 printf("-----------------------------------------------------------\n");
65 printf("Total Unfreed: %d bytes\n", totalSize);
66 fflush(stdout);
69 #else
70 #include <lib/system/elock.h>
71 #endif
72 pthread_mutex_t signalLock =
73 PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP;
75 int infatal=0;
77 Signal2<void, int, const eString&> logOutput;
78 int logOutputConsole=1;
80 void eFatal(const char* fmt, ...)
82 char buf[1024];
83 va_list ap;
84 va_start(ap, fmt);
85 vsnprintf(buf, 1024, fmt, ap);
86 va_end(ap);
88 singleLock s(signalLock);
89 logOutput(lvlFatal, buf);
90 fprintf(stderr, "%s\n",buf );
92 if (!infatal)
94 infatal=1;
95 eMessageBox msg(buf, "FATAL ERROR", eMessageBox::iconError|eMessageBox::btOK);
96 msg.show();
97 msg.exec();
99 _exit(0);
102 #ifdef DEBUG
103 void eDebug(const char* fmt, ...)
105 char buf[1024];
106 va_list ap;
107 va_start(ap, fmt);
108 vsnprintf(buf, 1024, fmt, ap);
109 va_end(ap);
110 singleLock s(signalLock);
111 if (logOutputConsole)
112 fprintf(stderr, "%s\n", buf);
113 else
114 logOutput(lvlDebug, eString(buf) + "\n");
117 void eDebugNoNewLine(const char* fmt, ...)
119 char buf[1024];
120 va_list ap;
121 va_start(ap, fmt);
122 vsnprintf(buf, 1024, fmt, ap);
123 va_end(ap);
124 singleLock s(signalLock);
125 if (logOutputConsole)
126 fprintf(stderr, buf);
127 else
128 logOutput(lvlDebug, buf);
131 void eWarning(const char* fmt, ...)
133 char buf[1024];
134 va_list ap;
135 va_start(ap, fmt);
136 vsnprintf(buf, 1024, fmt, ap);
137 va_end(ap);
138 singleLock s(signalLock);
139 if (logOutputConsole)
140 fprintf(stderr, "%s\n", buf);
141 else
142 logOutput(lvlWarning, eString(buf) + "\n");
144 #endif // DEBUG