Merge branch 'master' of git.opendreambox.org:/git/enigma2
[vuplus_dvbapp] / 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         FILE *f = fopen("/var/tmp/enigma2_mem.out", "w");
24         size_t len = 1024;
25         char *buffer = (char*)malloc(1024);
26         for(i = allocList->begin(); i != allocList->end(); i++)
27         {
28                 unsigned int tmp;
29                 fprintf(f, "%s\tLINE %d\tADDRESS %p\t%d unfreed\ttype %d (btcount %d)\n",
30                         i->second.file, i->second.line, (void*)i->second.address, i->second.size, i->second.type, i->second.btcount);
31                 totalSize += i->second.size;
32
33                 char **bt_string = backtrace_symbols( i->second.backtrace, i->second.btcount );
34                 for ( tmp=0; tmp < i->second.btcount; tmp++ )
35                 {
36                         if ( bt_string[tmp] )
37                         {
38                                 char *beg = strchr(bt_string[tmp], '(');
39                                 if ( beg )
40                                 {
41                                         std::string tmp1(beg+1);
42                                         int pos1=tmp1.find('+'), pos2=tmp1.find(')');
43                                         if ( pos1 != std::string::npos && pos2 != std::string::npos )
44                                         {
45                                                 std::string tmp2(tmp1.substr(pos1,(pos2-pos1)));
46                                                 tmp1.erase(pos1);
47                                                 if (tmp1.length())
48                                                 {
49                                                         int state;
50                                                         abi::__cxa_demangle(tmp1.c_str(), buffer, &len, &state);
51                                                         if (!state)
52                                                                 fprintf(f, "%d %s%s\n", tmp, buffer,tmp2.c_str());
53                                                         else
54                                                                 fprintf(f, "%d %s\n", tmp, bt_string[tmp]);
55                                                 }
56                                         }
57                                 }
58                                 else
59                                         fprintf(f, "%d %s\n", tmp, bt_string[tmp]);
60                         }
61                 }
62                 free(bt_string);
63                 if (i->second.btcount)
64                         fprintf(f, "\n");
65         }
66         free(buffer);
67
68         fprintf(f, "-----------------------------------------------------------\n");
69         fprintf(f, "Total Unfreed: %d bytes\n", totalSize);
70         fflush(f);
71 };
72 #endif
73
74 Signal2<void, int, const std::string&> logOutput;
75 int logOutputConsole=1;
76
77 static pthread_mutex_t DebugLock =
78         PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP;
79
80 extern void bsodFatal(const char *component);
81
82 void eFatal(const char* fmt, ...)
83 {
84         char buf[1024];
85         va_list ap;
86         va_start(ap, fmt);
87         vsnprintf(buf, 1024, fmt, ap);
88         va_end(ap);
89         {
90                 singleLock s(DebugLock);
91                 logOutput(lvlFatal, "FATAL: " + std::string(buf) + "\n");
92                 fprintf(stderr, "FATAL: %s\n",buf );
93         }
94         bsodFatal("enigma2");
95 }
96
97 #ifdef DEBUG
98 void eDebug(const char* fmt, ...)
99 {
100         char buf[1024];
101         va_list ap;
102         va_start(ap, fmt);
103         vsnprintf(buf, 1024, fmt, ap);
104         va_end(ap);
105         singleLock s(DebugLock);
106         logOutput(lvlDebug, std::string(buf) + "\n");
107         if (logOutputConsole)
108                 fprintf(stderr, "%s\n", buf);
109 }
110
111 void eDebugNoNewLine(const char* fmt, ...)
112 {
113         char buf[1024];
114         va_list ap;
115         va_start(ap, fmt);
116         vsnprintf(buf, 1024, fmt, ap);
117         va_end(ap);
118         singleLock s(DebugLock);
119         logOutput(lvlDebug, buf);
120         if (logOutputConsole)
121                 fprintf(stderr, "%s", buf);
122 }
123
124 void eWarning(const char* fmt, ...)
125 {
126         char buf[1024];
127         va_list ap;
128         va_start(ap, fmt);
129         vsnprintf(buf, 1024, fmt, ap);
130         va_end(ap);
131         singleLock s(DebugLock);
132         logOutput(lvlWarning, std::string(buf) + "\n");
133         if (logOutputConsole)
134                 fprintf(stderr, "%s\n", buf);
135 }
136 #endif // DEBUG
137
138 void ePythonOutput(const char *string)
139 {
140 #ifdef DEBUG
141         singleLock s(DebugLock);
142         logOutput(lvlWarning, string);
143         if (logOutputConsole)
144                 fwrite(string, 1, strlen(string), stderr);
145 #endif
146 }
147
148 void eWriteCrashdump()
149 {
150                 /* implement me */
151 }