fix
[enigma2.git] / lib / base / eerror.cpp
index af78040634f2fa1ea6322c9f20dd4fdf6468db5f..241b2d3df9e8c3e48be90b80336599cc256c9c00 100644 (file)
@@ -1,4 +1,5 @@
 #include <lib/base/eerror.h>
+#include <lib/base/elock.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -6,20 +7,72 @@
 
 #include <string>
 
-// #include <lib/gui/emessage.h>
-
 #ifdef MEMLEAK_CHECK
 AllocList *allocList;
 pthread_mutex_t memLock =
        PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
-#else
-       #include <lib/base/elock.h>
-#endif
 
-int infatal=0;
+void DumpUnfreed()
+{
+       AllocList::iterator i;
+       unsigned int totalSize = 0;
+
+       if(!allocList)
+               return;
+
+       size_t len = 1024;
+       char *buffer = (char*)malloc(1024);
+       for(i = allocList->begin(); i != allocList->end(); i++)
+       {
+               unsigned int tmp;
+               printf("%s\tLINE %d\tADDRESS %p\t%d unfreed\ttype %d (btcount %d)\n",
+                       i->second.file, i->second.line, (void*)i->second.address, i->second.size, i->second.type, i->second.btcount);
+               totalSize += i->second.size;
+               char **bt_string = backtrace_symbols( i->second.backtrace, i->second.btcount );
+               for ( tmp=0; tmp < i->second.btcount; tmp++ )
+               {
+                       if ( bt_string[tmp] )
+                       {
+                               char *beg = strchr(bt_string[tmp], '(');
+                               if ( beg )
+                               {
+                                       std::string tmp1(beg+1);
+                                       int pos1=tmp1.find('+'), pos2=tmp1.find(')');
+                                       if ( pos1 != std::string::npos && pos2 != std::string::npos )
+                                       {
+                                               std::string tmp2(tmp1.substr(pos1,(pos2-pos1)));
+                                               tmp1.erase(pos1);
+                                               if (tmp1.length())
+                                               {
+                                                       int state;
+                                                       abi::__cxa_demangle(tmp1.c_str(), buffer, &len, &state);
+                                                       if (!state)
+                                                               printf("%d %s%s\n", tmp, buffer,tmp2.c_str());
+                                                       else
+                                                               printf("%d %s\n", tmp, bt_string[tmp]);
+                                               }
+                                       }
+                               }
+                               else
+                                       printf("%d %s\n", tmp, bt_string[tmp]);
+                       }
+               }
+               free(bt_string);
+               printf("\n");
+       }
+       free(buffer);
+
+       printf("-----------------------------------------------------------\n");
+       printf("Total Unfreed: %d bytes\n", totalSize);
+       fflush(stdout);
+};
+#endif
 
 Signal2<void, int, const std::string&> logOutput;
-int logOutputConsole=1;
+int logOutputConsole=0;
+
+static pthread_mutex_t DebugLock =
+       PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP;
 
 void eFatal(const char* fmt, ...)
 {
@@ -28,18 +81,9 @@ void eFatal(const char* fmt, ...)
        va_start(ap, fmt);
        vsnprintf(buf, 1024, fmt, ap);
        va_end(ap);
+       singleLock s(DebugLock);
        logOutput(lvlFatal, buf);
-       fprintf(stderr, "%s\n",buf );
-#if 0
-       if (!infatal)
-       {
-               infatal=1;
-               eMessageBox msg(buf, "FATAL ERROR", eMessageBox::iconError|eMessageBox::btOK);
-               msg.show();
-               msg.exec();
-       }
-#endif
-
+       fprintf(stderr, "FATAL: %s\n",buf );
        _exit(0);
 }
 
@@ -51,6 +95,7 @@ void eDebug(const char* fmt, ...)
        va_start(ap, fmt);
        vsnprintf(buf, 1024, fmt, ap);
        va_end(ap);
+       singleLock s(DebugLock);
        logOutput(lvlDebug, std::string(buf) + "\n");
        if (logOutputConsole)
                fprintf(stderr, "%s\n", buf);
@@ -63,6 +108,7 @@ void eDebugNoNewLine(const char* fmt, ...)
        va_start(ap, fmt);
        vsnprintf(buf, 1024, fmt, ap);
        va_end(ap);
+       singleLock s(DebugLock);
        logOutput(lvlDebug, buf);
        if (logOutputConsole)
                fprintf(stderr, "%s", buf);
@@ -75,8 +121,24 @@ void eWarning(const char* fmt, ...)
        va_start(ap, fmt);
        vsnprintf(buf, 1024, fmt, ap);
        va_end(ap);
+       singleLock s(DebugLock);
        logOutput(lvlWarning, std::string(buf) + "\n");
        if (logOutputConsole)
                fprintf(stderr, "%s\n", buf);
 }
 #endif // DEBUG
+
+void ePythonOutput(const char *string)
+{
+#ifdef DEBUG
+       singleLock s(DebugLock);
+       logOutput(lvlWarning, string);
+       if (logOutputConsole)
+               fwrite(string, 1, strlen(string), stderr);
+#endif
+}
+
+void eWriteCrashdump()
+{
+               /* implement me */
+}