blob: 2a132eb5a3cb08680efbd677fb2dee267b410f93 [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>
Harald Welte3ae27582010-03-26 21:24:24 +080033#include <osmocore/logging.h>
Harald Welte4a2bb9e2010-03-26 09:33:40 +080034
Harald Welte3ae27582010-03-26 21:24:24 +080035static const struct log_info *log_info;
Harald Welte4a2bb9e2010-03-26 09:33:40 +080036
Harald Welte3ae27582010-03-26 21:24:24 +080037static struct log_context log_context;
38static void *tall_log_ctx = NULL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +080039static 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
Harald Welte3ae27582010-03-26 21:24:24 +080051int log_parse_level(const char *lvl)
Harald Welte4a2bb9e2010-03-26 09:33:40 +080052{
53 return get_string_value(loglevel_strs, lvl);
54}
55
Harald Welte3ae27582010-03-26 21:24:24 +080056int log_parse_category(const char *category)
Harald Welte4a2bb9e2010-03-26 09:33:40 +080057{
58 int i;
59
Harald Welte3ae27582010-03-26 21:24:24 +080060 for (i = 0; i < log_info->num_cat; ++i) {
61 if (!strcasecmp(log_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 */
Harald Welte3ae27582010-03-26 21:24:24 +080073void log_parse_category_mask(struct log_target* target, const char *_mask)
Harald Welte4a2bb9e2010-03-26 09:33:40 +080074{
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 {
Harald Welte3ae27582010-03-26 21:24:24 +080085 for (i = 0; i < log_info->num_cat; ++i) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +080086 char* colon = strstr(category_token, ",");
87 int length = strlen(category_token);
88
89 if (colon)
90 length = colon - category_token;
91
Harald Welte3ae27582010-03-26 21:24:24 +080092 if (strncasecmp(log_info->cat[i].name, category_token,
Harald Welte4a2bb9e2010-03-26 09:33:40 +080093 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 Welte3ae27582010-03-26 21:24:24 +0800110 if (subsys < log_info->num_cat)
111 return log_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
Harald Welte3ae27582010-03-26 21:24:24 +0800116static void _output(struct log_target *target, unsigned int subsys,
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800117 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
Harald Welte3ae27582010-03-26 21:24:24 +0800163static void _logp(unsigned int subsys, int level, char *file, int line,
164 int cont, const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800165{
Harald Welte3ae27582010-03-26 21:24:24 +0800166 struct log_target *tar;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800167
168 llist_for_each_entry(tar, &target_list, entry) {
Harald Welte3ae27582010-03-26 21:24:24 +0800169 struct log_category *category;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800170 int output = 0;
171
172 category = &tar->categories[subsys];
Harald Welte3ae27582010-03-26 21:24:24 +0800173 /* subsystem is not supposed to be logged */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800174 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 */
Harald Welte3ae27582010-03-26 21:24:24 +0800189 if ((tar->filter_map & LOG_FILTER_ALL) != 0)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800190 output = 1;
Harald Welte3ae27582010-03-26 21:24:24 +0800191 else if (log_info->filter_fn)
192 output = log_info->filter_fn(&log_context,
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800193 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
Harald Welte3ae27582010-03-26 21:24:24 +0800210void logp(unsigned int subsys, char *file, int line, int cont,
211 const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800212{
213 va_list ap;
214
215 va_start(ap, format);
Harald Welte3ae27582010-03-26 21:24:24 +0800216 _logp(subsys, LOGL_DEBUG, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800217 va_end(ap);
218}
219
Harald Welte3ae27582010-03-26 21:24:24 +0800220void logp2(unsigned int subsys, unsigned int level, char *file, int line, int cont, const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800221{
222 va_list ap;
223
224 va_start(ap, format);
Harald Welte3ae27582010-03-26 21:24:24 +0800225 _logp(subsys, level, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800226 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
Harald Welte3ae27582010-03-26 21:24:24 +0800248void log_add_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800249{
250 llist_add_tail(&target->entry, &target_list);
251}
252
Harald Welte3ae27582010-03-26 21:24:24 +0800253void log_del_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800254{
255 llist_del(&target->entry);
256}
257
Harald Welte3ae27582010-03-26 21:24:24 +0800258void log_reset_context(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800259{
Harald Welte3ae27582010-03-26 21:24:24 +0800260 memset(&log_context, 0, sizeof(log_context));
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800261}
262
Harald Welte3ae27582010-03-26 21:24:24 +0800263int log_set_context(uint8_t ctx_nr, void *value)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800264{
Harald Welte3ae27582010-03-26 21:24:24 +0800265 if (ctx_nr > LOG_MAX_CTX)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800266 return -EINVAL;
267
Harald Welte3ae27582010-03-26 21:24:24 +0800268 log_context.ctx[ctx_nr] = value;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800269
270 return 0;
271}
272
Harald Welte3ae27582010-03-26 21:24:24 +0800273void log_set_all_filter(struct log_target *target, int all)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800274{
275 if (all)
Harald Welte3ae27582010-03-26 21:24:24 +0800276 target->filter_map |= LOG_FILTER_ALL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800277 else
Harald Welte3ae27582010-03-26 21:24:24 +0800278 target->filter_map &= ~LOG_FILTER_ALL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800279}
280
Harald Welte3ae27582010-03-26 21:24:24 +0800281void log_set_use_color(struct log_target *target, int use_color)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800282{
283 target->use_color = use_color;
284}
285
Harald Welte3ae27582010-03-26 21:24:24 +0800286void log_set_print_timestamp(struct log_target *target, int print_timestamp)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800287{
288 target->print_timestamp = print_timestamp;
289}
290
Harald Welte3ae27582010-03-26 21:24:24 +0800291void log_set_log_level(struct log_target *target, int log_level)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800292{
293 target->loglevel = log_level;
294}
295
Harald Welte3ae27582010-03-26 21:24:24 +0800296void log_set_category_filter(struct log_target *target, int category,
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800297 int enable, int level)
298{
Harald Welte3ae27582010-03-26 21:24:24 +0800299 if (category >= log_info->num_cat)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800300 return;
301 target->categories[category].enabled = !!enable;
302 target->categories[category].loglevel = level;
303}
304
Harald Welte3ae27582010-03-26 21:24:24 +0800305static void _stderr_output(struct log_target *target, const char *log)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800306{
307 fprintf(target->tgt_stdout.out, "%s", log);
308 fflush(target->tgt_stdout.out);
309}
310
Harald Welte3ae27582010-03-26 21:24:24 +0800311struct log_target *log_target_create(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800312{
Harald Welte3ae27582010-03-26 21:24:24 +0800313 struct log_target *target;
Harald Weltecc6313c2010-03-26 22:04:03 +0800314 unsigned int i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800315
Harald Welte3ae27582010-03-26 21:24:24 +0800316 target = talloc_zero(tall_log_ctx, struct log_target);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800317 if (!target)
318 return NULL;
319
320 INIT_LLIST_HEAD(&target->entry);
Harald Weltecc6313c2010-03-26 22:04:03 +0800321
322 /* initialize the per-category enabled/loglevel from defaults */
323 for (i = 0; i < log_info->num_cat; i++) {
324 struct log_category *cat = &target->categories[i];
325 cat->enabled = log_info->cat[i].enabled;
326 cat->loglevel = log_info->cat[i].loglevel;
327 }
328
329 /* global settings */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800330 target->use_color = 1;
331 target->print_timestamp = 0;
Harald Weltecc6313c2010-03-26 22:04:03 +0800332
333 /* global log level */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800334 target->loglevel = 0;
335 return target;
336}
337
Harald Welte3ae27582010-03-26 21:24:24 +0800338struct log_target *log_target_create_stderr(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800339{
Harald Welte3ae27582010-03-26 21:24:24 +0800340 struct log_target *target;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800341
Harald Welte3ae27582010-03-26 21:24:24 +0800342 target = log_target_create();
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800343 if (!target)
344 return NULL;
345
346 target->tgt_stdout.out = stderr;
347 target->output = _stderr_output;
348 return target;
349}
350
Harald Welte3ae27582010-03-26 21:24:24 +0800351void log_init(const struct log_info *cat)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800352{
Harald Welte3ae27582010-03-26 21:24:24 +0800353 tall_log_ctx = talloc_named_const(NULL, 1, "logging");
354 log_info = cat;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800355}