blob: 7c508771fee85eba3782c050ec2c6d201bff2601 [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
Harald Welte01fd5cb2010-03-26 23:51:31 +080023#include "../config.h"
24
Harald Welte4a2bb9e2010-03-26 09:33:40 +080025#include <stdarg.h>
26#include <stdlib.h>
27#include <stdio.h>
28#include <string.h>
Harald Welte01fd5cb2010-03-26 23:51:31 +080029
30#ifdef HAVE_STRINGS_H
Harald Welte4a2bb9e2010-03-26 09:33:40 +080031#include <strings.h>
Harald Welte01fd5cb2010-03-26 23:51:31 +080032#endif
Harald Welte4a2bb9e2010-03-26 09:33:40 +080033#include <time.h>
34#include <errno.h>
35
36#include <osmocore/talloc.h>
37#include <osmocore/utils.h>
Harald Welte3ae27582010-03-26 21:24:24 +080038#include <osmocore/logging.h>
Harald Welte4a2bb9e2010-03-26 09:33:40 +080039
Harald Welte3ae27582010-03-26 21:24:24 +080040static const struct log_info *log_info;
Harald Welte4a2bb9e2010-03-26 09:33:40 +080041
Harald Welte3ae27582010-03-26 21:24:24 +080042static struct log_context log_context;
43static void *tall_log_ctx = NULL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +080044static 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
Harald Welte3ae27582010-03-26 21:24:24 +080056int log_parse_level(const char *lvl)
Harald Welte4a2bb9e2010-03-26 09:33:40 +080057{
58 return get_string_value(loglevel_strs, lvl);
59}
60
Harald Welte3ae27582010-03-26 21:24:24 +080061int log_parse_category(const char *category)
Harald Welte4a2bb9e2010-03-26 09:33:40 +080062{
63 int i;
64
Harald Welte3ae27582010-03-26 21:24:24 +080065 for (i = 0; i < log_info->num_cat; ++i) {
66 if (!strcasecmp(log_info->cat[i].name+1, category))
Harald Weltefaadfe22010-03-26 21:05:43 +080067 return i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +080068 }
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 */
Harald Welte3ae27582010-03-26 21:24:24 +080078void log_parse_category_mask(struct log_target* target, const char *_mask)
Harald Welte4a2bb9e2010-03-26 09:33:40 +080079{
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 {
Harald Welte3ae27582010-03-26 21:24:24 +080090 for (i = 0; i < log_info->num_cat; ++i) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +080091 char* colon = strstr(category_token, ",");
92 int length = strlen(category_token);
93
94 if (colon)
95 length = colon - category_token;
96
Harald Welte3ae27582010-03-26 21:24:24 +080097 if (strncasecmp(log_info->cat[i].name, category_token,
Harald Welte4a2bb9e2010-03-26 09:33:40 +080098 length) == 0) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +080099 int level = 0;
100
101 if (colon)
102 level = atoi(colon+1);
103
Harald Weltefaadfe22010-03-26 21:05:43 +0800104 target->categories[i].enabled = 1;
105 target->categories[i].loglevel = level;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800106 }
107 }
108 } while ((category_token = strtok(NULL, ":")));
109
110 free(mask);
111}
112
113static const char* color(int subsys)
114{
Harald Welte3ae27582010-03-26 21:24:24 +0800115 if (subsys < log_info->num_cat)
116 return log_info->cat[subsys].color;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800117
Harald Welted788f662010-03-26 09:45:03 +0800118 return NULL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800119}
120
Harald Welte3ae27582010-03-26 21:24:24 +0800121static void _output(struct log_target *target, unsigned int subsys,
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800122 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) {
Harald Welted788f662010-03-26 09:45:03 +0800139 const char *c = color(subsys);
140 if (c) {
141 snprintf(col, sizeof(col), "%s", color(subsys));
142 col[sizeof(col)-1] = '\0';
143 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800144 }
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
Harald Welte3ae27582010-03-26 21:24:24 +0800168static void _logp(unsigned int subsys, int level, char *file, int line,
169 int cont, const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800170{
Harald Welte3ae27582010-03-26 21:24:24 +0800171 struct log_target *tar;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800172
173 llist_for_each_entry(tar, &target_list, entry) {
Harald Welte3ae27582010-03-26 21:24:24 +0800174 struct log_category *category;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800175 int output = 0;
176
177 category = &tar->categories[subsys];
Harald Welte3ae27582010-03-26 21:24:24 +0800178 /* subsystem is not supposed to be logged */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800179 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 */
Harald Welte3ae27582010-03-26 21:24:24 +0800194 if ((tar->filter_map & LOG_FILTER_ALL) != 0)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800195 output = 1;
Harald Welte3ae27582010-03-26 21:24:24 +0800196 else if (log_info->filter_fn)
197 output = log_info->filter_fn(&log_context,
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800198 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
Harald Welte3ae27582010-03-26 21:24:24 +0800215void logp(unsigned int subsys, char *file, int line, int cont,
216 const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800217{
218 va_list ap;
219
220 va_start(ap, format);
Harald Welte3ae27582010-03-26 21:24:24 +0800221 _logp(subsys, LOGL_DEBUG, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800222 va_end(ap);
223}
224
Harald Welte3ae27582010-03-26 21:24:24 +0800225void logp2(unsigned int subsys, unsigned int level, char *file, int line, int cont, const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800226{
227 va_list ap;
228
229 va_start(ap, format);
Harald Welte3ae27582010-03-26 21:24:24 +0800230 _logp(subsys, level, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800231 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
Harald Welte3ae27582010-03-26 21:24:24 +0800253void log_add_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800254{
255 llist_add_tail(&target->entry, &target_list);
256}
257
Harald Welte3ae27582010-03-26 21:24:24 +0800258void log_del_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800259{
260 llist_del(&target->entry);
261}
262
Harald Welte3ae27582010-03-26 21:24:24 +0800263void log_reset_context(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800264{
Harald Welte3ae27582010-03-26 21:24:24 +0800265 memset(&log_context, 0, sizeof(log_context));
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800266}
267
Harald Welte3ae27582010-03-26 21:24:24 +0800268int log_set_context(uint8_t ctx_nr, void *value)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800269{
Harald Welte3ae27582010-03-26 21:24:24 +0800270 if (ctx_nr > LOG_MAX_CTX)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800271 return -EINVAL;
272
Harald Welte3ae27582010-03-26 21:24:24 +0800273 log_context.ctx[ctx_nr] = value;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800274
275 return 0;
276}
277
Harald Welte3ae27582010-03-26 21:24:24 +0800278void log_set_all_filter(struct log_target *target, int all)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800279{
280 if (all)
Harald Welte3ae27582010-03-26 21:24:24 +0800281 target->filter_map |= LOG_FILTER_ALL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800282 else
Harald Welte3ae27582010-03-26 21:24:24 +0800283 target->filter_map &= ~LOG_FILTER_ALL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800284}
285
Harald Welte3ae27582010-03-26 21:24:24 +0800286void log_set_use_color(struct log_target *target, int use_color)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800287{
288 target->use_color = use_color;
289}
290
Harald Welte3ae27582010-03-26 21:24:24 +0800291void log_set_print_timestamp(struct log_target *target, int print_timestamp)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800292{
293 target->print_timestamp = print_timestamp;
294}
295
Harald Welte3ae27582010-03-26 21:24:24 +0800296void log_set_log_level(struct log_target *target, int log_level)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800297{
298 target->loglevel = log_level;
299}
300
Harald Welte3ae27582010-03-26 21:24:24 +0800301void log_set_category_filter(struct log_target *target, int category,
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800302 int enable, int level)
303{
Harald Welte3ae27582010-03-26 21:24:24 +0800304 if (category >= log_info->num_cat)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800305 return;
306 target->categories[category].enabled = !!enable;
307 target->categories[category].loglevel = level;
308}
309
Harald Weltea3b844c2010-03-27 00:04:40 +0800310/* since C89/C99 says stderr is a macro, we can safely do this! */
311#ifdef stderr
Harald Welte3ae27582010-03-26 21:24:24 +0800312static void _stderr_output(struct log_target *target, const char *log)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800313{
314 fprintf(target->tgt_stdout.out, "%s", log);
315 fflush(target->tgt_stdout.out);
316}
Harald Weltea3b844c2010-03-27 00:04:40 +0800317#endif
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800318
Harald Welte3ae27582010-03-26 21:24:24 +0800319struct log_target *log_target_create(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800320{
Harald Welte3ae27582010-03-26 21:24:24 +0800321 struct log_target *target;
Harald Weltecc6313c2010-03-26 22:04:03 +0800322 unsigned int i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800323
Harald Welte3ae27582010-03-26 21:24:24 +0800324 target = talloc_zero(tall_log_ctx, struct log_target);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800325 if (!target)
326 return NULL;
327
328 INIT_LLIST_HEAD(&target->entry);
Harald Weltecc6313c2010-03-26 22:04:03 +0800329
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 Welte4a2bb9e2010-03-26 09:33:40 +0800338 target->use_color = 1;
339 target->print_timestamp = 0;
Harald Weltecc6313c2010-03-26 22:04:03 +0800340
341 /* global log level */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800342 target->loglevel = 0;
343 return target;
344}
345
Harald Welte3ae27582010-03-26 21:24:24 +0800346struct log_target *log_target_create_stderr(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800347{
Harald Weltea3b844c2010-03-27 00:04:40 +0800348/* since C89/C99 says stderr is a macro, we can safely do this! */
349#ifdef stderr
Harald Welte3ae27582010-03-26 21:24:24 +0800350 struct log_target *target;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800351
Harald Welte3ae27582010-03-26 21:24:24 +0800352 target = log_target_create();
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800353 if (!target)
354 return NULL;
355
356 target->tgt_stdout.out = stderr;
357 target->output = _stderr_output;
358 return target;
Harald Weltea3b844c2010-03-27 00:04:40 +0800359#else
360 return NULL;
361#endif /* stderr */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800362}
363
Harald Welte3ae27582010-03-26 21:24:24 +0800364void log_init(const struct log_info *cat)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800365{
Harald Welte3ae27582010-03-26 21:24:24 +0800366 tall_log_ctx = talloc_named_const(NULL, 1, "logging");
367 log_info = cat;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800368}