Import 'debug' support from OpenBSC into libosmocore
[osmocom-bb.git] / include / osmocore / debug.h
1 #ifndef _OSMOCORE_DEBUG_H
2 #define _OSMOCORE_DEBUG_H
3
4 #include <stdio.h>
5 #include <stdint.h>
6 #include <osmocore/linuxlist.h>
7
8 #define DEBUG_MAX_CATEGORY      32
9 #define DEBUG_MAX_CTX           8
10 #define DEBUG_MAX_FILTERS       8
11
12 #define DEBUG
13
14 #ifdef DEBUG
15 #define DEBUGP(ss, fmt, args...) debugp(ss, __FILE__, __LINE__, 0, fmt, ## args)
16 #define DEBUGPC(ss, fmt, args...) debugp(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
24 char *hexdump(const unsigned char *buf, int len);
25 void debugp(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         debugp2(ss, level, __FILE__, __LINE__, 0, fmt, ##args)
30 #define LOGPC(ss, level, fmt, args...) \
31         debugp2(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 DEBUG_FILTER_ALL        0x0001
41
42 struct debug_category {
43         uint8_t loglevel;
44         uint8_t enabled;
45 };
46
47 struct debug_info_cat {
48         const char *name;
49         const char *color;
50         const char *description;
51         int number;
52         uint8_t loglevel;
53         uint8_t enabled;
54 };
55
56 /* debug context information, passed to filter */
57 struct debug_context {
58         void *ctx[DEBUG_MAX_CTX+1];
59 };
60
61 struct debug_target;
62
63 typedef int debug_filter(const struct debug_context *ctx,
64                          struct debug_target *target);
65
66 struct debug_info {
67         /* filter callback function */
68         debug_filter *filter_fn;
69
70         /* per-category information */
71         const struct debug_info_cat *cat;
72         unsigned int num_cat;
73 };
74
75 struct debug_target {
76         struct llist_head entry;
77
78         int filter_map;
79         void *filter_data[DEBUG_MAX_FILTERS+1];
80
81         struct debug_category categories[DEBUG_MAX_CATEGORY+1];
82         uint8_t loglevel;
83         int use_color:1;
84         int print_timestamp:1;
85
86         union {
87                 struct {
88                         FILE *out;
89                 } tgt_stdout;
90
91                 struct {
92                         int priority;
93                 } tgt_syslog;
94
95                 struct {
96                         void *vty;
97                 } tgt_vty;
98         };
99
100         void (*output) (struct debug_target *target, const char *string);
101 };
102
103 /* use the above macros */
104 void debugp2(unsigned int subsys, unsigned int level, char *file,
105              int line, int cont, const char *format, ...)
106                                 __attribute__ ((format (printf, 6, 7)));
107 void debug_init(const struct debug_info *cat);
108
109 /* context management */
110 void debug_reset_context(void);
111 int debug_set_context(uint8_t ctx, void *value);
112
113 /* filter on the targets */
114 void debug_set_all_filter(struct debug_target *target, int);
115
116 void debug_set_use_color(struct debug_target *target, int);
117 void debug_set_print_timestamp(struct debug_target *target, int);
118 void debug_set_log_level(struct debug_target *target, int log_level);
119 void debug_parse_category_mask(struct debug_target *target, const char* mask);
120 int debug_parse_level(const char *lvl);
121 int debug_parse_category(const char *category);
122 void debug_set_category_filter(struct debug_target *target, int category,
123                                int enable, int level);
124
125 /* management of the targets */
126 struct debug_target *debug_target_create(void);
127 struct debug_target *debug_target_create_stderr(void);
128 void debug_add_target(struct debug_target *target);
129 void debug_del_target(struct debug_target *target);
130
131 #endif /* _OSMOCORE_DEBUG_H */