add support for cyclic garbage collection to eConsoleAppContainer
[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         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();
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         singleLock s(DebugLock);
90         logOutput(lvlFatal, "FATAL: " + std::string(buf) + "\n");
91         fprintf(stderr, "FATAL: %s\n",buf );
92         bsodFatal();
93 }
94
95 #ifdef DEBUG
96 void eDebug(const char* fmt, ...)
97 {
98         char buf[1024];
99         va_list ap;
100         va_start(ap, fmt);
101         vsnprintf(buf, 1024, fmt, ap);
102         va_end(ap);
103         singleLock s(DebugLock);
104         logOutput(lvlDebug, std::string(buf) + "\n");
105         if (logOutputConsole)
106                 fprintf(stderr, "%s\n", buf);
107 }
108
109 void eDebugNoNewLine(const char* fmt, ...)
110 {
111         char buf[1024];
112         va_list ap;
113         va_start(ap, fmt);
114         vsnprintf(buf, 1024, fmt, ap);
115         va_end(ap);
116         singleLock s(DebugLock);
117         logOutput(lvlDebug, buf);
118         if (logOutputConsole)
119                 fprintf(stderr, "%s", buf);
120 }
121
122 void eWarning(const char* fmt, ...)
123 {
124         char buf[1024];
125         va_list ap;
126         va_start(ap, fmt);
127         vsnprintf(buf, 1024, fmt, ap);
128         va_end(ap);
129         singleLock s(DebugLock);
130         logOutput(lvlWarning, std::string(buf) + "\n");
131         if (logOutputConsole)
132                 fprintf(stderr, "%s\n", buf);
133 }
134 #endif // DEBUG
135
136 void ePythonOutput(const char *string)
137 {
138 #ifdef DEBUG
139         singleLock s(DebugLock);
140         logOutput(lvlWarning, string);
141         if (logOutputConsole)
142                 fwrite(string, 1, strlen(string), stderr);
143 #endif
144 }
145
146 void eWriteCrashdump()
147 {
148                 /* implement me */
149 }