blob: b9e0c8fd0d4da7e3cd9c9fb5ed350cb456ac84c2 [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
24char *hexdump(const unsigned char *buf, int len);
25void logp(unsigned int subsys, char *file, int line, int cont, const char *format, ...) __attribute__ ((format (printf, 5, 6)));
26
27/* new logging interface */
28#define LOGP(ss, level, fmt, args...) \
29 logp2(ss, level, __FILE__, __LINE__, 0, fmt, ##args)
30#define LOGPC(ss, level, fmt, args...) \
31 logp2(ss, level, __FILE__, __LINE__, 1, fmt, ##args)
32
33/* different levels */
34#define LOGL_DEBUG 1 /* debugging information */
35#define LOGL_INFO 3
36#define LOGL_NOTICE 5 /* abnormal/unexpected condition */
37#define LOGL_ERROR 7 /* error condition, requires user action */
38#define LOGL_FATAL 8 /* fatal, program aborted */
39
40#define LOG_FILTER_ALL 0x0001
41
42struct log_category {
43 uint8_t loglevel;
44 uint8_t enabled;
45};
46
47struct log_info_cat {
48 const char *name;
49 const char *color;
50 const char *description;
51 uint8_t loglevel;
52 uint8_t enabled;
53};
54
55/* log context information, passed to filter */
56struct log_context {
57 void *ctx[LOG_MAX_CTX+1];
58};
59
60struct log_target;
61
62typedef int log_filter(const struct log_context *ctx,
63 struct log_target *target);
64
65struct log_info {
66 /* filter callback function */
67 log_filter *filter_fn;
68
69 /* per-category information */
70 const struct log_info_cat *cat;
71 unsigned int num_cat;
72};
73
74struct log_target {
75 struct llist_head entry;
76
77 int filter_map;
78 void *filter_data[LOG_MAX_FILTERS+1];
79
80 struct log_category categories[LOG_MAX_CATEGORY+1];
81 uint8_t loglevel;
82 int use_color:1;
83 int print_timestamp:1;
84
85 union {
86 struct {
87 FILE *out;
88 } tgt_stdout;
89
90 struct {
91 int priority;
92 } tgt_syslog;
93
94 struct {
95 void *vty;
96 } tgt_vty;
97 };
98
99 void (*output) (struct log_target *target, const char *string);
100};
101
102/* use the above macros */
103void logp2(unsigned int subsys, unsigned int level, char *file,
104 int line, int cont, const char *format, ...)
105 __attribute__ ((format (printf, 6, 7)));
106void log_init(const struct log_info *cat);
107
108/* context management */
109void log_reset_context(void);
110int log_set_context(uint8_t ctx, void *value);
111
112/* filter on the targets */
113void log_set_all_filter(struct log_target *target, int);
114
115void log_set_use_color(struct log_target *target, int);
116void log_set_print_timestamp(struct log_target *target, int);
117void log_set_log_level(struct log_target *target, int log_level);
118void log_parse_category_mask(struct log_target *target, const char* mask);
119int log_parse_level(const char *lvl);
Harald Welte9ac22252010-05-11 11:19:40 +0200120const char *log_level_str(unsigned int lvl);
Harald Welte3ae27582010-03-26 21:24:24 +0800121int log_parse_category(const char *category);
122void log_set_category_filter(struct log_target *target, int category,
123 int enable, int level);
124
125/* management of the targets */
126struct log_target *log_target_create(void);
127struct log_target *log_target_create_stderr(void);
128void log_add_target(struct log_target *target);
129void log_del_target(struct log_target *target);
130
131#endif /* _OSMOCORE_LOGGING_H */