06d90e52c0d00c015db22269d6642b1bb45347c1
[osmocom-bb.git] / include / osmocom / core / logging.h
1 #ifndef _OSMOCORE_LOGGING_H
2 #define _OSMOCORE_LOGGING_H
3
4 #include <stdio.h>
5 #include <stdint.h>
6 #include <osmocom/core/linuxlist.h>
7
8 #define LOG_MAX_CTX             8
9 #define LOG_MAX_FILTERS 8
10
11 #define DEBUG
12
13 #ifdef DEBUG
14 #define DEBUGP(ss, fmt, args...) logp(ss, __FILE__, __LINE__, 0, fmt, ## args)
15 #define DEBUGPC(ss, fmt, args...) logp(ss, __FILE__, __LINE__, 1, fmt, ## args)
16 #else
17 #define DEBUGP(xss, fmt, args...)
18 #define DEBUGPC(ss, fmt, args...)
19 #endif
20
21
22 void logp(int subsys, char *file, int line, int cont, const char *format, ...) __attribute__ ((format (printf, 5, 6)));
23
24 /* new logging interface */
25 #define LOGP(ss, level, fmt, args...) \
26         logp2(ss, level, __FILE__, __LINE__, 0, fmt, ##args)
27 #define LOGPC(ss, level, fmt, args...) \
28         logp2(ss, level, __FILE__, __LINE__, 1, fmt, ##args)
29
30 /* different levels */
31 #define LOGL_DEBUG      1       /* debugging information */
32 #define LOGL_INFO       3
33 #define LOGL_NOTICE     5       /* abnormal/unexpected condition */
34 #define LOGL_ERROR      7       /* error condition, requires user action */
35 #define LOGL_FATAL      8       /* fatal, program aborted */
36
37 #define LOG_FILTER_ALL  0x0001
38
39 /* logging levels defined by the library itself */
40 #define DLGLOBAL        -1
41 #define DLLAPDM         -2
42 #define DLINP           -3
43 #define DLMUX           -4
44 #define DLMI            -5
45 #define DLMIB           -6
46 #define OSMO_NUM_DLIB   7
47
48 struct log_category {
49         uint8_t loglevel;
50         uint8_t enabled;
51 };
52
53 struct log_info_cat {
54         const char *name;
55         const char *color;
56         const char *description;
57         uint8_t loglevel;
58         uint8_t enabled;
59 };
60
61 /* log context information, passed to filter */
62 struct log_context {
63         void *ctx[LOG_MAX_CTX+1];
64 };
65
66 struct log_target;
67
68 typedef int log_filter(const struct log_context *ctx,
69                        struct log_target *target);
70
71 struct log_info {
72         /* filter callback function */
73         log_filter *filter_fn;
74
75         /* per-category information */
76         struct log_info_cat *cat;
77         unsigned int num_cat;
78         unsigned int num_cat_user;
79 };
80
81 enum log_target_type {
82         LOG_TGT_TYPE_VTY,
83         LOG_TGT_TYPE_SYSLOG,
84         LOG_TGT_TYPE_FILE,
85         LOG_TGT_TYPE_STDERR,
86 };
87
88 struct log_target {
89         struct llist_head entry;
90
91         int filter_map;
92         void *filter_data[LOG_MAX_FILTERS+1];
93
94         struct log_category *categories;
95
96         uint8_t loglevel;
97         unsigned int use_color:1;
98         unsigned int print_timestamp:1;
99
100         enum log_target_type type;
101
102         union {
103                 struct {
104                         FILE *out;
105                         const char *fname;
106                 } tgt_file;
107
108                 struct {
109                         int priority;
110                         int facility;
111                 } tgt_syslog;
112
113                 struct {
114                         void *vty;
115                 } tgt_vty;
116         };
117
118         void (*output) (struct log_target *target, unsigned int level,
119                         const char *string);
120 };
121
122 /* use the above macros */
123 void logp2(int subsys, unsigned int level, char *file,
124            int line, int cont, const char *format, ...)
125                                 __attribute__ ((format (printf, 6, 7)));
126 int log_init(const struct log_info *inf, void *talloc_ctx);
127
128 /* context management */
129 void log_reset_context(void);
130 int log_set_context(uint8_t ctx, void *value);
131
132 /* filter on the targets */
133 void log_set_all_filter(struct log_target *target, int);
134
135 void log_set_use_color(struct log_target *target, int);
136 void log_set_print_timestamp(struct log_target *target, int);
137 void log_set_log_level(struct log_target *target, int log_level);
138 void log_parse_category_mask(struct log_target *target, const char* mask);
139 int log_parse_level(const char *lvl);
140 const char *log_level_str(unsigned int lvl);
141 int log_parse_category(const char *category);
142 void log_set_category_filter(struct log_target *target, int category,
143                                int enable, int level);
144
145 /* management of the targets */
146 struct log_target *log_target_create(void);
147 void log_target_destroy(struct log_target *target);
148 struct log_target *log_target_create_stderr(void);
149 struct log_target *log_target_create_file(const char *fname);
150 struct log_target *log_target_create_syslog(const char *ident, int option,
151                                             int facility);
152 int log_target_file_reopen(struct log_target *tgt);
153
154 void log_add_target(struct log_target *target);
155 void log_del_target(struct log_target *target);
156
157 /* Generate command string for VTY use */
158 const char *log_vty_command_string(const struct log_info *info);
159 const char *log_vty_command_description(const struct log_info *info);
160
161 struct log_target *log_target_find(int type, const char *fname);
162 extern struct llist_head osmo_log_target_list;
163
164 #endif /* _OSMOCORE_LOGGING_H */