Holger Freyther | 32636e8 | 2008-12-27 11:07:15 +0000 | [diff] [blame] | 1 | /* Debugging/Logging support code */ |
| 2 | /* (C) 2008 by Harald Welte <laforge@gnumonks.org> |
Holger Freyther | d546e31 | 2008-12-27 12:03:07 +0000 | [diff] [blame] | 3 | * (C) 2008 by Holger Hans Peter Freyther <zecke@selfish.org> |
Holger Freyther | 32636e8 | 2008-12-27 11:07:15 +0000 | [diff] [blame] | 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <stdarg.h> |
Holger Freyther | d546e31 | 2008-12-27 12:03:07 +0000 | [diff] [blame] | 23 | #include <stdlib.h> |
Holger Freyther | 32636e8 | 2008-12-27 11:07:15 +0000 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <string.h> |
Holger Freyther | d546e31 | 2008-12-27 12:03:07 +0000 | [diff] [blame] | 26 | #include <strings.h> |
Holger Freyther | 32636e8 | 2008-12-27 11:07:15 +0000 | [diff] [blame] | 27 | #include <time.h> |
| 28 | |
| 29 | #include <openbsc/debug.h> |
| 30 | |
Harald Welte | 5e68183 | 2009-12-20 00:06:02 +0100 | [diff] [blame] | 31 | unsigned int debug_mask = 0xffffffff & ~(DMI|DMIB|DMEAS); |
Holger Freyther | 32636e8 | 2008-12-27 11:07:15 +0000 | [diff] [blame] | 32 | |
Holger Freyther | d546e31 | 2008-12-27 12:03:07 +0000 | [diff] [blame] | 33 | struct debug_info { |
| 34 | const char *name; |
| 35 | const char *color; |
| 36 | const char *description; |
| 37 | int number; |
| 38 | }; |
| 39 | |
| 40 | #define DEBUG_CATEGORY(NUMBER, NAME, COLOR, DESCRIPTION) \ |
| 41 | { .name = NAME, .color = COLOR, .description = DESCRIPTION, .number = NUMBER }, |
| 42 | |
| 43 | #define ARRAY_SIZE(array) (sizeof(array)/sizeof(array[0])) |
| 44 | |
| 45 | static const struct debug_info debug_info[] = { |
Holger Freyther | 7c03e4c | 2008-12-27 12:46:48 +0000 | [diff] [blame] | 46 | DEBUG_CATEGORY(DRLL, "DRLL", "\033[1;31m", "") |
| 47 | DEBUG_CATEGORY(DCC, "DCC", "\033[1;32m", "") |
Holger Freyther | 3770b76 | 2009-06-02 02:35:12 +0000 | [diff] [blame] | 48 | DEBUG_CATEGORY(DMM, "DMM", "\033[1;33m", "") |
Holger Freyther | 7c03e4c | 2008-12-27 12:46:48 +0000 | [diff] [blame] | 49 | DEBUG_CATEGORY(DRR, "DRR", "\033[1;34m", "") |
Harald Welte | c1d2aae | 2009-05-23 06:43:35 +0000 | [diff] [blame] | 50 | DEBUG_CATEGORY(DRSL, "DRSL", "\033[1;35m", "") |
Holger Freyther | 7c03e4c | 2008-12-27 12:46:48 +0000 | [diff] [blame] | 51 | DEBUG_CATEGORY(DNM, "DNM", "\033[1;36m", "") |
Daniel Willmann | 10d06f6 | 2008-12-28 21:38:25 +0000 | [diff] [blame] | 52 | DEBUG_CATEGORY(DSMS, "DSMS", "\033[1;37m", "") |
Harald Welte | d35b6a7 | 2008-12-29 04:06:41 +0000 | [diff] [blame] | 53 | DEBUG_CATEGORY(DPAG, "DPAG", "\033[1;38m", "") |
Harald Welte | c125a68 | 2009-05-23 06:42:38 +0000 | [diff] [blame] | 54 | DEBUG_CATEGORY(DMNCC, "DMNCC","\033[1;39m", "") |
Harald Welte | edb3778 | 2009-05-01 14:59:07 +0000 | [diff] [blame] | 55 | DEBUG_CATEGORY(DINP, "DINP", "", "") |
Harald Welte | b60fa59 | 2009-02-06 12:02:53 +0000 | [diff] [blame] | 56 | DEBUG_CATEGORY(DMI, "DMI", "", "") |
| 57 | DEBUG_CATEGORY(DMIB, "DMIB", "", "") |
Harald Welte | ba59baf | 2009-02-23 00:04:04 +0000 | [diff] [blame] | 58 | DEBUG_CATEGORY(DMUX, "DMUX", "", "") |
Harald Welte | 10d0e67 | 2009-06-27 02:53:10 +0200 | [diff] [blame] | 59 | DEBUG_CATEGORY(DMEAS, "DMEAS", "", "") |
Holger Hans Peter Freyther | ed0a47b | 2009-08-01 16:54:45 +0200 | [diff] [blame] | 60 | DEBUG_CATEGORY(DSCCP, "DSCCP", "", "") |
Holger Hans Peter Freyther | 32201c5 | 2009-08-18 12:54:50 +0200 | [diff] [blame] | 61 | DEBUG_CATEGORY(DMSC, "DMSC", "", "") |
Holger Hans Peter Freyther | ff5fa4e | 2009-11-20 13:05:48 +0100 | [diff] [blame] | 62 | DEBUG_CATEGORY(DMGCP, "DMGCP", "", "") |
Harald Welte | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 63 | DEBUG_CATEGORY(DHO, "DHO", "", "") |
Holger Freyther | d546e31 | 2008-12-27 12:03:07 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
Holger Freyther | 7c03e4c | 2008-12-27 12:46:48 +0000 | [diff] [blame] | 66 | static int use_color = 1; |
| 67 | |
Holger Freyther | b332f61 | 2008-12-27 12:46:51 +0000 | [diff] [blame] | 68 | void debug_use_color(int color) |
| 69 | { |
| 70 | use_color = color; |
| 71 | } |
| 72 | |
Harald Welte | d3ff51d | 2009-06-09 20:21:57 +0000 | [diff] [blame] | 73 | static int print_timestamp = 0; |
| 74 | |
| 75 | void debug_timestamp(int enable) |
| 76 | { |
| 77 | print_timestamp = enable; |
| 78 | } |
| 79 | |
| 80 | |
Holger Freyther | d546e31 | 2008-12-27 12:03:07 +0000 | [diff] [blame] | 81 | /* |
| 82 | * Parse the category mask. |
| 83 | * category1:category2:category3 |
| 84 | */ |
Holger Freyther | 5ee72ee | 2008-12-27 12:46:49 +0000 | [diff] [blame] | 85 | void debug_parse_category_mask(const char *_mask) |
Holger Freyther | d546e31 | 2008-12-27 12:03:07 +0000 | [diff] [blame] | 86 | { |
| 87 | unsigned int new_mask = 0; |
| 88 | int i = 0; |
| 89 | char *mask = strdup(_mask); |
| 90 | char *category_token = NULL; |
| 91 | |
| 92 | category_token = strtok(mask, ":"); |
| 93 | do { |
| 94 | for (i = 0; i < ARRAY_SIZE(debug_info); ++i) { |
| 95 | if (strcasecmp(debug_info[i].name, category_token) == 0) |
| 96 | new_mask |= debug_info[i].number; |
| 97 | } |
Holger Freyther | ca362a6 | 2009-01-04 21:05:01 +0000 | [diff] [blame] | 98 | } while ((category_token = strtok(NULL, ":"))); |
Holger Freyther | d546e31 | 2008-12-27 12:03:07 +0000 | [diff] [blame] | 99 | |
| 100 | |
| 101 | free(mask); |
| 102 | debug_mask = new_mask; |
| 103 | } |
| 104 | |
Holger Freyther | 7c03e4c | 2008-12-27 12:46:48 +0000 | [diff] [blame] | 105 | const char* color(int subsys) |
| 106 | { |
| 107 | int i = 0; |
| 108 | |
| 109 | for (i = 0; use_color && i < ARRAY_SIZE(debug_info); ++i) { |
| 110 | if (debug_info[i].number == subsys) |
| 111 | return debug_info[i].color; |
| 112 | } |
| 113 | |
| 114 | return ""; |
| 115 | } |
| 116 | |
Harald Welte | 6ddd168 | 2009-02-06 12:38:29 +0000 | [diff] [blame] | 117 | void debugp(unsigned int subsys, char *file, int line, int cont, const char *format, ...) |
Holger Freyther | 32636e8 | 2008-12-27 11:07:15 +0000 | [diff] [blame] | 118 | { |
Holger Freyther | 32636e8 | 2008-12-27 11:07:15 +0000 | [diff] [blame] | 119 | va_list ap; |
Holger Freyther | 32636e8 | 2008-12-27 11:07:15 +0000 | [diff] [blame] | 120 | FILE *outfd = stderr; |
| 121 | |
| 122 | if (!(debug_mask & subsys)) |
| 123 | return; |
| 124 | |
| 125 | va_start(ap, format); |
| 126 | |
Harald Welte | 6ddd168 | 2009-02-06 12:38:29 +0000 | [diff] [blame] | 127 | fprintf(outfd, "%s", color(subsys)); |
| 128 | |
| 129 | if (!cont) { |
Harald Welte | d3ff51d | 2009-06-09 20:21:57 +0000 | [diff] [blame] | 130 | if (print_timestamp) { |
| 131 | char *timestr; |
| 132 | time_t tm; |
| 133 | tm = time(NULL); |
| 134 | timestr = ctime(&tm); |
| 135 | timestr[strlen(timestr)-1] = '\0'; |
| 136 | fprintf(outfd, "%s ", timestr); |
| 137 | } |
| 138 | fprintf(outfd, "<%4.4x> %s:%d ", subsys, file, line); |
Harald Welte | 6ddd168 | 2009-02-06 12:38:29 +0000 | [diff] [blame] | 139 | } |
Holger Freyther | 32636e8 | 2008-12-27 11:07:15 +0000 | [diff] [blame] | 140 | vfprintf(outfd, format, ap); |
Holger Freyther | 7c03e4c | 2008-12-27 12:46:48 +0000 | [diff] [blame] | 141 | fprintf(outfd, "\033[0;m"); |
Holger Freyther | 32636e8 | 2008-12-27 11:07:15 +0000 | [diff] [blame] | 142 | |
| 143 | va_end(ap); |
| 144 | |
| 145 | fflush(outfd); |
| 146 | } |
| 147 | |
Harald Welte | 3cc4bf5 | 2009-02-28 13:08:01 +0000 | [diff] [blame] | 148 | static char hexd_buff[4096]; |
| 149 | |
Holger Hans Peter Freyther | e78074e | 2009-08-20 13:16:26 +0200 | [diff] [blame] | 150 | char *hexdump(const unsigned char *buf, int len) |
Holger Freyther | ca362a6 | 2009-01-04 21:05:01 +0000 | [diff] [blame] | 151 | { |
| 152 | int i; |
Harald Welte | 3cc4bf5 | 2009-02-28 13:08:01 +0000 | [diff] [blame] | 153 | char *cur = hexd_buff; |
| 154 | |
| 155 | hexd_buff[0] = 0; |
Holger Freyther | ca362a6 | 2009-01-04 21:05:01 +0000 | [diff] [blame] | 156 | for (i = 0; i < len; i++) { |
Harald Welte | 3cc4bf5 | 2009-02-28 13:08:01 +0000 | [diff] [blame] | 157 | int len_remain = sizeof(hexd_buff) - (cur - hexd_buff); |
| 158 | int rc = snprintf(cur, len_remain, "%02x ", buf[i]); |
| 159 | if (rc <= 0) |
| 160 | break; |
| 161 | cur += rc; |
Holger Freyther | ca362a6 | 2009-01-04 21:05:01 +0000 | [diff] [blame] | 162 | } |
Harald Welte | 3cc4bf5 | 2009-02-28 13:08:01 +0000 | [diff] [blame] | 163 | hexd_buff[sizeof(hexd_buff)-1] = 0; |
| 164 | return hexd_buff; |
Holger Freyther | ca362a6 | 2009-01-04 21:05:01 +0000 | [diff] [blame] | 165 | } |
| 166 | |