always use unsigned
[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 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         logOutput(lvlFatal, buf);
87         fprintf(stderr, "FATAL: %s\n",buf );
88 #if 0
89         if (!infatal)
90         {
91                 infatal=1;
92                 eMessageBox msg(buf, "FATAL ERROR", eMessageBox::iconError|eMessageBox::btOK);
93                 msg.show();
94                 msg.exec();
95         }
96 #endif
97
98         _exit(0);
99 }
100
101 #ifdef DEBUG
102 void eDebug(const char* fmt, ...)
103 {
104         char buf[1024];
105         va_list ap;
106         va_start(ap, fmt);
107         vsnprintf(buf, 1024, fmt, ap);
108         va_end(ap);
109         logOutput(lvlDebug, std::string(buf) + "\n");
110         if (logOutputConsole)
111                 fprintf(stderr, "%s\n", buf);
112 }
113
114 void eDebugNoNewLine(const char* fmt, ...)
115 {
116         char buf[1024];
117         va_list ap;
118         va_start(ap, fmt);
119         vsnprintf(buf, 1024, fmt, ap);
120         va_end(ap);
121         logOutput(lvlDebug, buf);
122         if (logOutputConsole)
123                 fprintf(stderr, "%s", buf);
124 }
125
126 void eWarning(const char* fmt, ...)
127 {
128         char buf[1024];
129         va_list ap;
130         va_start(ap, fmt);
131         vsnprintf(buf, 1024, fmt, ap);
132         va_end(ap);
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         logOutput(lvlWarning, string);
142         if (logOutputConsole)
143                 fwrite(string, 1, strlen(string), stderr);
144 }
145
146 void eWriteCrashdump()
147 {
148                 /* implement me */
149 }