Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 1 | /* Debugging/Logging support code */ |
| 2 | |
| 3 | /* (C) 2008-2010 by Harald Welte <laforge@gnumonks.org> |
| 4 | * (C) 2008 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 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 along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <stdarg.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <stdio.h> |
| 26 | #include <string.h> |
| 27 | #include <strings.h> |
| 28 | #include <time.h> |
| 29 | #include <errno.h> |
| 30 | |
| 31 | #include <osmocore/talloc.h> |
| 32 | #include <osmocore/utils.h> |
| 33 | #include <osmocore/debug.h> |
| 34 | |
| 35 | static const struct debug_info *debug_info; |
| 36 | |
| 37 | static struct debug_context debug_context; |
| 38 | static void *tall_dbg_ctx = NULL; |
| 39 | static LLIST_HEAD(target_list); |
| 40 | |
| 41 | static const struct value_string loglevel_strs[] = { |
| 42 | { 0, "EVERYTHING" }, |
| 43 | { LOGL_DEBUG, "DEBUG" }, |
| 44 | { LOGL_INFO, "INFO" }, |
| 45 | { LOGL_NOTICE, "NOTICE" }, |
| 46 | { LOGL_ERROR, "ERROR" }, |
| 47 | { LOGL_FATAL, "FATAL" }, |
| 48 | { 0, NULL }, |
| 49 | }; |
| 50 | |
| 51 | int debug_parse_level(const char *lvl) |
| 52 | { |
| 53 | return get_string_value(loglevel_strs, lvl); |
| 54 | } |
| 55 | |
| 56 | int debug_parse_category(const char *category) |
| 57 | { |
| 58 | int i; |
| 59 | |
| 60 | for (i = 0; i < debug_info->num_cat; ++i) { |
| 61 | if (!strcasecmp(debug_info->cat[i].name+1, category)) |
| 62 | return debug_info->cat[i].number; |
| 63 | } |
| 64 | |
| 65 | return -EINVAL; |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * Parse the category mask. |
| 70 | * The format can be this: category1:category2:category3 |
| 71 | * or category1,2:category2,3:... |
| 72 | */ |
| 73 | void debug_parse_category_mask(struct debug_target* target, const char *_mask) |
| 74 | { |
| 75 | int i = 0; |
| 76 | char *mask = strdup(_mask); |
| 77 | char *category_token = NULL; |
| 78 | |
| 79 | /* Disable everything to enable it afterwards */ |
| 80 | for (i = 0; i < ARRAY_SIZE(target->categories); ++i) |
| 81 | target->categories[i].enabled = 0; |
| 82 | |
| 83 | category_token = strtok(mask, ":"); |
| 84 | do { |
| 85 | for (i = 0; i < debug_info->num_cat; ++i) { |
| 86 | char* colon = strstr(category_token, ","); |
| 87 | int length = strlen(category_token); |
| 88 | |
| 89 | if (colon) |
| 90 | length = colon - category_token; |
| 91 | |
| 92 | if (strncasecmp(debug_info->cat[i].name, category_token, |
| 93 | length) == 0) { |
| 94 | int number = debug_info->cat[i].number; |
| 95 | int level = 0; |
| 96 | |
| 97 | if (colon) |
| 98 | level = atoi(colon+1); |
| 99 | |
| 100 | target->categories[number].enabled = 1; |
| 101 | target->categories[number].loglevel = level; |
| 102 | } |
| 103 | } |
| 104 | } while ((category_token = strtok(NULL, ":"))); |
| 105 | |
| 106 | free(mask); |
| 107 | } |
| 108 | |
| 109 | static const char* color(int subsys) |
| 110 | { |
| 111 | int i = 0; |
| 112 | |
| 113 | for (i = 0; i < debug_info->num_cat; ++i) { |
| 114 | if (debug_info->cat[i].number == subsys) |
| 115 | return debug_info->cat[i].color; |
| 116 | } |
| 117 | |
Harald Welte | d788f66 | 2010-03-26 09:45:03 +0800 | [diff] [blame^] | 118 | return NULL; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static void _output(struct debug_target *target, unsigned int subsys, |
| 122 | char *file, int line, int cont, const char *format, |
| 123 | va_list ap) |
| 124 | { |
| 125 | char col[30]; |
| 126 | char sub[30]; |
| 127 | char tim[30]; |
| 128 | char buf[4096]; |
| 129 | char final[4096]; |
| 130 | |
| 131 | /* prepare the data */ |
| 132 | col[0] = '\0'; |
| 133 | sub[0] = '\0'; |
| 134 | tim[0] = '\0'; |
| 135 | buf[0] = '\0'; |
| 136 | |
| 137 | /* are we using color */ |
| 138 | if (target->use_color) { |
Harald Welte | d788f66 | 2010-03-26 09:45:03 +0800 | [diff] [blame^] | 139 | const char *c = color(subsys); |
| 140 | if (c) { |
| 141 | snprintf(col, sizeof(col), "%s", color(subsys)); |
| 142 | col[sizeof(col)-1] = '\0'; |
| 143 | } |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 144 | } |
| 145 | vsnprintf(buf, sizeof(buf), format, ap); |
| 146 | buf[sizeof(buf)-1] = '\0'; |
| 147 | |
| 148 | if (!cont) { |
| 149 | if (target->print_timestamp) { |
| 150 | char *timestr; |
| 151 | time_t tm; |
| 152 | tm = time(NULL); |
| 153 | timestr = ctime(&tm); |
| 154 | timestr[strlen(timestr)-1] = '\0'; |
| 155 | snprintf(tim, sizeof(tim), "%s ", timestr); |
| 156 | tim[sizeof(tim)-1] = '\0'; |
| 157 | } |
| 158 | snprintf(sub, sizeof(sub), "<%4.4x> %s:%d ", subsys, file, line); |
| 159 | sub[sizeof(sub)-1] = '\0'; |
| 160 | } |
| 161 | |
| 162 | snprintf(final, sizeof(final), "%s%s%s%s\033[0;m", col, tim, sub, buf); |
| 163 | final[sizeof(final)-1] = '\0'; |
| 164 | target->output(target, final); |
| 165 | } |
| 166 | |
| 167 | |
| 168 | static void _debugp(unsigned int subsys, int level, char *file, int line, |
| 169 | int cont, const char *format, va_list ap) |
| 170 | { |
| 171 | struct debug_target *tar; |
| 172 | |
| 173 | llist_for_each_entry(tar, &target_list, entry) { |
| 174 | struct debug_category *category; |
| 175 | int output = 0; |
| 176 | |
| 177 | category = &tar->categories[subsys]; |
| 178 | /* subsystem is not supposed to be debugged */ |
| 179 | if (!category->enabled) |
| 180 | continue; |
| 181 | |
| 182 | /* Check the global log level */ |
| 183 | if (tar->loglevel != 0 && level < tar->loglevel) |
| 184 | continue; |
| 185 | |
| 186 | /* Check the category log level */ |
| 187 | if (tar->loglevel == 0 && category->loglevel != 0 && |
| 188 | level < category->loglevel) |
| 189 | continue; |
| 190 | |
| 191 | /* Apply filters here... if that becomes messy we will |
| 192 | * need to put filters in a list and each filter will |
| 193 | * say stop, continue, output */ |
| 194 | if ((tar->filter_map & DEBUG_FILTER_ALL) != 0) |
| 195 | output = 1; |
| 196 | else if (debug_info->filter_fn) |
| 197 | output = debug_info->filter_fn(&debug_context, |
| 198 | tar); |
| 199 | |
| 200 | if (output) { |
| 201 | /* FIXME: copying the va_list is an ugly |
| 202 | * workaround against a bug hidden somewhere in |
| 203 | * _output. If we do not copy here, the first |
| 204 | * call to _output() will corrupt the va_list |
| 205 | * contents, and any further _output() calls |
| 206 | * with the same va_list will segfault */ |
| 207 | va_list bp; |
| 208 | va_copy(bp, ap); |
| 209 | _output(tar, subsys, file, line, cont, format, bp); |
| 210 | va_end(bp); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | void debugp(unsigned int subsys, char *file, int line, int cont, |
| 216 | const char *format, ...) |
| 217 | { |
| 218 | va_list ap; |
| 219 | |
| 220 | va_start(ap, format); |
| 221 | _debugp(subsys, LOGL_DEBUG, file, line, cont, format, ap); |
| 222 | va_end(ap); |
| 223 | } |
| 224 | |
| 225 | void debugp2(unsigned int subsys, unsigned int level, char *file, int line, int cont, const char *format, ...) |
| 226 | { |
| 227 | va_list ap; |
| 228 | |
| 229 | va_start(ap, format); |
| 230 | _debugp(subsys, level, file, line, cont, format, ap); |
| 231 | va_end(ap); |
| 232 | } |
| 233 | |
| 234 | static char hexd_buff[4096]; |
| 235 | |
| 236 | char *hexdump(const unsigned char *buf, int len) |
| 237 | { |
| 238 | int i; |
| 239 | char *cur = hexd_buff; |
| 240 | |
| 241 | hexd_buff[0] = 0; |
| 242 | for (i = 0; i < len; i++) { |
| 243 | int len_remain = sizeof(hexd_buff) - (cur - hexd_buff); |
| 244 | int rc = snprintf(cur, len_remain, "%02x ", buf[i]); |
| 245 | if (rc <= 0) |
| 246 | break; |
| 247 | cur += rc; |
| 248 | } |
| 249 | hexd_buff[sizeof(hexd_buff)-1] = 0; |
| 250 | return hexd_buff; |
| 251 | } |
| 252 | |
| 253 | void debug_add_target(struct debug_target *target) |
| 254 | { |
| 255 | llist_add_tail(&target->entry, &target_list); |
| 256 | } |
| 257 | |
| 258 | void debug_del_target(struct debug_target *target) |
| 259 | { |
| 260 | llist_del(&target->entry); |
| 261 | } |
| 262 | |
| 263 | void debug_reset_context(void) |
| 264 | { |
| 265 | memset(&debug_context, 0, sizeof(debug_context)); |
| 266 | } |
| 267 | |
| 268 | int debug_set_context(uint8_t ctx_nr, void *value) |
| 269 | { |
| 270 | if (ctx_nr > DEBUG_MAX_CTX) |
| 271 | return -EINVAL; |
| 272 | |
| 273 | debug_context.ctx[ctx_nr] = value; |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | void debug_set_all_filter(struct debug_target *target, int all) |
| 279 | { |
| 280 | if (all) |
| 281 | target->filter_map |= DEBUG_FILTER_ALL; |
| 282 | else |
| 283 | target->filter_map &= ~DEBUG_FILTER_ALL; |
| 284 | } |
| 285 | |
| 286 | void debug_set_use_color(struct debug_target *target, int use_color) |
| 287 | { |
| 288 | target->use_color = use_color; |
| 289 | } |
| 290 | |
| 291 | void debug_set_print_timestamp(struct debug_target *target, int print_timestamp) |
| 292 | { |
| 293 | target->print_timestamp = print_timestamp; |
| 294 | } |
| 295 | |
| 296 | void debug_set_log_level(struct debug_target *target, int log_level) |
| 297 | { |
| 298 | target->loglevel = log_level; |
| 299 | } |
| 300 | |
| 301 | void debug_set_category_filter(struct debug_target *target, int category, |
| 302 | int enable, int level) |
| 303 | { |
| 304 | if (category >= debug_info->num_cat) |
| 305 | return; |
| 306 | target->categories[category].enabled = !!enable; |
| 307 | target->categories[category].loglevel = level; |
| 308 | } |
| 309 | |
| 310 | static void _stderr_output(struct debug_target *target, const char *log) |
| 311 | { |
| 312 | fprintf(target->tgt_stdout.out, "%s", log); |
| 313 | fflush(target->tgt_stdout.out); |
| 314 | } |
| 315 | |
| 316 | struct debug_target *debug_target_create(void) |
| 317 | { |
| 318 | struct debug_target *target; |
| 319 | |
| 320 | target = talloc_zero(tall_dbg_ctx, struct debug_target); |
| 321 | if (!target) |
| 322 | return NULL; |
| 323 | |
| 324 | INIT_LLIST_HEAD(&target->entry); |
| 325 | memcpy(target->categories, debug_info->cat, |
| 326 | sizeof(struct debug_category)*debug_info->num_cat); |
| 327 | target->use_color = 1; |
| 328 | target->print_timestamp = 0; |
| 329 | target->loglevel = 0; |
| 330 | return target; |
| 331 | } |
| 332 | |
| 333 | struct debug_target *debug_target_create_stderr(void) |
| 334 | { |
| 335 | struct debug_target *target; |
| 336 | |
| 337 | target = debug_target_create(); |
| 338 | if (!target) |
| 339 | return NULL; |
| 340 | |
| 341 | target->tgt_stdout.out = stderr; |
| 342 | target->output = _stderr_output; |
| 343 | return target; |
| 344 | } |
| 345 | |
| 346 | void debug_init(const struct debug_info *cat) |
| 347 | { |
| 348 | tall_dbg_ctx = talloc_named_const(NULL, 1, "debug"); |
| 349 | debug_info = cat; |
| 350 | } |