1 /* Debugging/Logging support code */
3 /* (C) 2008-2010 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2008 by Holger Hans Peter Freyther <zecke@selfish.org>
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.
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.
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.
23 #include "../config.h"
36 #include <osmocore/talloc.h>
37 #include <osmocore/utils.h>
38 #include <osmocore/logging.h>
40 static const struct log_info *log_info;
42 static struct log_context log_context;
43 static void *tall_log_ctx = NULL;
44 static LLIST_HEAD(target_list);
46 static const struct value_string loglevel_strs[] = {
48 { LOGL_DEBUG, "DEBUG" },
49 { LOGL_INFO, "INFO" },
50 { LOGL_NOTICE, "NOTICE" },
51 { LOGL_ERROR, "ERROR" },
52 { LOGL_FATAL, "FATAL" },
56 int log_parse_level(const char *lvl)
58 return get_string_value(loglevel_strs, lvl);
61 int log_parse_category(const char *category)
65 for (i = 0; i < log_info->num_cat; ++i) {
66 if (!strcasecmp(log_info->cat[i].name+1, category))
74 * Parse the category mask.
75 * The format can be this: category1:category2:category3
76 * or category1,2:category2,3:...
78 void log_parse_category_mask(struct log_target* target, const char *_mask)
81 char *mask = strdup(_mask);
82 char *category_token = NULL;
84 /* Disable everything to enable it afterwards */
85 for (i = 0; i < ARRAY_SIZE(target->categories); ++i)
86 target->categories[i].enabled = 0;
88 category_token = strtok(mask, ":");
90 for (i = 0; i < log_info->num_cat; ++i) {
91 char* colon = strstr(category_token, ",");
92 int length = strlen(category_token);
95 length = colon - category_token;
97 if (strncasecmp(log_info->cat[i].name, category_token,
102 level = atoi(colon+1);
104 target->categories[i].enabled = 1;
105 target->categories[i].loglevel = level;
108 } while ((category_token = strtok(NULL, ":")));
113 static const char* color(int subsys)
115 if (subsys < log_info->num_cat)
116 return log_info->cat[subsys].color;
121 static void _output(struct log_target *target, unsigned int subsys,
122 char *file, int line, int cont, const char *format,
131 /* prepare the data */
137 /* are we using color */
138 if (target->use_color) {
139 const char *c = color(subsys);
141 snprintf(col, sizeof(col), "%s", color(subsys));
142 col[sizeof(col)-1] = '\0';
145 vsnprintf(buf, sizeof(buf), format, ap);
146 buf[sizeof(buf)-1] = '\0';
149 if (target->print_timestamp) {
153 timestr = ctime(&tm);
154 timestr[strlen(timestr)-1] = '\0';
155 snprintf(tim, sizeof(tim), "%s ", timestr);
156 tim[sizeof(tim)-1] = '\0';
158 snprintf(sub, sizeof(sub), "<%4.4x> %s:%d ", subsys, file, line);
159 sub[sizeof(sub)-1] = '\0';
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);
168 static void _logp(unsigned int subsys, int level, char *file, int line,
169 int cont, const char *format, va_list ap)
171 struct log_target *tar;
173 llist_for_each_entry(tar, &target_list, entry) {
174 struct log_category *category;
177 category = &tar->categories[subsys];
178 /* subsystem is not supposed to be logged */
179 if (!category->enabled)
182 /* Check the global log level */
183 if (tar->loglevel != 0 && level < tar->loglevel)
186 /* Check the category log level */
187 if (tar->loglevel == 0 && category->loglevel != 0 &&
188 level < category->loglevel)
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)
196 else if (log_info->filter_fn)
197 output = log_info->filter_fn(&log_context,
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 */
209 _output(tar, subsys, file, line, cont, format, bp);
215 void logp(unsigned int subsys, char *file, int line, int cont,
216 const char *format, ...)
220 va_start(ap, format);
221 _logp(subsys, LOGL_DEBUG, file, line, cont, format, ap);
225 void logp2(unsigned int subsys, unsigned int level, char *file, int line, int cont, const char *format, ...)
229 va_start(ap, format);
230 _logp(subsys, level, file, line, cont, format, ap);
234 static char hexd_buff[4096];
236 char *hexdump(const unsigned char *buf, int len)
239 char *cur = hexd_buff;
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]);
249 hexd_buff[sizeof(hexd_buff)-1] = 0;
253 void log_add_target(struct log_target *target)
255 llist_add_tail(&target->entry, &target_list);
258 void log_del_target(struct log_target *target)
260 llist_del(&target->entry);
263 void log_reset_context(void)
265 memset(&log_context, 0, sizeof(log_context));
268 int log_set_context(uint8_t ctx_nr, void *value)
270 if (ctx_nr > LOG_MAX_CTX)
273 log_context.ctx[ctx_nr] = value;
278 void log_set_all_filter(struct log_target *target, int all)
281 target->filter_map |= LOG_FILTER_ALL;
283 target->filter_map &= ~LOG_FILTER_ALL;
286 void log_set_use_color(struct log_target *target, int use_color)
288 target->use_color = use_color;
291 void log_set_print_timestamp(struct log_target *target, int print_timestamp)
293 target->print_timestamp = print_timestamp;
296 void log_set_log_level(struct log_target *target, int log_level)
298 target->loglevel = log_level;
301 void log_set_category_filter(struct log_target *target, int category,
302 int enable, int level)
304 if (category >= log_info->num_cat)
306 target->categories[category].enabled = !!enable;
307 target->categories[category].loglevel = level;
310 static void _stderr_output(struct log_target *target, const char *log)
312 fprintf(target->tgt_stdout.out, "%s", log);
313 fflush(target->tgt_stdout.out);
316 struct log_target *log_target_create(void)
318 struct log_target *target;
321 target = talloc_zero(tall_log_ctx, struct log_target);
325 INIT_LLIST_HEAD(&target->entry);
327 /* initialize the per-category enabled/loglevel from defaults */
328 for (i = 0; i < log_info->num_cat; i++) {
329 struct log_category *cat = &target->categories[i];
330 cat->enabled = log_info->cat[i].enabled;
331 cat->loglevel = log_info->cat[i].loglevel;
334 /* global settings */
335 target->use_color = 1;
336 target->print_timestamp = 0;
338 /* global log level */
339 target->loglevel = 0;
343 struct log_target *log_target_create_stderr(void)
345 struct log_target *target;
347 target = log_target_create();
351 target->tgt_stdout.out = stderr;
352 target->output = _stderr_output;
356 void log_init(const struct log_info *cat)
358 tall_log_ctx = talloc_named_const(NULL, 1, "logging");