blob: 19fc305877177a99eaeaf484da299015ab0da5a9 [file] [log] [blame]
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001/* 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
35static const struct debug_info *debug_info;
36
37static struct debug_context debug_context;
38static void *tall_dbg_ctx = NULL;
39static LLIST_HEAD(target_list);
40
41static 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
51int debug_parse_level(const char *lvl)
52{
53 return get_string_value(loglevel_strs, lvl);
54}
55
56int 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))
Harald Weltefaadfe22010-03-26 21:05:43 +080062 return i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +080063 }
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 */
73void 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) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +080094 int level = 0;
95
96 if (colon)
97 level = atoi(colon+1);
98
Harald Weltefaadfe22010-03-26 21:05:43 +080099 target->categories[i].enabled = 1;
100 target->categories[i].loglevel = level;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800101 }
102 }
103 } while ((category_token = strtok(NULL, ":")));
104
105 free(mask);
106}
107
108static const char* color(int subsys)
109{
Harald Weltefaadfe22010-03-26 21:05:43 +0800110 if (subsys < debug_info->num_cat)
111 return debug_info->cat[subsys].color;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800112
Harald Welted788f662010-03-26 09:45:03 +0800113 return NULL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800114}
115
116static void _output(struct debug_target *target, unsigned int subsys,
117 char *file, int line, int cont, const char *format,
118 va_list ap)
119{
120 char col[30];
121 char sub[30];
122 char tim[30];
123 char buf[4096];
124 char final[4096];
125
126 /* prepare the data */
127 col[0] = '\0';
128 sub[0] = '\0';
129 tim[0] = '\0';
130 buf[0] = '\0';
131
132 /* are we using color */
133 if (target->use_color) {
Harald Welted788f662010-03-26 09:45:03 +0800134 const char *c = color(subsys);
135 if (c) {
136 snprintf(col, sizeof(col), "%s", color(subsys));
137 col[sizeof(col)-1] = '\0';
138 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800139 }
140 vsnprintf(buf, sizeof(buf), format, ap);
141 buf[sizeof(buf)-1] = '\0';
142
143 if (!cont) {
144 if (target->print_timestamp) {
145 char *timestr;
146 time_t tm;
147 tm = time(NULL);
148 timestr = ctime(&tm);
149 timestr[strlen(timestr)-1] = '\0';
150 snprintf(tim, sizeof(tim), "%s ", timestr);
151 tim[sizeof(tim)-1] = '\0';
152 }
153 snprintf(sub, sizeof(sub), "<%4.4x> %s:%d ", subsys, file, line);
154 sub[sizeof(sub)-1] = '\0';
155 }
156
157 snprintf(final, sizeof(final), "%s%s%s%s\033[0;m", col, tim, sub, buf);
158 final[sizeof(final)-1] = '\0';
159 target->output(target, final);
160}
161
162
163static void _debugp(unsigned int subsys, int level, char *file, int line,
164 int cont, const char *format, va_list ap)
165{
166 struct debug_target *tar;
167
168 llist_for_each_entry(tar, &target_list, entry) {
169 struct debug_category *category;
170 int output = 0;
171
172 category = &tar->categories[subsys];
173 /* subsystem is not supposed to be debugged */
174 if (!category->enabled)
175 continue;
176
177 /* Check the global log level */
178 if (tar->loglevel != 0 && level < tar->loglevel)
179 continue;
180
181 /* Check the category log level */
182 if (tar->loglevel == 0 && category->loglevel != 0 &&
183 level < category->loglevel)
184 continue;
185
186 /* Apply filters here... if that becomes messy we will
187 * need to put filters in a list and each filter will
188 * say stop, continue, output */
189 if ((tar->filter_map & DEBUG_FILTER_ALL) != 0)
190 output = 1;
191 else if (debug_info->filter_fn)
192 output = debug_info->filter_fn(&debug_context,
193 tar);
194
195 if (output) {
196 /* FIXME: copying the va_list is an ugly
197 * workaround against a bug hidden somewhere in
198 * _output. If we do not copy here, the first
199 * call to _output() will corrupt the va_list
200 * contents, and any further _output() calls
201 * with the same va_list will segfault */
202 va_list bp;
203 va_copy(bp, ap);
204 _output(tar, subsys, file, line, cont, format, bp);
205 va_end(bp);
206 }
207 }
208}
209
210void debugp(unsigned int subsys, char *file, int line, int cont,
211 const char *format, ...)
212{
213 va_list ap;
214
215 va_start(ap, format);
216 _debugp(subsys, LOGL_DEBUG, file, line, cont, format, ap);
217 va_end(ap);
218}
219
220void debugp2(unsigned int subsys, unsigned int level, char *file, int line, int cont, const char *format, ...)
221{
222 va_list ap;
223
224 va_start(ap, format);
225 _debugp(subsys, level, file, line, cont, format, ap);
226 va_end(ap);
227}
228
229static char hexd_buff[4096];
230
231char *hexdump(const unsigned char *buf, int len)
232{
233 int i;
234 char *cur = hexd_buff;
235
236 hexd_buff[0] = 0;
237 for (i = 0; i < len; i++) {
238 int len_remain = sizeof(hexd_buff) - (cur - hexd_buff);
239 int rc = snprintf(cur, len_remain, "%02x ", buf[i]);
240 if (rc <= 0)
241 break;
242 cur += rc;
243 }
244 hexd_buff[sizeof(hexd_buff)-1] = 0;
245 return hexd_buff;
246}
247
248void debug_add_target(struct debug_target *target)
249{
250 llist_add_tail(&target->entry, &target_list);
251}
252
253void debug_del_target(struct debug_target *target)
254{
255 llist_del(&target->entry);
256}
257
258void debug_reset_context(void)
259{
260 memset(&debug_context, 0, sizeof(debug_context));
261}
262
263int debug_set_context(uint8_t ctx_nr, void *value)
264{
265 if (ctx_nr > DEBUG_MAX_CTX)
266 return -EINVAL;
267
268 debug_context.ctx[ctx_nr] = value;
269
270 return 0;
271}
272
273void debug_set_all_filter(struct debug_target *target, int all)
274{
275 if (all)
276 target->filter_map |= DEBUG_FILTER_ALL;
277 else
278 target->filter_map &= ~DEBUG_FILTER_ALL;
279}
280
281void debug_set_use_color(struct debug_target *target, int use_color)
282{
283 target->use_color = use_color;
284}
285
286void debug_set_print_timestamp(struct debug_target *target, int print_timestamp)
287{
288 target->print_timestamp = print_timestamp;
289}
290
291void debug_set_log_level(struct debug_target *target, int log_level)
292{
293 target->loglevel = log_level;
294}
295
296void debug_set_category_filter(struct debug_target *target, int category,
297 int enable, int level)
298{
299 if (category >= debug_info->num_cat)
300 return;
301 target->categories[category].enabled = !!enable;
302 target->categories[category].loglevel = level;
303}
304
305static void _stderr_output(struct debug_target *target, const char *log)
306{
307 fprintf(target->tgt_stdout.out, "%s", log);
308 fflush(target->tgt_stdout.out);
309}
310
311struct debug_target *debug_target_create(void)
312{
313 struct debug_target *target;
314
315 target = talloc_zero(tall_dbg_ctx, struct debug_target);
316 if (!target)
317 return NULL;
318
319 INIT_LLIST_HEAD(&target->entry);
320 memcpy(target->categories, debug_info->cat,
321 sizeof(struct debug_category)*debug_info->num_cat);
322 target->use_color = 1;
323 target->print_timestamp = 0;
324 target->loglevel = 0;
325 return target;
326}
327
328struct debug_target *debug_target_create_stderr(void)
329{
330 struct debug_target *target;
331
332 target = debug_target_create();
333 if (!target)
334 return NULL;
335
336 target->tgt_stdout.out = stderr;
337 target->output = _stderr_output;
338 return target;
339}
340
341void debug_init(const struct debug_info *cat)
342{
343 tall_dbg_ctx = talloc_named_const(NULL, 1, "debug");
344 debug_info = cat;
345}