more work on keyboard support
[enigma2.git] / lib / base / eerror.h
1 #ifndef __E_ERROR__
2 #define __E_ERROR__
3
4 #include <string>
5 #include <map>       
6 #include <new>
7 #include <libsig_comp.h>
8
9 // to use memleak check change the following in configure.ac
10 // * add -DMEMLEAK_CHECK and -rdynamic to CPP_FLAGS
11
12 #ifdef MEMLEAK_CHECK
13 #define BACKTRACE_DEPTH 5
14 #include <map>
15 #include <lib/base/elock.h>
16 #include <execinfo.h>
17 #include <string>
18 #include <new>
19 #include <cxxabi.h>
20 #endif // MEMLEAK_CHECK
21
22 #ifndef NULL
23 #define NULL 0
24 #endif
25
26 #ifndef SWIG
27 #define CHECKFORMAT __attribute__ ((__format__(__printf__, 1, 2)))
28 #else
29 #define CHECKFORMAT
30 #endif
31
32 void CHECKFORMAT eFatal(const char*, ...);
33
34 enum { lvlDebug=1, lvlWarning=2, lvlFatal=4 };
35
36 #ifndef SWIG
37 extern Signal2<void, int, const std::string&> logOutput;
38 extern int logOutputConsole;
39 #endif
40
41 #ifdef ASSERT
42 #undef ASSERT
43 #endif
44
45 #ifdef DEBUG
46     void CHECKFORMAT eDebug(const char*, ...);
47     void CHECKFORMAT eDebugNoNewLine(const char*, ...);
48     void CHECKFORMAT eWarning(const char*, ...);
49 #ifndef SWIG
50     #define ASSERT(x) { if (!(x)) eFatal("%s:%d ASSERTION %s FAILED!", __FILE__, __LINE__, #x); }
51 #endif
52
53 #ifdef MEMLEAK_CHECK
54 typedef struct
55 {
56         unsigned int address;
57         unsigned int size;
58         char *file;
59         void *backtrace[BACKTRACE_DEPTH];
60         unsigned char btcount;
61         unsigned short line;
62         unsigned char type;
63 } ALLOC_INFO;
64
65 typedef std::map<unsigned int, ALLOC_INFO> AllocList;
66
67 extern AllocList *allocList;
68 extern pthread_mutex_t memLock;
69
70 static inline void AddTrack(unsigned int addr,  unsigned int asize,  const char *fname, unsigned int lnum, unsigned int type)
71 {
72         ALLOC_INFO info;
73
74         if(!allocList)
75                 allocList = new(AllocList);
76
77         info.address = addr;
78         info.file = strdup(fname);
79         info.line = lnum;
80         info.size = asize;
81         info.type = type;
82         info.btcount = backtrace( info.backtrace, BACKTRACE_DEPTH );
83         singleLock s(memLock);
84         (*allocList)[addr]=info;
85 };
86
87 static inline void RemoveTrack(unsigned int addr, unsigned int type)
88 {
89         if(!allocList)
90                 return;
91         AllocList::iterator i;
92         singleLock s(memLock);
93         i = allocList->find(addr);
94         if ( i != allocList->end() )
95         {
96                 if ( i->second.type != type )
97                         i->second.type=3;
98                 else
99                 {
100                         free(i->second.file);
101                         allocList->erase(i);
102                 }
103         }
104 };
105
106 inline void * operator new(unsigned int size, const char *file, int line)
107 {
108         void *ptr = (void *)malloc(size);
109         AddTrack((unsigned int)ptr, size, file, line, 1);
110         return(ptr);
111 };
112
113 inline void operator delete(void *p)
114 {
115         RemoveTrack((unsigned int)p,1);
116         free(p);
117 };
118
119 inline void * operator new[](unsigned int size, const char *file, int line)
120 {
121         void *ptr = (void *)malloc(size);
122         AddTrack((unsigned int)ptr, size, file, line, 2);
123         return(ptr);
124 };
125
126 inline void operator delete[](void *p)
127 {
128         RemoveTrack((unsigned int)p, 2);
129         free(p);
130 };
131
132 void DumpUnfreed();
133 #define new new(__FILE__, __LINE__)
134
135 #endif // MEMLEAK_CHECK
136
137 #else
138     inline void eDebug(const char* fmt, ...)
139     {
140     }
141
142     inline void eDebugNoNewLine(const char* fmt, ...)
143     {
144     }
145
146     inline void eWarning(const char* fmt, ...)
147     {
148     }
149     #define ASSERT(x) do { } while (0)
150 #endif //DEBUG
151
152 void ePythonOutput(const char *);
153 void eWriteCrashdump();
154
155 #endif // __E_ERROR__