blob: c1bebcab1ec49f2069ba4c9036b05f3613d261b3 [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
31static unsigned int debug_mask = 0xffffffff & ~DMI;
32
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", "")
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 Freytherd546e312008-12-27 12:03:07 +000052};
53
Holger Freyther7c03e4c2008-12-27 12:46:48 +000054static int use_color = 1;
55
Holger Freytherd546e312008-12-27 12:03:07 +000056/*
57 * Parse the category mask.
58 * category1:category2:category3
59 */
Holger Freyther5ee72ee2008-12-27 12:46:49 +000060void debug_parse_category_mask(const char *_mask)
Holger Freytherd546e312008-12-27 12:03:07 +000061{
62 unsigned int new_mask = 0;
63 int i = 0;
64 char *mask = strdup(_mask);
65 char *category_token = NULL;
66
67 category_token = strtok(mask, ":");
68 do {
69 for (i = 0; i < ARRAY_SIZE(debug_info); ++i) {
70 if (strcasecmp(debug_info[i].name, category_token) == 0)
71 new_mask |= debug_info[i].number;
72 }
73 } while (category_token = strtok(NULL, ":"));
74
75
76 free(mask);
77 debug_mask = new_mask;
78}
79
Holger Freyther7c03e4c2008-12-27 12:46:48 +000080const char* color(int subsys)
81{
82 int i = 0;
83
84 for (i = 0; use_color && i < ARRAY_SIZE(debug_info); ++i) {
85 if (debug_info[i].number == subsys)
86 return debug_info[i].color;
87 }
88
89 return "";
90}
91
Holger Freyther32636e82008-12-27 11:07:15 +000092void debugp(unsigned int subsys, char *file, int line, const char *format, ...)
93{
94 char *timestr;
95 va_list ap;
96 time_t tm;
97 FILE *outfd = stderr;
98
99 if (!(debug_mask & subsys))
100 return;
101
102 va_start(ap, format);
103
104 tm = time(NULL);
105 timestr = ctime(&tm);
106 timestr[strlen(timestr)-1] = '\0';
Holger Freyther7c03e4c2008-12-27 12:46:48 +0000107 fprintf(outfd, "%s%s <%4.4x> %s:%d ", color(subsys), timestr, subsys, file, line);
Holger Freyther32636e82008-12-27 11:07:15 +0000108 vfprintf(outfd, format, ap);
Holger Freyther7c03e4c2008-12-27 12:46:48 +0000109 fprintf(outfd, "\033[0;m");
Holger Freyther32636e82008-12-27 11:07:15 +0000110
111 va_end(ap);
112
113 fflush(outfd);
114}
115