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