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