comparison src/debug.c @ 350:7a91e30826d8 stable-0-6-65

Hans Liss - debug level output
author Carl Byington <carl@five-ten-sg.com>
date Fri, 11 Sep 2015 10:18:03 -0700
parents cc8ee701f190
children
comparison
equal deleted inserted replaced
349:a57c15b3108a 350:7a91e30826d8
10 #define NUM_COL 32 10 #define NUM_COL 32
11 #define MAX_DEPTH 32 11 #define MAX_DEPTH 32
12 12
13 static struct pst_debug_func *func_head = NULL; 13 static struct pst_debug_func *func_head = NULL;
14 static int func_depth = 0; 14 static int func_depth = 0;
15 static int pst_debuglevel = 0;
15 static char indent[MAX_DEPTH*4+1]; 16 static char indent[MAX_DEPTH*4+1];
16 static FILE *debug_fp = NULL; 17 static FILE *debug_fp = NULL;
17 #ifdef HAVE_SEMAPHORE_H 18 #ifdef HAVE_SEMAPHORE_H
18 static sem_t* debug_mutex = NULL; 19 static sem_t* debug_mutex = NULL;
19 #endif 20 #endif
20 21
22
23 void pst_debug_setlevel(int level)
24 {
25 pst_debuglevel = level;
26 }
21 27
22 void pst_debug_lock() 28 void pst_debug_lock()
23 { 29 {
24 #ifdef HAVE_SEMAPHORE_H 30 #ifdef HAVE_SEMAPHORE_H
25 if (debug_mutex) sem_wait(debug_mutex); 31 if (debug_mutex) sem_wait(debug_mutex);
48 exit(1); 54 exit(1);
49 } 55 }
50 } 56 }
51 57
52 58
53 void pst_debug_func(const char* function) { 59 void pst_debug_func(int level, const char* function) {
60 if (pst_debuglevel > level) return;
54 struct pst_debug_func *func_ptr = pst_malloc (sizeof(struct pst_debug_func)); 61 struct pst_debug_func *func_ptr = pst_malloc (sizeof(struct pst_debug_func));
55 func_ptr->name = strdup(function); 62 func_ptr->name = strdup(function);
56 func_ptr->next = func_head; 63 func_ptr->next = func_head;
57 func_head = func_ptr; 64 func_head = func_ptr;
58 func_depth++; 65 func_depth++;
59 } 66 }
60 67
61 68
62 void pst_debug_func_ret() { 69 void pst_debug_func_ret(int level) {
70 if (pst_debuglevel > level) return;
63 //remove the head item 71 //remove the head item
64 struct pst_debug_func *func_ptr = func_head; 72 struct pst_debug_func *func_ptr = func_head;
65 if (func_head) { 73 if (func_head) {
66 func_head = func_head->next; 74 func_head = func_head->next;
67 free(func_ptr->name); 75 free(func_ptr->name);
71 DIE(("function list is empty!\n")); 79 DIE(("function list is empty!\n"));
72 } 80 }
73 } 81 }
74 82
75 83
76 static void pst_debug_info(int line, const char* file); 84 static void pst_debug_info(int level, int line, const char* file);
77 static void pst_debug_info(int line, const char* file) { 85 static void pst_debug_info(int level, int line, const char* file) {
86 if (pst_debuglevel > level) return;
78 int le = (func_depth > MAX_DEPTH) ? MAX_DEPTH : func_depth; 87 int le = (func_depth > MAX_DEPTH) ? MAX_DEPTH : func_depth;
79 if (le > 0) le--; 88 if (le > 0) le--;
80 char *func = (func_head ? func_head->name : "No Function"); 89 char *func = (func_head ? func_head->name : "No Function");
81 pst_debug_lock(); 90 pst_debug_lock();
82 fprintf(debug_fp, "%06d %.*s%s %s(%d) ", getpid(), le*4, indent, func, file, line); 91 fprintf(debug_fp, "%06d %.*s%s %s(%d) ", getpid(), le*4, indent, func, file, line);
83 } 92 }
84 93
85 94
86 void pst_debug(int line, const char* file, const char *fmt, ...) { 95 void pst_debug(int level, int line, const char* file, const char *fmt, ...) {
96 if (pst_debuglevel > level) return;
87 if (debug_fp) { 97 if (debug_fp) {
88 pst_debug_info(line, file); 98 pst_debug_info(level, line, file);
89 va_list ap; 99 va_list ap;
90 va_start(ap,fmt); 100 va_start(ap,fmt);
91 vfprintf(debug_fp, fmt, ap); 101 vfprintf(debug_fp, fmt, ap);
92 va_end(ap); 102 va_end(ap);
93 fflush(debug_fp); 103 fflush(debug_fp);
94 pst_debug_unlock(); 104 pst_debug_unlock();
95 } 105 }
96 } 106 }
97 107
98 108
99 void pst_debug_hexdump(int line, const char *file, const char *buf, size_t size, int cols, int delta) { 109 void pst_debug_hexdump(int level, int line, const char *file, const char *buf, size_t size, int cols, int delta) {
110 if (pst_debuglevel > level) return;
100 if (debug_fp) { 111 if (debug_fp) {
101 pst_debug_info(line, file); 112 pst_debug_info(level, line, file);
102 pst_debug_hexdumper(debug_fp, buf, size, cols, delta); 113 pst_debug_hexdumper(debug_fp, buf, size, cols, delta);
103 pst_debug_unlock(); 114 pst_debug_unlock();
104 } 115 }
105 } 116 }
106 117
107 118