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