erroneously checked in
[codemux.git] / debug.h
1 #ifndef _DEBUG_H_
2 #define _DEBUG_H_
3 #include <stdio.h>
4 #include "applib.h"
5 #undef max
6
7
8 /*
9   TRACE  : print with function name
10   TRACE0 : print without function name
11   TRACE1 : print "buf" whose size is "size"
12 */
13
14 #define DEBUG
15
16 extern HANDLE hdebugLog;
17 extern int defaultTraceSync;
18 #define TRACE0(fmt, msg...) {                                             \
19        char __buf[2048];                                                  \
20        if (hdebugLog) {                                                   \
21           snprintf(__buf, sizeof(__buf), fmt, ##msg);                     \
22           WriteLog(hdebugLog, __buf, strlen(__buf), defaultTraceSync);    \
23        }                                                                  \
24 }          
25 #define TRACE1(buf, size) {                                 \
26        WriteLog(hdebugLog, buf, size, defaultTraceSync);    \
27 }
28 #define TRACE(fmt, msg...) {                                               \
29        char __buf[2048];                                                   \
30        if (hdebugLog) {                                                    \
31          snprintf(__buf, sizeof(__buf), "[%s] " fmt, __FUNCTION__, ##msg); \
32          WriteLog(hdebugLog, __buf, strlen(__buf), defaultTraceSync);      \
33        }                                                                   \
34 }                                                                  
35 #define TRACEX(fmt) {                                                      \
36        char __buf[2048];                                                   \
37        if (hdebugLog) {                                                    \
38          snprintf(__buf, sizeof(__buf), "[%s] " fmt, __FUNCTION__);        \
39          WriteLog(hdebugLog, __buf, strlen(__buf), defaultTraceSync);      \
40        }                                                                   \
41 }                                                                  
42
43 #ifndef HERE
44 #define HERE TRACE("file %s, line %d, func %s\n", __FILE__, __LINE__, __FUNCTION__)
45 #endif
46
47 #ifdef DEBUG
48 #define ASSERT(exp) {                                         \
49   if (!(exp)) {                                               \
50     TRACE("ASSERTION (%s) FAILED in %s (%s:%d)\n",            \
51          (#exp), __FUNCTION__, __FILE__, __LINE__);           \
52   }                                                           \
53 }
54 #else
55 #define ASSERT(exp)         1 ? (void)0 : (exp)
56 #endif // DEBUG
57
58 /*--------------------------------------------------------------
59   macros used for debugging memory leaks 
60   if DEBUG_MEMORY_LEAK is enabled, we track down all the memory
61   allocation/freeing to count the number of allocations
62  -------------------------------------------------------------*/
63 //#define DEBUG_MEMORY_LEAK
64
65 #ifndef DEBUG_MEMORY_LEAK
66
67 #define xcalloc(nmemb, size) calloc(nmemb, size)
68 #define xmalloc(size)        malloc(size)
69 #define xrealloc(ptr, size)  realloc(ptr, size)
70 #define xstrdup(s)           strdup(s)
71 #define xfree(ptr)           free(ptr)
72
73 #else
74
75 #define xcalloc(nmemb, size) dbgcalloc(nmemb, size, __FUNCTION__, \
76                                       __FILE__, __LINE__)
77 #define xmalloc(size)        dbgmalloc(size, __FUNCTION__,\
78                                        __FILE__, __LINE__)
79 #define xrealloc(ptr, size)  dbgrealloc(ptr, size, __FUNCTION__,\
80                                         __FILE__, __LINE__)
81 #define xstrdup(s)           dbgstrdup(s, __FUNCTION__, __FILE__, __LINE__)
82 #define xfree(ptr)           dbgfree(ptr, __FUNCTION__, __FILE__, __LINE__)
83
84 void *dbgcalloc(size_t nmemb, size_t size, 
85                 const char* func, const char* file, const int line);
86 void *dbgmalloc(size_t size, 
87                 const char* func, const char* file, const int line);
88 void *dbgrealloc(void *ptr, size_t size, 
89                  const char* func, const char* file, const int line);
90 char *dbgstrdup(const char *s, 
91                 const char* func, const char* file, const int line);
92 void  dbgfree(void *ptr, const char* func, const char* file, const int line);
93 void  dbg_print_memtrace(void);
94
95 #endif /* DEBUG_MEMOTY_LEAK */
96
97 #endif /* _DEBUG_H_ */