blob: 7c508771fee85eba3782c050ec2c6d201bff2601 [file] [log] [blame]
Harald Welte411e86f2010-03-26 21:35:28 +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
Harald Weltef3f90022010-04-30 14:29:56 +020023#include "../config.h"
24
Harald Welte411e86f2010-03-26 21:35:28 +080025#include <stdarg.h>
26#include <stdlib.h>
27#include <stdio.h>
28#include <string.h>
Harald Weltef3f90022010-04-30 14:29:56 +020029
30#ifdef HAVE_STRINGS_H
Harald Welte411e86f2010-03-26 21:35:28 +080031#include <strings.h>
Harald Weltef3f90022010-04-30 14:29:56 +020032#endif
Harald Welte411e86f2010-03-26 21:35:28 +080033#include <time.h>
34#include <errno.h>
35
36#include <osmocore/talloc.h>
37#include <osmocore/utils.h>
38#include <osmocore/logging.h>
39
40static const struct log_info *log_info;
41
42static struct log_context log_context;
43static void *tall_log_ctx = NULL;
44static LLIST_HEAD(target_list);
45
46static const struct value_string loglevel_strs[] = {
47 { 0, "EVERYTHING" },
48 { LOGL_DEBUG, "DEBUG" },
49 { LOGL_INFO, "INFO" },
50 { LOGL_NOTICE, "NOTICE" },
51 { LOGL_ERROR, "ERROR" },
52 { LOGL_FATAL, "FATAL" },
53 { 0, NULL },
54};
55
56int log_parse_level(const char *lvl)
57{
58 return get_string_value(loglevel_strs, lvl);
59}
60
61int log_parse_category(const char *category)
62{
63 int i;
64
65 for (i = 0; i < log_info->num_cat; ++i) {
66 if (!strcasecmp(log_info->cat[i].name+1, category))
67 return i;
68 }
69
70 return -EINVAL;
71}
72
73/*
74 * Parse the category mask.
75 * The format can be this: category1:category2:category3
76 * or category1,2:category2,3:...
77 */
78void log_parse_category_mask(struct log_target* target, const char *_mask)
79{
80 int i = 0;
81 char *mask = strdup(_mask);
82 char *category_token = NULL;
83
84 /* Disable everything to enable it afterwards */
85 for (i = 0; i < ARRAY_SIZE(target->categories); ++i)
86 target->categories[i].enabled = 0;
87
88 category_token = strtok(mask, ":");
89 do {
90 for (i = 0; i < log_info->num_cat; ++i) {
91 char* colon = strstr(category_token, ",");
92 int length = strlen(category_token);
93
94 if (colon)
95 length = colon - category_token;
96
97 if (strncasecmp(log_info->cat[i].name, category_token,
98 length) == 0) {
99 int level = 0;
100
101 if (colon)
102 level = atoi(colon+1);
103
104 target->categories[i].enabled = 1;
105 target->categories[i].loglevel = level;
106 }
107 }
108 } while ((category_token = strtok(NULL, ":")));
109
110 free(mask);
111}
112
113static const char* color(int subsys)
114{
115 if (subsys < log_info->num_cat)
116 return log_info->cat[subsys].color;
117
118 return NULL;
119}
120
121static void _output(struct log_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 const char *c = color(subsys);
140 if (c) {
141 snprintf(col, sizeof(col), "%s", color(subsys));
142 col[sizeof(col)-1] = '\0';
143 }
144 }
145 vsnprintf(buf, sizeof(buf), format, ap);
146 buf[sizeof(buf)-1] = '\0';
147
148 if (!cont) {
149 if (target->print_timestamp) {
150 char *timestr;
151 time_t tm;
152 tm = time(NULL);
153 timestr = ctime(&tm);
154 timestr[strlen(timestr)-1] = '\0';
155 snprintf(tim, sizeof(tim), "%s ", timestr);
156 tim[sizeof(tim)-1] = '\0';
157 }
158 snprintf(sub, sizeof(sub), "<%4.4x> %s:%d ", subsys, file, line);
159 sub[sizeof(sub)-1] = '\0';
160 }
161
162 snprintf(final, sizeof(final), "%s%s%s%s\033[0;m", col, tim, sub, buf);
163 final[sizeof(final)-1] = '\0';
164 target->output(target, final);
165}
166
167
168static void _logp(unsigned int subsys, int level, char *file, int line,
169 int cont, const char *format, va_list ap)
170{
171 struct log_target *tar;
172
173 llist_for_each_entry(tar, &target_list, entry) {
174 struct log_category *category;
175 int output = 0;
176
177 category = &tar->categories[subsys];
178 /* subsystem is not supposed to be logged */
179 if (!category->enabled)
180 continue;
181
182 /* Check the global log level */
183 if (tar->loglevel != 0 && level < tar->loglevel)
184 continue;
185
186 /* Check the category log level */
187 if (tar->loglevel == 0 && category->loglevel != 0 &&
188 level < category->loglevel)
189 continue;
190
191 /* Apply filters here... if that becomes messy we will
192 * need to put filters in a list and each filter will
193 * say stop, continue, output */
194 if ((tar->filter_map & LOG_FILTER_ALL) != 0)
195 output = 1;
196 else if (log_info->filter_fn)
197 output = log_info->filter_fn(&log_context,
198 tar);
199
200 if (output) {
201 /* FIXME: copying the va_list is an ugly
202 * workaround against a bug hidden somewhere in
203 * _output. If we do not copy here, the first
204 * call to _output() will corrupt the va_list
205 * contents, and any further _output() calls
206 * with the same va_list will segfault */
207 va_list bp;
208 va_copy(bp, ap);
209 _output(tar, subsys, file, line, cont, format, bp);
210 va_end(bp);
211 }
212 }
213}
214
215void logp(unsigned int subsys, char *file, int line, int cont,
216 const char *format, ...)
217{
218 va_list ap;
219
220 va_start(ap, format);
221 _logp(subsys, LOGL_DEBUG, file, line, cont, format, ap);
222 va_end(ap);
223}
224
225void logp2(unsigned int subsys, unsigned int level, char *file, int line, int cont, const char *format, ...)
226{
227 va_list ap;
228
229 va_start(ap, format);
230 _logp(subsys, level, file, line, cont, format, ap);
231 va_end(ap);
232}
233
234static char hexd_buff[4096];
235
236char *hexdump(const unsigned char *buf, int len)
237{
238 int i;
239 char *cur = hexd_buff;
240
241 hexd_buff[0] = 0;
242 for (i = 0; i < len; i++) {
243 int len_remain = sizeof(hexd_buff) - (cur - hexd_buff);
244 int rc = snprintf(cur, len_remain, "%02x ", buf[i]);
245 if (rc <= 0)
246 break;
247 cur += rc;
248 }
249 hexd_buff[sizeof(hexd_buff)-1] = 0;
250 return hexd_buff;
251}
252
253void log_add_target(struct log_target *target)
254{
255 llist_add_tail(&target->entry, &target_list);
256}
257
258void log_del_target(struct log_target *target)
259{
260 llist_del(&target->entry);
261}
262
263void log_reset_context(void)
264{
265 memset(&log_context, 0, sizeof(log_context));
266}
267
268int log_set_context(uint8_t ctx_nr, void *value)
269{
270 if (ctx_nr > LOG_MAX_CTX)
271 return -EINVAL;
272
273 log_context.ctx[ctx_nr] = value;
274
275 return 0;
276}
277
278void log_set_all_filter(struct log_target *target, int all)
279{
280 if (all)
281 target->filter_map |= LOG_FILTER_ALL;
282 else
283 target->filter_map &= ~LOG_FILTER_ALL;
284}
285
286void log_set_use_color(struct log_target *target, int use_color)
287{
288 target->use_color = use_color;
289}
290
291void log_set_print_timestamp(struct log_target *target, int print_timestamp)
292{
293 target->print_timestamp = print_timestamp;
294}
295
296void log_set_log_level(struct log_target *target, int log_level)
297{
298 target->loglevel = log_level;
299}
300
301void log_set_category_filter(struct log_target *target, int category,
302 int enable, int level)
303{
304 if (category >= log_info->num_cat)
305 return;
306 target->categories[category].enabled = !!enable;
307 target->categories[category].loglevel = level;
308}
309
Harald Weltef3f90022010-04-30 14:29:56 +0200310/* since C89/C99 says stderr is a macro, we can safely do this! */
311#ifdef stderr
Harald Welte411e86f2010-03-26 21:35:28 +0800312static void _stderr_output(struct log_target *target, const char *log)
313{
314 fprintf(target->tgt_stdout.out, "%s", log);
315 fflush(target->tgt_stdout.out);
316}
Harald Weltef3f90022010-04-30 14:29:56 +0200317#endif
Harald Welte411e86f2010-03-26 21:35:28 +0800318
319struct log_target *log_target_create(void)
320{
321 struct log_target *target;
Harald Weltef3f90022010-04-30 14:29:56 +0200322 unsigned int i;
Harald Welte411e86f2010-03-26 21:35:28 +0800323
324 target = talloc_zero(tall_log_ctx, struct log_target);
325 if (!target)
326 return NULL;
327
328 INIT_LLIST_HEAD(&target->entry);
Harald Weltef3f90022010-04-30 14:29:56 +0200329
330 /* initialize the per-category enabled/loglevel from defaults */
331 for (i = 0; i < log_info->num_cat; i++) {
332 struct log_category *cat = &target->categories[i];
333 cat->enabled = log_info->cat[i].enabled;
334 cat->loglevel = log_info->cat[i].loglevel;
335 }
336
337 /* global settings */
Harald Welte411e86f2010-03-26 21:35:28 +0800338 target->use_color = 1;
339 target->print_timestamp = 0;
Harald Weltef3f90022010-04-30 14:29:56 +0200340
341 /* global log level */
Harald Welte411e86f2010-03-26 21:35:28 +0800342 target->loglevel = 0;
343 return target;
344}
345
346struct log_target *log_target_create_stderr(void)
347{
Harald Weltef3f90022010-04-30 14:29:56 +0200348/* since C89/C99 says stderr is a macro, we can safely do this! */
349#ifdef stderr
Harald Welte411e86f2010-03-26 21:35:28 +0800350 struct log_target *target;
351
352 target = log_target_create();
353 if (!target)
354 return NULL;
355
356 target->tgt_stdout.out = stderr;
357 target->output = _stderr_output;
358 return target;
Harald Weltef3f90022010-04-30 14:29:56 +0200359#else
360 return NULL;
361#endif /* stderr */
Harald Welte411e86f2010-03-26 21:35:28 +0800362}
363
364void log_init(const struct log_info *cat)
365{
366 tall_log_ctx = talloc_named_const(NULL, 1, "logging");
367 log_info = cat;
368}