Mercurial > libpst
comparison src/debug.c @ 172:6954d315aaa8
move version-info into main configure.in, and set it properly.
prefix all external symbols in the shared library with pst_ to avoid symbol clashes with other shared libraries.
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Sat, 04 Apr 2009 16:00:48 -0700 |
parents | fc11b1d1ad34 |
children | ac6e22c8a9cf |
comparison
equal
deleted
inserted
replaced
171:6c1e75bc4cac | 172:6954d315aaa8 |
---|---|
5 char * function; | 5 char * function; |
6 unsigned int line; | 6 unsigned int line; |
7 char * file; | 7 char * file; |
8 char * text; | 8 char * text; |
9 struct pst_debug_item *next; | 9 struct pst_debug_item *next; |
10 } *item_head=NULL, *item_tail=NULL, *item_ptr=NULL, *info_ptr=NULL, *temp_list=NULL; | 10 }; |
11 | |
12 static struct pst_debug_item *item_head=NULL, *item_tail=NULL, *item_ptr=NULL, *info_ptr=NULL, *temp_list=NULL; | |
11 | 13 |
12 | 14 |
13 struct pst_debug_func { | 15 struct pst_debug_func { |
14 char * name; | 16 char * name; |
15 struct pst_debug_func *next; | 17 struct pst_debug_func *next; |
16 } *func_head=NULL, *func_ptr=NULL; | 18 }; |
19 | |
20 static struct pst_debug_func *func_head=NULL, *func_ptr=NULL; | |
17 | 21 |
18 | 22 |
19 void pst_debug_write_msg(struct pst_debug_item *item, const char *fmt, va_list *ap, int size); | 23 void pst_debug_write_msg(struct pst_debug_item *item, const char *fmt, va_list *ap, int size); |
20 void pst_debug_write_hex(struct pst_debug_item *item, char *buf, size_t size, int col); | 24 void pst_debug_write_hex(struct pst_debug_item *item, char *buf, size_t size, int col); |
21 void * xmalloc(size_t size); | 25 void * pst_malloc(size_t size); |
22 | 26 |
23 size_t pst_debug_fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream) { | 27 size_t pst_debug_fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream) { |
24 return fwrite(ptr, size, nitems, stream); | 28 return fwrite(ptr, size, nitems, stream); |
25 } | 29 } |
26 | 30 |
72 | 76 |
73 fprintf(out, "\n"); | 77 fprintf(out, "\n"); |
74 } | 78 } |
75 | 79 |
76 | 80 |
77 FILE *debug_fp = NULL; | 81 static FILE *debug_fp = NULL; |
78 unsigned int max_items=DEBUG_MAX_ITEMS, curr_items=0; | 82 static unsigned int max_items=DEBUG_MAX_ITEMS, curr_items=0; |
79 | 83 |
80 | 84 |
81 void pst_debug_init(const char* fname) { | 85 void pst_debug_init(const char* fname) { |
82 unsigned char version = DEBUG_VERSION; | 86 unsigned char version = DEBUG_VERSION; |
83 item_head = item_tail = NULL; | 87 item_head = item_tail = NULL; |
95 // function must be called before pst_debug_msg. It sets up the | 99 // function must be called before pst_debug_msg. It sets up the |
96 // structure for the function that follows | 100 // structure for the function that follows |
97 void pst_debug_msg_info(int line, const char* file, int type) { | 101 void pst_debug_msg_info(int line, const char* file, int type) { |
98 char *x; | 102 char *x; |
99 if (!debug_fp) return; // no file | 103 if (!debug_fp) return; // no file |
100 info_ptr = (struct pst_debug_item*) xmalloc(sizeof(struct pst_debug_item)); | 104 info_ptr = (struct pst_debug_item*) pst_malloc(sizeof(struct pst_debug_item)); |
101 info_ptr->type = type; | 105 info_ptr->type = type; |
102 info_ptr->line = line; | 106 info_ptr->line = line; |
103 x = (func_head==NULL?"No Function":func_head->name); | 107 x = (func_head==NULL?"No Function":func_head->name); |
104 info_ptr->function = (char*) xmalloc(strlen(x)+1); | 108 info_ptr->function = (char*) pst_malloc(strlen(x)+1); |
105 strcpy(info_ptr->function, x); | 109 strcpy(info_ptr->function, x); |
106 | 110 |
107 info_ptr->file = (char*) xmalloc(strlen(file)+1); | 111 info_ptr->file = (char*) pst_malloc(strlen(file)+1); |
108 strcpy(info_ptr->file, file); | 112 strcpy(info_ptr->file, file); |
109 | 113 |
110 //put the current record on a temp linked list | 114 //put the current record on a temp linked list |
111 info_ptr->next = temp_list; | 115 info_ptr->next = temp_list; |
112 temp_list = info_ptr; | 116 temp_list = info_ptr; |
150 f = vsnprintf(x, 1, fmt, ap); | 154 f = vsnprintf(x, 1, fmt, ap); |
151 va_end(ap); | 155 va_end(ap); |
152 #endif | 156 #endif |
153 | 157 |
154 if (f > 0 && f < MAX_MESSAGE_SIZE) { | 158 if (f > 0 && f < MAX_MESSAGE_SIZE) { |
155 info_ptr->text = (char*) xmalloc(f+1); | 159 info_ptr->text = (char*) pst_malloc(f+1); |
156 va_start(ap, fmt); | 160 va_start(ap, fmt); |
157 if ((g = vsnprintf(info_ptr->text, f, fmt, ap)) == -1) { | 161 if ((g = vsnprintf(info_ptr->text, f, fmt, ap)) == -1) { |
158 fprintf(stderr, "_debug_msg: Dying! vsnprintf returned -1 for format \"%s\"\n", fmt); | 162 fprintf(stderr, "_debug_msg: Dying! vsnprintf returned -1 for format \"%s\"\n", fmt); |
159 exit(-2); | 163 exit(-2); |
160 } | 164 } |
210 info_ptr = NULL; | 214 info_ptr = NULL; |
211 } | 215 } |
212 | 216 |
213 | 217 |
214 void pst_debug_func(const char *function) { | 218 void pst_debug_func(const char *function) { |
215 func_ptr = xmalloc (sizeof(struct pst_debug_func)); | 219 func_ptr = pst_malloc (sizeof(struct pst_debug_func)); |
216 func_ptr->name = xmalloc(strlen(function)+1); | 220 func_ptr->name = pst_malloc(strlen(function)+1); |
217 strcpy(func_ptr->name, function); | 221 strcpy(func_ptr->name, function); |
218 func_ptr->next = func_head; | 222 func_ptr->next = func_head; |
219 func_head = func_ptr; | 223 func_head = func_ptr; |
220 } | 224 } |
221 | 225 |
260 struct pst_debug_file_rec_m mfile_rec; | 264 struct pst_debug_file_rec_m mfile_rec; |
261 struct pst_debug_file_rec_l lfile_rec; | 265 struct pst_debug_file_rec_l lfile_rec; |
262 | 266 |
263 if (curr_items == 0) return; // no items to write. | 267 if (curr_items == 0) return; // no items to write. |
264 | 268 |
265 index = (int64_t*)xmalloc(index_size); | 269 index = (int64_t*)pst_malloc(index_size); |
266 memset(index, 0, index_size); // valgrind, avoid writing uninitialized data | 270 memset(index, 0, index_size); // valgrind, avoid writing uninitialized data |
267 file_pos += index_size; | 271 file_pos += index_size; |
268 // write the index first, we will re-write it later, but | 272 // write the index first, we will re-write it later, but |
269 // we want to allocate the space | 273 // we want to allocate the space |
270 pst_debug_fwrite(index, index_size, 1, debug_fp); | 274 pst_debug_fwrite(index, index_size, 1, debug_fp); |
276 index[index_ptr++] = file_pos; | 280 index[index_ptr++] = file_pos; |
277 size = strlen(item_ptr->function) + | 281 size = strlen(item_ptr->function) + |
278 strlen(item_ptr->file) + | 282 strlen(item_ptr->file) + |
279 strlen(item_ptr->text) + 3; //for the three \0s | 283 strlen(item_ptr->text) + 3; //for the three \0s |
280 if (buf) free(buf); | 284 if (buf) free(buf); |
281 buf = xmalloc(size+1); | 285 buf = pst_malloc(size+1); |
282 ptr = 0; | 286 ptr = 0; |
283 funcname=ptr; | 287 funcname=ptr; |
284 ptr += sprintf(&(buf[ptr]), "%s", item_ptr->function)+1; | 288 ptr += sprintf(&(buf[ptr]), "%s", item_ptr->function)+1; |
285 filename=ptr; | 289 filename=ptr; |
286 ptr += sprintf(&(buf[ptr]), "%s", item_ptr->file)+1; | 290 ptr += sprintf(&(buf[ptr]), "%s", item_ptr->file)+1; |
433 pst_debug_fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp); | 437 pst_debug_fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp); |
434 fseeko(debug_fp, 0, SEEK_END); | 438 fseeko(debug_fp, 0, SEEK_END); |
435 } | 439 } |
436 | 440 |
437 | 441 |
438 void *xmalloc(size_t size) { | 442 void *pst_malloc(size_t size) { |
439 void *mem = malloc(size); | 443 void *mem = malloc(size); |
440 if (!mem) { | 444 if (!mem) { |
441 fprintf(stderr, "xMalloc: Out Of memory [req: %ld]\n", (long)size); | 445 fprintf(stderr, "pst_malloc: Out Of memory [req: %ld]\n", (long)size); |
442 exit(1); | 446 exit(1); |
443 } | 447 } |
444 return mem; | 448 return mem; |
445 } | 449 } |
446 | 450 |