libosmocore  0.6.3
Osmocom core library
logging.h
Go to the documentation of this file.
1 #ifndef _OSMOCORE_LOGGING_H
2 #define _OSMOCORE_LOGGING_H
3 
10 #include <stdio.h>
11 #include <stdint.h>
12 #include <stdarg.h>
13 #include <osmocom/core/linuxlist.h>
14 
16 #define LOG_MAX_CTX 8
17 
18 #define LOG_MAX_FILTERS 8
19 
20 #define DEBUG
21 
22 #ifdef DEBUG
23 #define DEBUGP(ss, fmt, args...) logp(ss, __FILE__, __LINE__, 0, fmt, ## args)
24 #define DEBUGPC(ss, fmt, args...) logp(ss, __FILE__, __LINE__, 1, fmt, ## args)
25 #else
26 #define DEBUGP(xss, fmt, args...)
27 #define DEBUGPC(ss, fmt, args...)
28 #endif
29 
30 
31 void osmo_vlogp(int subsys, int level, const char *file, int line,
32  int cont, const char *format, va_list ap);
33 
34 void logp(int subsys, const char *file, int line, int cont, const char *format, ...) __attribute__ ((format (printf, 5, 6)));
35 
42 #define LOGP(ss, level, fmt, args...) \
43  logp2(ss, level, __FILE__, __LINE__, 0, fmt, ##args)
44 
51 #define LOGPC(ss, level, fmt, args...) \
52  logp2(ss, level, __FILE__, __LINE__, 1, fmt, ##args)
53 
55 #define LOGL_DEBUG 1
56 #define LOGL_INFO 3
57 #define LOGL_NOTICE 5
58 #define LOGL_ERROR 7
59 #define LOGL_FATAL 8
61 #define LOG_FILTER_ALL 0x0001
62 
63 /* logging levels defined by the library itself */
64 #define DLGLOBAL -1
65 #define DLLAPD -2
66 #define DLINP -3
67 #define DLMUX -4
68 #define DLMI -5
69 #define DLMIB -6
70 #define DLSMS -7
71 #define OSMO_NUM_DLIB 7
72 
73 struct log_category {
74  uint8_t loglevel;
75  uint8_t enabled;
76 };
77 
79 struct log_info_cat {
80  const char *name;
81  const char *color;
82  const char *description;
83  uint8_t loglevel;
84  uint8_t enabled;
85 };
86 
88 struct log_context {
89  void *ctx[LOG_MAX_CTX+1];
90 };
91 
92 struct log_target;
93 
95 typedef int log_filter(const struct log_context *ctx,
96  struct log_target *target);
97 
99 struct log_info {
100  /* \brief filter callback function */
101  log_filter *filter_fn;
102 
104  const struct log_info_cat *cat;
106  unsigned int num_cat;
108  unsigned int num_cat_user;
109 };
110 
118 };
119 
121 struct log_target {
122  struct llist_head entry;
128 
131 
133  uint8_t loglevel;
135  unsigned int use_color:1;
137  unsigned int print_timestamp:1;
139  unsigned int print_filename:1;
140 
143 
144  union {
145  struct {
146  FILE *out;
147  const char *fname;
148  } tgt_file;
149 
150  struct {
151  int priority;
152  int facility;
153  } tgt_syslog;
154 
155  struct {
156  void *vty;
157  } tgt_vty;
158 
159  struct {
160  void *rb;
161  } tgt_rb;
162  };
163 
170  void (*output) (struct log_target *target, unsigned int level,
171  const char *string);
172 };
173 
174 /* use the above macros */
175 void logp2(int subsys, unsigned int level, const char *file,
176  int line, int cont, const char *format, ...)
177  __attribute__ ((format (printf, 6, 7)));
178 int log_init(const struct log_info *inf, void *talloc_ctx);
179 
180 /* context management */
181 void log_reset_context(void);
182 int log_set_context(uint8_t ctx, void *value);
183 
184 /* filter on the targets */
185 void log_set_all_filter(struct log_target *target, int);
186 
187 void log_set_use_color(struct log_target *target, int);
188 void log_set_print_timestamp(struct log_target *target, int);
189 void log_set_print_filename(struct log_target *target, int);
190 void log_set_log_level(struct log_target *target, int log_level);
191 void log_parse_category_mask(struct log_target *target, const char* mask);
192 int log_parse_level(const char *lvl);
193 const char *log_level_str(unsigned int lvl);
194 int log_parse_category(const char *category);
195 void log_set_category_filter(struct log_target *target, int category,
196  int enable, int level);
197 
198 /* management of the targets */
199 struct log_target *log_target_create(void);
200 void log_target_destroy(struct log_target *target);
202 struct log_target *log_target_create_file(const char *fname);
203 struct log_target *log_target_create_syslog(const char *ident, int option,
204  int facility);
205 int log_target_file_reopen(struct log_target *tgt);
206 int log_targets_reopen(void);
207 
208 void log_add_target(struct log_target *target);
209 void log_del_target(struct log_target *target);
210 
211 /* Generate command string for VTY use */
212 const char *log_vty_command_string(const struct log_info *info);
213 const char *log_vty_command_description(const struct log_info *info);
214 
215 struct log_target *log_target_find(int type, const char *fname);
216 extern struct llist_head osmo_log_target_list;
217 
220 #endif /* _OSMOCORE_LOGGING_H */