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 | e9a8261 | 2008-12-27 16:45:29 +0000 | [diff] [blame^] | 31 | static unsigned int debug_mask = 0xffffffff & ~(DMI|DMIB); |
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", "") |
| 48 | DEBUG_CATEGORY(DNM, "DMM", "\033[1;33m", "") |
| 49 | DEBUG_CATEGORY(DRR, "DRR", "\033[1;34m", "") |
| 50 | DEBUG_CATEGORY(DRSL, "DRSSL","\033[1;35m", "") |
| 51 | DEBUG_CATEGORY(DNM, "DNM", "\033[1;36m", "") |
Holger Freyther | d546e31 | 2008-12-27 12:03:07 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
Holger Freyther | 7c03e4c | 2008-12-27 12:46:48 +0000 | [diff] [blame] | 54 | static int use_color = 1; |
| 55 | |
Holger Freyther | b332f61 | 2008-12-27 12:46:51 +0000 | [diff] [blame] | 56 | void debug_use_color(int color) |
| 57 | { |
| 58 | use_color = color; |
| 59 | } |
| 60 | |
Holger Freyther | d546e31 | 2008-12-27 12:03:07 +0000 | [diff] [blame] | 61 | /* |
| 62 | * Parse the category mask. |
| 63 | * category1:category2:category3 |
| 64 | */ |
Holger Freyther | 5ee72ee | 2008-12-27 12:46:49 +0000 | [diff] [blame] | 65 | void debug_parse_category_mask(const char *_mask) |
Holger Freyther | d546e31 | 2008-12-27 12:03:07 +0000 | [diff] [blame] | 66 | { |
| 67 | unsigned int new_mask = 0; |
| 68 | int i = 0; |
| 69 | char *mask = strdup(_mask); |
| 70 | char *category_token = NULL; |
| 71 | |
| 72 | category_token = strtok(mask, ":"); |
| 73 | do { |
| 74 | for (i = 0; i < ARRAY_SIZE(debug_info); ++i) { |
| 75 | if (strcasecmp(debug_info[i].name, category_token) == 0) |
| 76 | new_mask |= debug_info[i].number; |
| 77 | } |
| 78 | } while (category_token = strtok(NULL, ":")); |
| 79 | |
| 80 | |
| 81 | free(mask); |
| 82 | debug_mask = new_mask; |
| 83 | } |
| 84 | |
Holger Freyther | 7c03e4c | 2008-12-27 12:46:48 +0000 | [diff] [blame] | 85 | const char* color(int subsys) |
| 86 | { |
| 87 | int i = 0; |
| 88 | |
| 89 | for (i = 0; use_color && i < ARRAY_SIZE(debug_info); ++i) { |
| 90 | if (debug_info[i].number == subsys) |
| 91 | return debug_info[i].color; |
| 92 | } |
| 93 | |
| 94 | return ""; |
| 95 | } |
| 96 | |
Holger Freyther | 32636e8 | 2008-12-27 11:07:15 +0000 | [diff] [blame] | 97 | void debugp(unsigned int subsys, char *file, int line, const char *format, ...) |
| 98 | { |
| 99 | char *timestr; |
| 100 | va_list ap; |
| 101 | time_t tm; |
| 102 | FILE *outfd = stderr; |
| 103 | |
| 104 | if (!(debug_mask & subsys)) |
| 105 | return; |
| 106 | |
| 107 | va_start(ap, format); |
| 108 | |
| 109 | tm = time(NULL); |
| 110 | timestr = ctime(&tm); |
| 111 | timestr[strlen(timestr)-1] = '\0'; |
Holger Freyther | 7c03e4c | 2008-12-27 12:46:48 +0000 | [diff] [blame] | 112 | fprintf(outfd, "%s%s <%4.4x> %s:%d ", color(subsys), timestr, subsys, file, line); |
Holger Freyther | 32636e8 | 2008-12-27 11:07:15 +0000 | [diff] [blame] | 113 | vfprintf(outfd, format, ap); |
Holger Freyther | 7c03e4c | 2008-12-27 12:46:48 +0000 | [diff] [blame] | 114 | fprintf(outfd, "\033[0;m"); |
Holger Freyther | 32636e8 | 2008-12-27 11:07:15 +0000 | [diff] [blame] | 115 | |
| 116 | va_end(ap); |
| 117 | |
| 118 | fflush(outfd); |
| 119 | } |
| 120 | |