blob: 70e8f14969a34bff11c8c9de7081456aa109054d [file] [log] [blame]
Harald Welte3ae27582010-03-26 21:24:24 +08001#ifndef _OSMOCORE_LOGGING_H
2#define _OSMOCORE_LOGGING_H
3
4#include <stdio.h>
5#include <stdint.h>
6#include <osmocore/linuxlist.h>
7
8#define LOG_MAX_CATEGORY 32
9#define LOG_MAX_CTX 8
10#define LOG_MAX_FILTERS 8
11
12#define DEBUG
13
14#ifdef DEBUG
15#define DEBUGP(ss, fmt, args...) logp(ss, __FILE__, __LINE__, 0, fmt, ## args)
16#define DEBUGPC(ss, fmt, args...) logp(ss, __FILE__, __LINE__, 1, fmt, ## args)
17#else
18#define DEBUGP(xss, fmt, args...)
19#define DEBUGPC(ss, fmt, args...)
20#endif
21
22#define static_assert(exp, name) typedef int dummy##name [(exp) ? 1 : -1];
23
Harald Welte3ae27582010-03-26 21:24:24 +080024void logp(unsigned int subsys, char *file, int line, int cont, const char *format, ...) __attribute__ ((format (printf, 5, 6)));
25
26/* new logging interface */
27#define LOGP(ss, level, fmt, args...) \
28 logp2(ss, level, __FILE__, __LINE__, 0, fmt, ##args)
29#define LOGPC(ss, level, fmt, args...) \
30 logp2(ss, level, __FILE__, __LINE__, 1, fmt, ##args)
31
32/* different levels */
33#define LOGL_DEBUG 1 /* debugging information */
34#define LOGL_INFO 3
35#define LOGL_NOTICE 5 /* abnormal/unexpected condition */
36#define LOGL_ERROR 7 /* error condition, requires user action */
37#define LOGL_FATAL 8 /* fatal, program aborted */
38
39#define LOG_FILTER_ALL 0x0001
40
41struct log_category {
42 uint8_t loglevel;
43 uint8_t enabled;
44};
45
46struct log_info_cat {
47 const char *name;
48 const char *color;
49 const char *description;
50 uint8_t loglevel;
51 uint8_t enabled;
52};
53
54/* log context information, passed to filter */
55struct log_context {
56 void *ctx[LOG_MAX_CTX+1];
57};
58
59struct log_target;
60
61typedef int log_filter(const struct log_context *ctx,
62 struct log_target *target);
63
64struct log_info {
65 /* filter callback function */
66 log_filter *filter_fn;
67
68 /* per-category information */
69 const struct log_info_cat *cat;
70 unsigned int num_cat;
71};
72
73struct log_target {
74 struct llist_head entry;
75
76 int filter_map;
77 void *filter_data[LOG_MAX_FILTERS+1];
78
79 struct log_category categories[LOG_MAX_CATEGORY+1];
80 uint8_t loglevel;
81 int use_color:1;
82 int print_timestamp:1;
83
84 union {
85 struct {
86 FILE *out;
Harald Welte0083cd32010-08-25 14:55:44 +020087 } tgt_file;
Harald Welte3ae27582010-03-26 21:24:24 +080088
89 struct {
90 int priority;
91 } tgt_syslog;
92
93 struct {
94 void *vty;
95 } tgt_vty;
96 };
97
98 void (*output) (struct log_target *target, const char *string);
99};
100
101/* use the above macros */
102void logp2(unsigned int subsys, unsigned int level, char *file,
103 int line, int cont, const char *format, ...)
104 __attribute__ ((format (printf, 6, 7)));
105void log_init(const struct log_info *cat);
106
107/* context management */
108void log_reset_context(void);
109int log_set_context(uint8_t ctx, void *value);
110
111/* filter on the targets */
112void log_set_all_filter(struct log_target *target, int);
113
114void log_set_use_color(struct log_target *target, int);
115void log_set_print_timestamp(struct log_target *target, int);
116void log_set_log_level(struct log_target *target, int log_level);
117void log_parse_category_mask(struct log_target *target, const char* mask);
118int log_parse_level(const char *lvl);
Harald Welte9ac22252010-05-11 11:19:40 +0200119const char *log_level_str(unsigned int lvl);
Harald Welte3ae27582010-03-26 21:24:24 +0800120int log_parse_category(const char *category);
121void log_set_category_filter(struct log_target *target, int category,
122 int enable, int level);
123
124/* management of the targets */
125struct log_target *log_target_create(void);
126struct log_target *log_target_create_stderr(void);
127void log_add_target(struct log_target *target);
128void log_del_target(struct log_target *target);
129
Harald Welte7638af92010-05-11 16:39:22 +0200130/* Gernerate command argument strings for VTY use */
131const char *log_vty_category_string(struct log_info *info);
132const char *log_vty_level_string(struct log_info *info);
133
Harald Welte3ae27582010-03-26 21:24:24 +0800134#endif /* _OSMOCORE_LOGGING_H */