Debian Codegraph @

xpp/xtalk/debug.c

raw
1 /*
2 * Written by Oron Peled <[email protected]>
3 * Copyright (C) 2008, Xorcom
4 *
5 * All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 */
22
23
24 #include <stdio.h>
25 #include <stdint.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <syslog.h>
29 #include <execinfo.h>
30 #include <xtalk/debug.h>
31 #include <autoconfig.h>
32
33 int verbose = LOG_INFO;
34 int debug_mask;
35
36 void log_function(int level, int mask, const char *msg, ...)
37 {
38 va_list ap;
39
40 va_start(ap, msg);
41 if (verbose >= level) {
42 if (level < LOG_DEBUG || (mask & debug_mask))
43 vfprintf(stderr, msg, ap);
44 }
45 va_end(ap);
46 }
47
48 void dump_packet(int loglevel, int mask, const char *msg,
49 const char *buf, int len)
50 {
51 int i;
52
53 if (!mask || (mask & debug_mask)) {
54 log_function(loglevel, ~0, "%-15s:", msg);
55 for (i = 0; i < len; i++)
56 log_function(loglevel, ~0, " %02X", (uint8_t)buf[i]);
57 log_function(loglevel, ~0, "\n");
58 }
59 }
60
61 /* from glibc info(1) */
62 void print_backtrace(FILE *fp)
63 {
64 void *array[10];
65 size_t size;
66 char **strings;
67 size_t i;
68
69 size = backtrace(array, 10);
70 strings = backtrace_symbols(array, size);
71 for (i = 0; i < size; i++)
72 fprintf(fp, "%s\n", strings[i]);
73 free(strings);
74 }