epgcache: add possibility to change the default epg cache read/write path
[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 typedef struct
21 {
22         unsigned int address;
23         unsigned int size;
24         const char *file;
25         void *backtrace[BACKTRACE_DEPTH];
26         unsigned char btcount;
27         unsigned short line;
28         unsigned char type;
29 } ALLOC_INFO;
30
31 typedef std::map<unsigned int, ALLOC_INFO> AllocList;
32
33 extern AllocList *allocList;
34 extern pthread_mutex_t memLock;
35
36 static inline void AddTrack(unsigned int addr,  unsigned int asize,  const char *fname, unsigned int lnum, unsigned int type)
37 {
38         ALLOC_INFO info;
39
40         if(!allocList)
41                 allocList = new(AllocList);
42
43         info.address = addr;
44         info.file = fname;
45         info.line = lnum;
46         info.size = asize;
47         info.type = type;
48         info.btcount = 0; //backtrace( info.backtrace, BACKTRACE_DEPTH );
49         singleLock s(memLock);
50         (*allocList)[addr]=info;
51 };
52
53 static inline void RemoveTrack(unsigned int addr, unsigned int type)
54 {
55         if(!allocList)
56                 return;
57         AllocList::iterator i;
58         singleLock s(memLock);
59         i = allocList->find(addr);
60         if ( i != allocList->end() )
61         {
62                 if ( i->second.type != type )
63                         i->second.type=3;
64                 else
65                         allocList->erase(i);
66         }
67 };
68
69 inline void * operator new(size_t size, const char *file, int line)
70 {
71         void *ptr = (void *)malloc(size);
72         AddTrack((unsigned int)ptr, size, file, line, 1);
73         return(ptr);
74 };
75
76 inline void operator delete(void *p)
77 {
78         RemoveTrack((unsigned int)p,1);
79         free(p);
80 };
81
82 inline void * operator new[](size_t size, const char *file, int line)
83 {
84         void *ptr = (void *)malloc(size);
85         AddTrack((unsigned int)ptr, size, file, line, 2);
86         return(ptr);
87 };
88
89 inline void operator delete[](void *p)
90 {
91         RemoveTrack((unsigned int)p, 2);
92         free(p);
93 };
94
95 void DumpUnfreed();
96 #define new new(__FILE__, __LINE__)
97
98 #endif // MEMLEAK_CHECK
99
100 #ifndef NULL
101 #define NULL 0
102 #endif
103
104 #ifdef ASSERT
105 #undef ASSERT
106 #endif
107
108 #ifndef SWIG
109
110 #define CHECKFORMAT __attribute__ ((__format__(__printf__, 1, 2)))
111
112 extern Signal2<void, int, const std::string&> logOutput;
113 extern int logOutputConsole;
114
115 void CHECKFORMAT eFatal(const char*, ...);
116 enum { lvlDebug=1, lvlWarning=2, lvlFatal=4 };
117
118 #ifdef DEBUG
119     void CHECKFORMAT eDebug(const char*, ...);
120     void CHECKFORMAT eDebugNoNewLine(const char*, ...);
121     void CHECKFORMAT eWarning(const char*, ...);
122     #define ASSERT(x) { if (!(x)) eFatal("%s:%d ASSERTION %s FAILED!", __FILE__, __LINE__, #x); }
123 #else  // DEBUG
124     inline void eDebug(const char* fmt, ...)
125     {
126     }
127
128     inline void eDebugNoNewLine(const char* fmt, ...)
129     {
130     }
131
132     inline void eWarning(const char* fmt, ...)
133     {
134     }
135     #define ASSERT(x) do { } while (0)
136 #endif //DEBUG
137
138 void eWriteCrashdump();
139
140 #endif // SWIG
141
142 void ePythonOutput(const char *);
143
144 #endif // __E_ERROR__