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