Setting tag codemux-0.1-15
[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 #ifdef DEBUG
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 #else
42 #define TRACE0(fmt, msg...) (void)0
43 #define TRACE1(fmt, msg...) (void)0
44 #define TRACE(fmt, msg...)  (void)0
45 #define TRACEX(fmt)         (void)0
46 #endif // DEBUG
47
48 /*--------------------------------------------------------------
49   macros used for debugging memory leaks 
50   if DEBUG_MEMORY_LEAK is enabled, we track down all the memory
51   allocation/freeing to count the number of allocations
52  -------------------------------------------------------------*/
53 //#define DEBUG_MEMORY_LEAK
54
55 #ifndef DEBUG_MEMORY_LEAK
56
57 #define xcalloc(nmemb, size) calloc(nmemb, size)
58 #define xmalloc(size)        malloc(size)
59 #define xrealloc(ptr, size)  realloc(ptr, size)
60 #define xstrdup(s)           strdup(s)
61 #define xfree(ptr)           free(ptr)
62
63 #else
64
65 #ifndef DEBUG
66 #error "define DEBUG first to make DEBUG_MEMORY_LEAK work"
67 #endif
68
69 #define xcalloc(nmemb, size) dbgcalloc(nmemb, size, __FUNCTION__, \
70                                       __FILE__, __LINE__)
71 #define xmalloc(size)        dbgmalloc(size, __FUNCTION__,        \
72                                        __FILE__, __LINE__)
73 #define xrealloc(ptr, size)  dbgrealloc(ptr, size, __FUNCTION__,  \
74                                         __FILE__, __LINE__)
75 #define xstrdup(s)           dbgstrdup(s, __FUNCTION__, __FILE__, __LINE__)
76 #define xfree(ptr)           dbgfree(ptr, __FUNCTION__, __FILE__, __LINE__)
77
78 void *dbgcalloc(size_t nmemb, size_t size, 
79                 const char* func, const char* file, const int line);
80 void *dbgmalloc(size_t size, 
81                 const char* func, const char* file, const int line);
82 void *dbgrealloc(void *ptr, size_t size, 
83                  const char* func, const char* file, const int line);
84 char *dbgstrdup(const char *s, 
85                 const char* func, const char* file, const int line);
86 void  dbgfree(void *ptr, const char* func, const char* file, const int line);
87 void  dbg_print_memtrace(void);
88
89 #endif /* DEBUG_MEMOTY_LEAK */
90
91 #endif /* _DEBUG_H_ */