blob: 5be4e58ed485837dca221a6f0f3f48387e7620ca [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 Welte4ebdf742010-05-19 19:54:00 +020040const struct log_info *osmo_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 Welte9ac22252010-05-11 11:19:40 +020061const char *log_level_str(unsigned int lvl)
62{
63 return get_value_string(loglevel_strs, lvl);
64}
65
Harald Welte3ae27582010-03-26 21:24:24 +080066int log_parse_category(const char *category)
Harald Welte4a2bb9e2010-03-26 09:33:40 +080067{
68 int i;
69
Harald Welte4ebdf742010-05-19 19:54:00 +020070 for (i = 0; i < osmo_log_info->num_cat; ++i) {
71 if (!strcasecmp(osmo_log_info->cat[i].name+1, category))
Harald Weltefaadfe22010-03-26 21:05:43 +080072 return i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +080073 }
74
75 return -EINVAL;
76}
77
78/*
79 * Parse the category mask.
80 * The format can be this: category1:category2:category3
81 * or category1,2:category2,3:...
82 */
Harald Welte3ae27582010-03-26 21:24:24 +080083void log_parse_category_mask(struct log_target* target, const char *_mask)
Harald Welte4a2bb9e2010-03-26 09:33:40 +080084{
85 int i = 0;
86 char *mask = strdup(_mask);
87 char *category_token = NULL;
88
89 /* Disable everything to enable it afterwards */
90 for (i = 0; i < ARRAY_SIZE(target->categories); ++i)
91 target->categories[i].enabled = 0;
92
93 category_token = strtok(mask, ":");
94 do {
Harald Welte4ebdf742010-05-19 19:54:00 +020095 for (i = 0; i < osmo_log_info->num_cat; ++i) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +080096 char* colon = strstr(category_token, ",");
97 int length = strlen(category_token);
98
99 if (colon)
100 length = colon - category_token;
101
Harald Welte4ebdf742010-05-19 19:54:00 +0200102 if (strncasecmp(osmo_log_info->cat[i].name,
103 category_token, length) == 0) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800104 int level = 0;
105
106 if (colon)
107 level = atoi(colon+1);
108
Harald Weltefaadfe22010-03-26 21:05:43 +0800109 target->categories[i].enabled = 1;
110 target->categories[i].loglevel = level;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800111 }
112 }
113 } while ((category_token = strtok(NULL, ":")));
114
115 free(mask);
116}
117
118static const char* color(int subsys)
119{
Harald Welte4ebdf742010-05-19 19:54:00 +0200120 if (subsys < osmo_log_info->num_cat)
121 return osmo_log_info->cat[subsys].color;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800122
Harald Welted788f662010-03-26 09:45:03 +0800123 return NULL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800124}
125
Harald Welte3ae27582010-03-26 21:24:24 +0800126static void _output(struct log_target *target, unsigned int subsys,
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800127 char *file, int line, int cont, const char *format,
128 va_list ap)
129{
130 char col[30];
131 char sub[30];
132 char tim[30];
133 char buf[4096];
134 char final[4096];
135
136 /* prepare the data */
137 col[0] = '\0';
138 sub[0] = '\0';
139 tim[0] = '\0';
140 buf[0] = '\0';
141
142 /* are we using color */
143 if (target->use_color) {
Harald Welted788f662010-03-26 09:45:03 +0800144 const char *c = color(subsys);
145 if (c) {
146 snprintf(col, sizeof(col), "%s", color(subsys));
147 col[sizeof(col)-1] = '\0';
148 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800149 }
150 vsnprintf(buf, sizeof(buf), format, ap);
151 buf[sizeof(buf)-1] = '\0';
152
153 if (!cont) {
154 if (target->print_timestamp) {
155 char *timestr;
156 time_t tm;
157 tm = time(NULL);
158 timestr = ctime(&tm);
159 timestr[strlen(timestr)-1] = '\0';
160 snprintf(tim, sizeof(tim), "%s ", timestr);
161 tim[sizeof(tim)-1] = '\0';
162 }
163 snprintf(sub, sizeof(sub), "<%4.4x> %s:%d ", subsys, file, line);
164 sub[sizeof(sub)-1] = '\0';
165 }
166
Harald Weltea439a3a2010-07-30 21:01:54 +0200167 snprintf(final, sizeof(final), "%s%s%s%s%s", col, tim, sub, buf,
168 target->use_color ? "\033[0;m" : "");
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800169 final[sizeof(final)-1] = '\0';
170 target->output(target, final);
171}
172
173
Harald Welte3ae27582010-03-26 21:24:24 +0800174static void _logp(unsigned int subsys, int level, char *file, int line,
175 int cont, const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800176{
Harald Welte3ae27582010-03-26 21:24:24 +0800177 struct log_target *tar;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800178
179 llist_for_each_entry(tar, &target_list, entry) {
Harald Welte3ae27582010-03-26 21:24:24 +0800180 struct log_category *category;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800181 int output = 0;
182
183 category = &tar->categories[subsys];
Harald Welte3ae27582010-03-26 21:24:24 +0800184 /* subsystem is not supposed to be logged */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800185 if (!category->enabled)
186 continue;
187
188 /* Check the global log level */
189 if (tar->loglevel != 0 && level < tar->loglevel)
190 continue;
191
192 /* Check the category log level */
193 if (tar->loglevel == 0 && category->loglevel != 0 &&
194 level < category->loglevel)
195 continue;
196
197 /* Apply filters here... if that becomes messy we will
198 * need to put filters in a list and each filter will
199 * say stop, continue, output */
Harald Welte3ae27582010-03-26 21:24:24 +0800200 if ((tar->filter_map & LOG_FILTER_ALL) != 0)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800201 output = 1;
Harald Welte4ebdf742010-05-19 19:54:00 +0200202 else if (osmo_log_info->filter_fn)
203 output = osmo_log_info->filter_fn(&log_context,
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800204 tar);
205
206 if (output) {
207 /* FIXME: copying the va_list is an ugly
208 * workaround against a bug hidden somewhere in
209 * _output. If we do not copy here, the first
210 * call to _output() will corrupt the va_list
211 * contents, and any further _output() calls
212 * with the same va_list will segfault */
213 va_list bp;
214 va_copy(bp, ap);
215 _output(tar, subsys, file, line, cont, format, bp);
216 va_end(bp);
217 }
218 }
219}
220
Harald Welte3ae27582010-03-26 21:24:24 +0800221void logp(unsigned int subsys, char *file, int line, int cont,
222 const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800223{
224 va_list ap;
225
226 va_start(ap, format);
Harald Welte3ae27582010-03-26 21:24:24 +0800227 _logp(subsys, LOGL_DEBUG, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800228 va_end(ap);
229}
230
Harald Welte3ae27582010-03-26 21:24:24 +0800231void logp2(unsigned int subsys, unsigned int level, char *file, int line, int cont, const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800232{
233 va_list ap;
234
235 va_start(ap, format);
Harald Welte3ae27582010-03-26 21:24:24 +0800236 _logp(subsys, level, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800237 va_end(ap);
238}
239
Harald Welte3ae27582010-03-26 21:24:24 +0800240void log_add_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800241{
242 llist_add_tail(&target->entry, &target_list);
243}
244
Harald Welte3ae27582010-03-26 21:24:24 +0800245void log_del_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800246{
247 llist_del(&target->entry);
248}
249
Harald Welte3ae27582010-03-26 21:24:24 +0800250void log_reset_context(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800251{
Harald Welte3ae27582010-03-26 21:24:24 +0800252 memset(&log_context, 0, sizeof(log_context));
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800253}
254
Harald Welte3ae27582010-03-26 21:24:24 +0800255int log_set_context(uint8_t ctx_nr, void *value)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800256{
Harald Welte3ae27582010-03-26 21:24:24 +0800257 if (ctx_nr > LOG_MAX_CTX)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800258 return -EINVAL;
259
Harald Welte3ae27582010-03-26 21:24:24 +0800260 log_context.ctx[ctx_nr] = value;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800261
262 return 0;
263}
264
Harald Welte3ae27582010-03-26 21:24:24 +0800265void log_set_all_filter(struct log_target *target, int all)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800266{
267 if (all)
Harald Welte3ae27582010-03-26 21:24:24 +0800268 target->filter_map |= LOG_FILTER_ALL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800269 else
Harald Welte3ae27582010-03-26 21:24:24 +0800270 target->filter_map &= ~LOG_FILTER_ALL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800271}
272
Harald Welte3ae27582010-03-26 21:24:24 +0800273void log_set_use_color(struct log_target *target, int use_color)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800274{
275 target->use_color = use_color;
276}
277
Harald Welte3ae27582010-03-26 21:24:24 +0800278void log_set_print_timestamp(struct log_target *target, int print_timestamp)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800279{
280 target->print_timestamp = print_timestamp;
281}
282
Harald Welte3ae27582010-03-26 21:24:24 +0800283void log_set_log_level(struct log_target *target, int log_level)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800284{
285 target->loglevel = log_level;
286}
287
Harald Welte3ae27582010-03-26 21:24:24 +0800288void log_set_category_filter(struct log_target *target, int category,
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800289 int enable, int level)
290{
Harald Welte4ebdf742010-05-19 19:54:00 +0200291 if (category >= osmo_log_info->num_cat)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800292 return;
293 target->categories[category].enabled = !!enable;
294 target->categories[category].loglevel = level;
295}
296
Harald Weltea3b844c2010-03-27 00:04:40 +0800297/* since C89/C99 says stderr is a macro, we can safely do this! */
298#ifdef stderr
Harald Welte0083cd32010-08-25 14:55:44 +0200299static void _file_output(struct log_target *target, const char *log)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800300{
Harald Welte0083cd32010-08-25 14:55:44 +0200301 fprintf(target->tgt_file.out, "%s", log);
302 fflush(target->tgt_file.out);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800303}
Harald Weltea3b844c2010-03-27 00:04:40 +0800304#endif
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800305
Harald Welte3ae27582010-03-26 21:24:24 +0800306struct log_target *log_target_create(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800307{
Harald Welte3ae27582010-03-26 21:24:24 +0800308 struct log_target *target;
Harald Weltecc6313c2010-03-26 22:04:03 +0800309 unsigned int i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800310
Harald Welte3ae27582010-03-26 21:24:24 +0800311 target = talloc_zero(tall_log_ctx, struct log_target);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800312 if (!target)
313 return NULL;
314
315 INIT_LLIST_HEAD(&target->entry);
Harald Weltecc6313c2010-03-26 22:04:03 +0800316
317 /* initialize the per-category enabled/loglevel from defaults */
Harald Welte4ebdf742010-05-19 19:54:00 +0200318 for (i = 0; i < osmo_log_info->num_cat; i++) {
Harald Weltecc6313c2010-03-26 22:04:03 +0800319 struct log_category *cat = &target->categories[i];
Harald Welte4ebdf742010-05-19 19:54:00 +0200320 cat->enabled = osmo_log_info->cat[i].enabled;
321 cat->loglevel = osmo_log_info->cat[i].loglevel;
Harald Weltecc6313c2010-03-26 22:04:03 +0800322 }
323
324 /* global settings */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800325 target->use_color = 1;
326 target->print_timestamp = 0;
Harald Weltecc6313c2010-03-26 22:04:03 +0800327
328 /* global log level */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800329 target->loglevel = 0;
330 return target;
331}
332
Harald Welte3ae27582010-03-26 21:24:24 +0800333struct log_target *log_target_create_stderr(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800334{
Harald Weltea3b844c2010-03-27 00:04:40 +0800335/* since C89/C99 says stderr is a macro, we can safely do this! */
336#ifdef stderr
Harald Welte3ae27582010-03-26 21:24:24 +0800337 struct log_target *target;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800338
Harald Welte3ae27582010-03-26 21:24:24 +0800339 target = log_target_create();
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800340 if (!target)
341 return NULL;
342
Harald Welte0083cd32010-08-25 14:55:44 +0200343 target->tgt_file.out = stderr;
344 target->output = _file_output;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800345 return target;
Harald Weltea3b844c2010-03-27 00:04:40 +0800346#else
347 return NULL;
348#endif /* stderr */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800349}
350
Harald Welte3086c392010-08-25 19:10:50 +0200351struct log_target *log_target_create_file(const char *fname)
352{
353 struct log_target *target;
354
355 target = log_target_create();
356 if (!target)
357 return NULL;
358
359 target->tgt_file.out = fopen(fname, "a");
360 if (!target->tgt_file.out)
361 return NULL;
362
363 target->output = _file_output;
364
365 target->tgt_file.fname = talloc_strdup(target, fname);
366
367 return target;
368}
369
370void log_target_destroy(struct log_target *target)
371{
372
373 /* just in case, to make sure we don't have any references */
374 log_del_target(target);
375
376 if (target->output == &_file_output) {
377 /* don't close stderr */
378 if (target->tgt_file.out != stderr) {
379 fclose(target->tgt_file.out);
380 target->tgt_file.out = NULL;
381 }
382 }
383
384 talloc_free(target);
385}
386
387/* close and re-open a log file (for log file rotation) */
388int log_target_file_reopen(struct log_target *target)
389{
390 fclose(target->tgt_file.out);
391
392 target->tgt_file.out = fopen(target->tgt_file.fname, "a");
393 if (!target->tgt_file.out)
394 return -errno;
395
396 /* we assume target->output already to be set */
397
398 return 0;
399}
400
Harald Welte7638af92010-05-11 16:39:22 +0200401const char *log_vty_level_string(struct log_info *info)
402{
403 const struct value_string *vs;
404 unsigned int len = 3; /* ()\0 */
405 char *str;
406
407 for (vs = loglevel_strs; vs->value || vs->str; vs++)
408 len += strlen(vs->str) + 1;
409
410 str = talloc_zero_size(NULL, len);
411 if (!str)
412 return NULL;
413
414 str[0] = '(';
415 for (vs = loglevel_strs; vs->value || vs->str; vs++) {
416 strcat(str, vs->str);
417 strcat(str, "|");
418 }
419 str[strlen(str)-1] = ')';
420
421 return str;
422}
423
424const char *log_vty_category_string(struct log_info *info)
425{
426 unsigned int len = 3; /* "()\0" */
427 unsigned int i;
428 char *str;
429
430 for (i = 0; i < info->num_cat; i++)
431 len += strlen(info->cat[i].name) + 1;
432
433 str = talloc_zero_size(NULL, len);
434 if (!str)
435 return NULL;
436
437 str[0] = '(';
438 for (i = 0; i < info->num_cat; i++) {
439 strcat(str, info->cat[i].name+1);
440 strcat(str, "|");
441 }
442 str[strlen(str)-1] = ')';
443
444 return str;
445}
446
Harald Welte3ae27582010-03-26 21:24:24 +0800447void log_init(const struct log_info *cat)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800448{
Harald Welte3ae27582010-03-26 21:24:24 +0800449 tall_log_ctx = talloc_named_const(NULL, 1, "logging");
Harald Welte4ebdf742010-05-19 19:54:00 +0200450 osmo_log_info = cat;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800451}