rewind into timeshift (almost) works now
[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 extern void bsodFatal();
78
79 void eFatal(const char* fmt, ...)
80 {
81         char buf[1024];
82         va_list ap;
83         va_start(ap, fmt);
84         vsnprintf(buf, 1024, fmt, ap);
85         va_end(ap);
86         singleLock s(DebugLock);
87         logOutput(lvlFatal, buf);
88         fprintf(stderr, "FATAL: %s\n",buf );
89         bsodFatal();
90 }
91
92 #ifdef DEBUG
93 void eDebug(const char* fmt, ...)
94 {
95         char buf[1024];
96         va_list ap;
97         va_start(ap, fmt);
98         vsnprintf(buf, 1024, fmt, ap);
99         va_end(ap);
100         singleLock s(DebugLock);
101         logOutput(lvlDebug, std::string(buf) + "\n");
102         if (logOutputConsole)
103                 fprintf(stderr, "%s\n", buf);
104 }
105
106 void eDebugNoNewLine(const char* fmt, ...)
107 {
108         char buf[1024];
109         va_list ap;
110         va_start(ap, fmt);
111         vsnprintf(buf, 1024, fmt, ap);
112         va_end(ap);
113         singleLock s(DebugLock);
114         logOutput(lvlDebug, buf);
115         if (logOutputConsole)
116                 fprintf(stderr, "%s", buf);
117 }
118
119 void eWarning(const char* fmt, ...)
120 {
121         char buf[1024];
122         va_list ap;
123         va_start(ap, fmt);
124         vsnprintf(buf, 1024, fmt, ap);
125         va_end(ap);
126         singleLock s(DebugLock);
127         logOutput(lvlWarning, std::string(buf) + "\n");
128         if (logOutputConsole)
129                 fprintf(stderr, "%s\n", buf);
130 }
131 #endif // DEBUG
132
133 void ePythonOutput(const char *string)
134 {
135 #ifdef DEBUG
136         singleLock s(DebugLock);
137         logOutput(lvlWarning, string);
138         if (logOutputConsole)
139                 fwrite(string, 1, strlen(string), stderr);
140 #endif
141 }
142
143 void eWriteCrashdump()
144 {
145                 /* implement me */
146 }