Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1 | /* Debugging/Logging support code */ |
| 2 | /* (C) 2008 by Harald Welte <laforge@gnumonks.org> |
| 3 | * (C) 2008 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 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> |
| 23 | #include <stdlib.h> |
| 24 | #include <stdio.h> |
| 25 | #include <string.h> |
| 26 | #include <strings.h> |
| 27 | #include <time.h> |
| 28 | |
| 29 | #include <openbsc/debug.h> |
| 30 | |
Harald Welte | 0299368 | 2009-06-27 02:53:10 +0200 | [diff] [blame] | 31 | unsigned int debug_mask = 0xffffffff & ~(DMI|DMIB|DMEAS); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 32 | |
| 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[] = { |
| 46 | DEBUG_CATEGORY(DRLL, "DRLL", "\033[1;31m", "") |
| 47 | DEBUG_CATEGORY(DCC, "DCC", "\033[1;32m", "") |
| 48 | DEBUG_CATEGORY(DMM, "DMM", "\033[1;33m", "") |
| 49 | DEBUG_CATEGORY(DRR, "DRR", "\033[1;34m", "") |
| 50 | DEBUG_CATEGORY(DRSL, "DRSL", "\033[1;35m", "") |
| 51 | DEBUG_CATEGORY(DNM, "DNM", "\033[1;36m", "") |
| 52 | DEBUG_CATEGORY(DSMS, "DSMS", "\033[1;37m", "") |
| 53 | DEBUG_CATEGORY(DPAG, "DPAG", "\033[1;38m", "") |
| 54 | DEBUG_CATEGORY(DMNCC, "DMNCC","\033[1;39m", "") |
| 55 | DEBUG_CATEGORY(DINP, "DINP", "", "") |
| 56 | DEBUG_CATEGORY(DMI, "DMI", "", "") |
| 57 | DEBUG_CATEGORY(DMIB, "DMIB", "", "") |
| 58 | DEBUG_CATEGORY(DMUX, "DMUX", "", "") |
Harald Welte | 0299368 | 2009-06-27 02:53:10 +0200 | [diff] [blame] | 59 | DEBUG_CATEGORY(DMEAS, "DMEAS", "", "") |
Holger Hans Peter Freyther | b9cf781 | 2009-08-01 16:54:45 +0200 | [diff] [blame] | 60 | DEBUG_CATEGORY(DSCCP, "DSCCP", "", "") |
Holger Hans Peter Freyther | acf289e | 2009-08-18 12:54:50 +0200 | [diff] [blame] | 61 | DEBUG_CATEGORY(DMSC, "DMSC", "", "") |
Holger Hans Peter Freyther | 66f78e6 | 2009-11-20 13:05:48 +0100 | [diff] [blame] | 62 | DEBUG_CATEGORY(DMGCP, "DMGCP", "", "") |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | static int use_color = 1; |
| 66 | |
| 67 | void debug_use_color(int color) |
| 68 | { |
| 69 | use_color = color; |
| 70 | } |
| 71 | |
| 72 | static int print_timestamp = 0; |
| 73 | |
| 74 | void debug_timestamp(int enable) |
| 75 | { |
| 76 | print_timestamp = enable; |
| 77 | } |
| 78 | |
| 79 | |
| 80 | /* |
| 81 | * Parse the category mask. |
| 82 | * category1:category2:category3 |
| 83 | */ |
| 84 | void debug_parse_category_mask(const char *_mask) |
| 85 | { |
| 86 | unsigned int new_mask = 0; |
| 87 | int i = 0; |
| 88 | char *mask = strdup(_mask); |
| 89 | char *category_token = NULL; |
| 90 | |
| 91 | category_token = strtok(mask, ":"); |
| 92 | do { |
| 93 | for (i = 0; i < ARRAY_SIZE(debug_info); ++i) { |
| 94 | if (strcasecmp(debug_info[i].name, category_token) == 0) |
| 95 | new_mask |= debug_info[i].number; |
| 96 | } |
| 97 | } while ((category_token = strtok(NULL, ":"))); |
| 98 | |
| 99 | |
| 100 | free(mask); |
| 101 | debug_mask = new_mask; |
| 102 | } |
| 103 | |
| 104 | const char* color(int subsys) |
| 105 | { |
| 106 | int i = 0; |
| 107 | |
| 108 | for (i = 0; use_color && i < ARRAY_SIZE(debug_info); ++i) { |
| 109 | if (debug_info[i].number == subsys) |
| 110 | return debug_info[i].color; |
| 111 | } |
| 112 | |
| 113 | return ""; |
| 114 | } |
| 115 | |
| 116 | void debugp(unsigned int subsys, char *file, int line, int cont, const char *format, ...) |
| 117 | { |
| 118 | va_list ap; |
| 119 | FILE *outfd = stderr; |
| 120 | |
| 121 | if (!(debug_mask & subsys)) |
| 122 | return; |
| 123 | |
| 124 | va_start(ap, format); |
| 125 | |
| 126 | fprintf(outfd, "%s", color(subsys)); |
| 127 | |
| 128 | if (!cont) { |
| 129 | if (print_timestamp) { |
| 130 | char *timestr; |
| 131 | time_t tm; |
| 132 | tm = time(NULL); |
| 133 | timestr = ctime(&tm); |
| 134 | timestr[strlen(timestr)-1] = '\0'; |
| 135 | fprintf(outfd, "%s ", timestr); |
| 136 | } |
| 137 | fprintf(outfd, "<%4.4x> %s:%d ", subsys, file, line); |
| 138 | } |
| 139 | vfprintf(outfd, format, ap); |
| 140 | fprintf(outfd, "\033[0;m"); |
| 141 | |
| 142 | va_end(ap); |
| 143 | |
| 144 | fflush(outfd); |
| 145 | } |
| 146 | |
| 147 | static char hexd_buff[4096]; |
| 148 | |
Holger Hans Peter Freyther | 26d15d1 | 2009-08-20 13:16:26 +0200 | [diff] [blame] | 149 | char *hexdump(const unsigned char *buf, int len) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 150 | { |
| 151 | int i; |
| 152 | char *cur = hexd_buff; |
| 153 | |
| 154 | hexd_buff[0] = 0; |
| 155 | for (i = 0; i < len; i++) { |
| 156 | int len_remain = sizeof(hexd_buff) - (cur - hexd_buff); |
| 157 | int rc = snprintf(cur, len_remain, "%02x ", buf[i]); |
| 158 | if (rc <= 0) |
| 159 | break; |
| 160 | cur += rc; |
| 161 | } |
| 162 | hexd_buff[sizeof(hexd_buff)-1] = 0; |
| 163 | return hexd_buff; |
| 164 | } |
| 165 | |