a84118793a91d377c9dfe7eeb6b390f261fb6edd
[enigma2.git] / lib / base / eerror.cpp
1 #include <lib/base/eerror.h>
2 #include <stdarg.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6
7 #include <string>
8
9 // #include <lib/gui/emessage.h>
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         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                 printf("%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                 char **bt_string = backtrace_symbols( i->second.backtrace, i->second.btcount );
33                 for ( tmp=0; tmp < i->second.btcount; tmp++ )
34                 {
35                         if ( bt_string[tmp] )
36                         {
37                                 char *beg = strchr(bt_string[tmp], '(');
38                                 if ( beg )
39                                 {
40                                         std::string tmp1(beg+1);
41                                         int pos1=tmp1.find('+'), pos2=tmp1.find(')');
42                                         if ( pos1 != std::string::npos && pos2 != std::string::npos )
43                                         {
44                                                 std::string tmp2(tmp1.substr(pos1,(pos2-pos1)));
45                                                 tmp1.erase(pos1);
46                                                 if (tmp1.length())
47                                                 {
48                                                         int state;
49                                                         abi::__cxa_demangle(tmp1.c_str(), buffer, &len, &state);
50                                                         if (!state)
51                                                                 printf("%d %s%s\n", tmp, buffer,tmp2.c_str());
52                                                         else
53                                                                 printf("%d %s\n", tmp, bt_string[tmp]);
54                                                 }
55                                         }
56                                 }
57                                 else
58                                         printf("%d %s\n", tmp, bt_string[tmp]);
59                         }
60                 }
61                 free(bt_string);
62                 printf("\n");
63         }
64         free(buffer);
65
66         printf("-----------------------------------------------------------\n");
67         printf("Total Unfreed: %d bytes\n", totalSize);
68         fflush(stdout);
69 };
70 #else
71         #include <lib/base/elock.h>
72 #endif
73
74 int infatal=0;
75
76 Signal2<void, int, const std::string&> logOutput;
77 int logOutputConsole=1;
78
79 pthread_mutex_t signalLock =
80         PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP;
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(signalLock);
90         logOutput(lvlFatal, buf);
91         fprintf(stderr, "FATAL: %s\n",buf );
92 #if 0
93         if (!infatal)
94         {
95                 infatal=1;
96                 eMessageBox msg(buf, "FATAL ERROR", eMessageBox::iconError|eMessageBox::btOK);
97                 msg.show();
98                 msg.exec();
99         }
100 #endif
101         _exit(0);
102 }
103
104 #ifdef DEBUG
105 void eDebug(const char* fmt, ...)
106 {
107         char buf[1024];
108         va_list ap;
109         va_start(ap, fmt);
110         vsnprintf(buf, 1024, fmt, ap);
111         va_end(ap);
112         singleLock s(signalLock);
113         logOutput(lvlDebug, std::string(buf) + "\n");
114         if (logOutputConsole)
115                 fprintf(stderr, "%s\n", buf);
116 }
117
118 void eDebugNoNewLine(const char* fmt, ...)
119 {
120         char buf[1024];
121         va_list ap;
122         va_start(ap, fmt);
123         vsnprintf(buf, 1024, fmt, ap);
124         va_end(ap);
125         singleLock s(signalLock);
126         logOutput(lvlDebug, buf);
127         if (logOutputConsole)
128                 fprintf(stderr, "%s", buf);
129 }
130
131 void eWarning(const char* fmt, ...)
132 {
133         char buf[1024];
134         va_list ap;
135         va_start(ap, fmt);
136         vsnprintf(buf, 1024, fmt, ap);
137         va_end(ap);
138         singleLock s(signalLock);
139         logOutput(lvlWarning, std::string(buf) + "\n");
140         if (logOutputConsole)
141                 fprintf(stderr, "%s\n", buf);
142 }
143 #endif // DEBUG
144
145 void ePythonOutput(const char *string)
146 {
147         logOutput(lvlWarning, string);
148         if (logOutputConsole)
149                 fwrite(string, 1, strlen(string), stderr);
150 }
151
152 void eWriteCrashdump()
153 {
154                 /* implement me */
155 }