blob: f88a90475d24d2933c20d2a2fd7b8a963f20be53 [file] [log] [blame]
Holger Freyther32636e82008-12-27 11:07:15 +00001/* Debugging/Logging support code */
2/* (C) 2008 by Harald Welte <laforge@gnumonks.org>
Holger Freytherd546e312008-12-27 12:03:07 +00003 * (C) 2008 by Holger Hans Peter Freyther <zecke@selfish.org>
Holger Freyther32636e82008-12-27 11:07:15 +00004 * 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 Freytherd546e312008-12-27 12:03:07 +000023#include <stdlib.h>
Holger Freyther32636e82008-12-27 11:07:15 +000024#include <stdio.h>
25#include <string.h>
Holger Freytherd546e312008-12-27 12:03:07 +000026#include <strings.h>
Holger Freyther32636e82008-12-27 11:07:15 +000027#include <time.h>
28
29#include <openbsc/debug.h>
30
Harald Welte10d0e672009-06-27 02:53:10 +020031unsigned int debug_mask = 0xffffffff & ~(DMI|DMIB|DMEAS);
Holger Freyther32636e82008-12-27 11:07:15 +000032
Holger Freytherd546e312008-12-27 12:03:07 +000033struct 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
45static const struct debug_info debug_info[] = {
Holger Freyther7c03e4c2008-12-27 12:46:48 +000046 DEBUG_CATEGORY(DRLL, "DRLL", "\033[1;31m", "")
47 DEBUG_CATEGORY(DCC, "DCC", "\033[1;32m", "")
Holger Freyther3770b762009-06-02 02:35:12 +000048 DEBUG_CATEGORY(DMM, "DMM", "\033[1;33m", "")
Holger Freyther7c03e4c2008-12-27 12:46:48 +000049 DEBUG_CATEGORY(DRR, "DRR", "\033[1;34m", "")
Harald Weltec1d2aae2009-05-23 06:43:35 +000050 DEBUG_CATEGORY(DRSL, "DRSL", "\033[1;35m", "")
Holger Freyther7c03e4c2008-12-27 12:46:48 +000051 DEBUG_CATEGORY(DNM, "DNM", "\033[1;36m", "")
Daniel Willmann10d06f62008-12-28 21:38:25 +000052 DEBUG_CATEGORY(DSMS, "DSMS", "\033[1;37m", "")
Harald Welted35b6a72008-12-29 04:06:41 +000053 DEBUG_CATEGORY(DPAG, "DPAG", "\033[1;38m", "")
Harald Weltec125a682009-05-23 06:42:38 +000054 DEBUG_CATEGORY(DMNCC, "DMNCC","\033[1;39m", "")
Harald Welteedb37782009-05-01 14:59:07 +000055 DEBUG_CATEGORY(DINP, "DINP", "", "")
Harald Welteb60fa592009-02-06 12:02:53 +000056 DEBUG_CATEGORY(DMI, "DMI", "", "")
57 DEBUG_CATEGORY(DMIB, "DMIB", "", "")
Harald Welteba59baf2009-02-23 00:04:04 +000058 DEBUG_CATEGORY(DMUX, "DMUX", "", "")
Harald Welte10d0e672009-06-27 02:53:10 +020059 DEBUG_CATEGORY(DMEAS, "DMEAS", "", "")
Holger Hans Peter Freythered0a47b2009-08-01 16:54:45 +020060 DEBUG_CATEGORY(DSCCP, "DSCCP", "", "")
Holger Hans Peter Freyther32201c52009-08-18 12:54:50 +020061 DEBUG_CATEGORY(DMSC, "DMSC", "", "")
Holger Freytherd546e312008-12-27 12:03:07 +000062};
63
Holger Freyther7c03e4c2008-12-27 12:46:48 +000064static int use_color = 1;
65
Holger Freytherb332f612008-12-27 12:46:51 +000066void debug_use_color(int color)
67{
68 use_color = color;
69}
70
Harald Welted3ff51d2009-06-09 20:21:57 +000071static int print_timestamp = 0;
72
73void debug_timestamp(int enable)
74{
75 print_timestamp = enable;
76}
77
78
Holger Freytherd546e312008-12-27 12:03:07 +000079/*
80 * Parse the category mask.
81 * category1:category2:category3
82 */
Holger Freyther5ee72ee2008-12-27 12:46:49 +000083void debug_parse_category_mask(const char *_mask)
Holger Freytherd546e312008-12-27 12:03:07 +000084{
85 unsigned int new_mask = 0;
86 int i = 0;
87 char *mask = strdup(_mask);
88 char *category_token = NULL;
89
90 category_token = strtok(mask, ":");
91 do {
92 for (i = 0; i < ARRAY_SIZE(debug_info); ++i) {
93 if (strcasecmp(debug_info[i].name, category_token) == 0)
94 new_mask |= debug_info[i].number;
95 }
Holger Freytherca362a62009-01-04 21:05:01 +000096 } while ((category_token = strtok(NULL, ":")));
Holger Freytherd546e312008-12-27 12:03:07 +000097
98
99 free(mask);
100 debug_mask = new_mask;
101}
102
Holger Freyther7c03e4c2008-12-27 12:46:48 +0000103const char* color(int subsys)
104{
105 int i = 0;
106
107 for (i = 0; use_color && i < ARRAY_SIZE(debug_info); ++i) {
108 if (debug_info[i].number == subsys)
109 return debug_info[i].color;
110 }
111
112 return "";
113}
114
Harald Welte6ddd1682009-02-06 12:38:29 +0000115void debugp(unsigned int subsys, char *file, int line, int cont, const char *format, ...)
Holger Freyther32636e82008-12-27 11:07:15 +0000116{
Holger Freyther32636e82008-12-27 11:07:15 +0000117 va_list ap;
Holger Freyther32636e82008-12-27 11:07:15 +0000118 FILE *outfd = stderr;
119
120 if (!(debug_mask & subsys))
121 return;
122
123 va_start(ap, format);
124
Harald Welte6ddd1682009-02-06 12:38:29 +0000125 fprintf(outfd, "%s", color(subsys));
126
127 if (!cont) {
Harald Welted3ff51d2009-06-09 20:21:57 +0000128 if (print_timestamp) {
129 char *timestr;
130 time_t tm;
131 tm = time(NULL);
132 timestr = ctime(&tm);
133 timestr[strlen(timestr)-1] = '\0';
134 fprintf(outfd, "%s ", timestr);
135 }
136 fprintf(outfd, "<%4.4x> %s:%d ", subsys, file, line);
Harald Welte6ddd1682009-02-06 12:38:29 +0000137 }
Holger Freyther32636e82008-12-27 11:07:15 +0000138 vfprintf(outfd, format, ap);
Holger Freyther7c03e4c2008-12-27 12:46:48 +0000139 fprintf(outfd, "\033[0;m");
Holger Freyther32636e82008-12-27 11:07:15 +0000140
141 va_end(ap);
142
143 fflush(outfd);
144}
145
Harald Welte3cc4bf52009-02-28 13:08:01 +0000146static char hexd_buff[4096];
147
Holger Hans Peter Freythere78074e2009-08-20 13:16:26 +0200148char *hexdump(const unsigned char *buf, int len)
Holger Freytherca362a62009-01-04 21:05:01 +0000149{
150 int i;
Harald Welte3cc4bf52009-02-28 13:08:01 +0000151 char *cur = hexd_buff;
152
153 hexd_buff[0] = 0;
Holger Freytherca362a62009-01-04 21:05:01 +0000154 for (i = 0; i < len; i++) {
Harald Welte3cc4bf52009-02-28 13:08:01 +0000155 int len_remain = sizeof(hexd_buff) - (cur - hexd_buff);
156 int rc = snprintf(cur, len_remain, "%02x ", buf[i]);
157 if (rc <= 0)
158 break;
159 cur += rc;
Holger Freytherca362a62009-01-04 21:05:01 +0000160 }
Harald Welte3cc4bf52009-02-28 13:08:01 +0000161 hexd_buff[sizeof(hexd_buff)-1] = 0;
162 return hexd_buff;
Holger Freytherca362a62009-01-04 21:05:01 +0000163}
164