fix ASSERT in the correct way
[enigma2.git] / lib / base / eerror.cpp
1 #include <lib/base/eerror.h>
2 #include <lib/base/elock.h>
3 #include <stdarg.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7
8 #include <string>
9
10 #ifdef MEMLEAK_CHECK
11 AllocList *allocList;
12 pthread_mutex_t memLock =
13         PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
14
15 void DumpUnfreed()
16 {
17         AllocList::iterator i;
18         unsigned int totalSize = 0;
19
20         if(!allocList)
21                 return;
22
23         size_t len = 1024;
24         char *buffer = (char*)malloc(1024);
25         for(i = allocList->begin(); i != allocList->end(); i++)
26         {
27                 unsigned int tmp;
28                 printf("%s\tLINE %d\tADDRESS %p\t%d unfreed\ttype %d (btcount %d)\n",
29                         i->second.file, i->second.line, (void*)i->second.address, i->second.size, i->second.type, i->second.btcount);
30                 totalSize += i->second.size;
31                 char **bt_string = backtrace_symbols( i->second.backtrace, i->second.btcount );
32                 for ( tmp=0; tmp < i->second.btcount; tmp++ )
33                 {
34                         if ( bt_string[tmp] )
35                         {
36                                 char *beg = strchr(bt_string[tmp], '(');
37                                 if ( beg )
38                                 {
39                                         std::string tmp1(beg+1);
40                                         int pos1=tmp1.find('+'), pos2=tmp1.find(')');
41                                         if ( pos1 != std::string::npos && pos2 != std::string::npos )
42                                         {
43                                                 std::string tmp2(tmp1.substr(pos1,(pos2-pos1)));
44                                                 tmp1.erase(pos1);
45                                                 if (tmp1.length())
46                                                 {
47                                                         int state;
48                                                         abi::__cxa_demangle(tmp1.c_str(), buffer, &len, &state);
49                                                         if (!state)
50                                                                 printf("%d %s%s\n", tmp, buffer,tmp2.c_str());
51                                                         else
52                                                                 printf("%d %s\n", tmp, bt_string[tmp]);
53                                                 }
54                                         }
55                                 }
56                                 else
57                                         printf("%d %s\n", tmp, bt_string[tmp]);
58                         }
59                 }
60                 free(bt_string);
61                 printf("\n");
62         }
63         free(buffer);
64
65         printf("-----------------------------------------------------------\n");
66         printf("Total Unfreed: %d bytes\n", totalSize);
67         fflush(stdout);
68 };
69 #endif
70
71 Signal2<void, int, const std::string&> logOutput;
72 int logOutputConsole=1;
73
74 static pthread_mutex_t DebugLock =
75         PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP;
76
77 void eFatal(const char* fmt, ...)
78 {
79         char buf[1024];
80         va_list ap;
81         va_start(ap, fmt);
82         vsnprintf(buf, 1024, fmt, ap);
83         va_end(ap);
84         singleLock s(DebugLock);
85         logOutput(lvlFatal, buf);
86         fprintf(stderr, "FATAL: %s\n",buf );
87         _exit(0);
88 }
89
90 #ifdef DEBUG
91 void eDebug(const char* fmt, ...)
92 {
93         char buf[1024];
94         va_list ap;
95         va_start(ap, fmt);
96         vsnprintf(buf, 1024, fmt, ap);
97         va_end(ap);
98         singleLock s(DebugLock);
99         logOutput(lvlDebug, std::string(buf) + "\n");
100         if (logOutputConsole)
101                 fprintf(stderr, "%s\n", buf);
102 }
103
104 void eDebugNoNewLine(const char* fmt, ...)
105 {
106         char buf[1024];
107         va_list ap;
108         va_start(ap, fmt);
109         vsnprintf(buf, 1024, fmt, ap);
110         va_end(ap);
111         singleLock s(DebugLock);
112         logOutput(lvlDebug, buf);
113         if (logOutputConsole)
114                 fprintf(stderr, "%s", buf);
115 }
116
117 void eWarning(const char* fmt, ...)
118 {
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(DebugLock);
125         logOutput(lvlWarning, std::string(buf) + "\n");
126         if (logOutputConsole)
127                 fprintf(stderr, "%s\n", buf);
128 }
129 #endif // DEBUG
130
131 void ePythonOutput(const char *string)
132 {
133 #ifdef DEBUG
134         singleLock s(DebugLock);
135         logOutput(lvlWarning, string);
136         if (logOutputConsole)
137                 fwrite(string, 1, strlen(string), stderr);
138 #endif
139 }
140
141 void eWriteCrashdump()
142 {
143                 /* implement me */
144 }