libosmocore  1.4.0
Osmocom core library
logging.h
Go to the documentation of this file.
1 #pragma once
2 
7 #include <stdio.h>
8 #include <stdint.h>
9 #include <stdarg.h>
10 #include <stdbool.h>
11 #include <osmocom/core/defs.h>
12 #include <osmocom/core/linuxlist.h>
13 
15 #define LOG_MAX_CTX 8
17 #define LOG_MAX_FILTERS 8
18 
19 #ifndef DEBUG
20 #define DEBUG
21 #endif
22 
23 #ifdef DEBUG
29 #define DEBUGP(ss, fmt, args...) LOGP(ss, LOGL_DEBUG, fmt, ##args)
30 #define DEBUGPC(ss, fmt, args...) LOGPC(ss, LOGL_DEBUG, fmt, ##args)
31 #else
32 #define DEBUGP(xss, fmt, args...)
33 #define DEBUGPC(ss, fmt, args...)
34 #endif
35 
36 
37 void osmo_vlogp(int subsys, int level, const char *file, int line,
38  int cont, const char *format, va_list ap);
39 
40 void logp(int subsys, const char *file, int line, int cont, const char *format, ...) OSMO_DEPRECATED("Use DEBUGP* macros instead");
41 
48 #define LOGP(ss, level, fmt, args...) \
49  LOGPSRC(ss, level, NULL, 0, fmt, ## args)
50 
57 #define LOGPC(ss, level, fmt, args...) \
58  do { \
59  if (log_check_level(ss, level)) \
60  logp2(ss, level, __FILE__, __LINE__, 1, fmt, ##args); \
61  } while(0)
62 
75 #define LOGPSRC(ss, level, caller_file, caller_line, fmt, args...) \
76  LOGPSRCC(ss, level, caller_file, caller_line, 0, fmt, ##args)
77 
91 #define LOGPSRCC(ss, level, caller_file, caller_line, cont, fmt, args...) \
92  do { \
93  if (log_check_level(ss, level)) {\
94  if (caller_file) \
95  logp2(ss, level, caller_file, caller_line, cont, fmt, ##args); \
96  else \
97  logp2(ss, level, __FILE__, __LINE__, cont, fmt, ##args); \
98  }\
99  } while(0)
100 
102 #define LOGL_DEBUG 1
103 #define LOGL_INFO 3
104 #define LOGL_NOTICE 5
105 #define LOGL_ERROR 7
106 #define LOGL_FATAL 8
108 /* logging subsystems defined by the library itself */
109 #define DLGLOBAL -1
110 #define DLLAPD -2
111 #define DLINP -3
112 #define DLMUX -4
113 #define DLMI -5
114 #define DLMIB -6
115 #define DLSMS -7
116 #define DLCTRL -8
117 #define DLGTP -9
118 #define DLSTATS -10
119 #define DLGSUP -11
120 #define DLOAP -12
121 #define DLSS7 -13
122 #define DLSCCP -14
123 #define DLSUA -15
124 #define DLM3UA -16
125 #define DLMGCP -17
126 #define DLJIBUF -18
127 #define DLRSPRO -19
128 #define OSMO_NUM_DLIB 19
130 /* Colors that can be used in log_info_cat.color */
131 #define OSMO_LOGCOLOR_NORMAL NULL
132 #define OSMO_LOGCOLOR_RED "\033[1;31m"
133 #define OSMO_LOGCOLOR_GREEN "\033[1;32m"
134 #define OSMO_LOGCOLOR_YELLOW "\033[1;33m"
135 #define OSMO_LOGCOLOR_BLUE "\033[1;34m"
136 #define OSMO_LOGCOLOR_PURPLE "\033[1;35m"
137 #define OSMO_LOGCOLOR_CYAN "\033[1;36m"
138 #define OSMO_LOGCOLOR_DARKRED "\033[31m"
139 #define OSMO_LOGCOLOR_DARKGREEN "\033[32m"
140 #define OSMO_LOGCOLOR_DARKYELLOW "\033[33m"
141 #define OSMO_LOGCOLOR_DARKBLUE "\033[34m"
142 #define OSMO_LOGCOLOR_DARKPURPLE "\033[35m"
143 #define OSMO_LOGCOLOR_DARKCYAN "\033[36m"
144 #define OSMO_LOGCOLOR_DARKGREY "\033[1;30m"
145 #define OSMO_LOGCOLOR_GREY "\033[37m"
146 #define OSMO_LOGCOLOR_BRIGHTWHITE "\033[1;37m"
147 #define OSMO_LOGCOLOR_END "\033[0;m"
148 
150 struct log_category {
151  uint8_t loglevel;
152  uint8_t enabled;
153 };
154 
156 struct log_info_cat {
157  const char *name;
158  const char *color;
159  const char *description;
160  uint8_t loglevel;
161  uint8_t enabled;
162 };
163 
165 struct log_context {
166  void *ctx[LOG_MAX_CTX+1];
167 };
168 
178 };
179 
191 };
192 
194 #define LOG_FILTER_ALL (1<<LOG_FLT_ALL)
196 #define GPRS_CTX_NSVC LOG_CTX_GB_NSVC
198 #define GPRS_CTX_BVC LOG_CTX_GB_BVC
202 struct log_target;
203 
205 typedef int log_filter(const struct log_context *ctx,
206  struct log_target *target);
207 
208 struct log_info;
209 struct vty;
210 struct gsmtap_inst;
211 
212 typedef void log_print_filters(struct vty *vty,
213  const struct log_info *info,
214  const struct log_target *tgt);
215 
216 typedef void log_save_filters(struct vty *vty,
217  const struct log_info *info,
218  const struct log_target *tgt);
219 
221 struct log_info {
222  /* filter callback function */
224 
226  const struct log_info_cat *cat;
228  unsigned int num_cat;
230  unsigned int num_cat_user;
231 
236 };
237 
246 };
247 
253 };
254 
259 };
260 
262 struct log_target {
263  struct llist_head entry;
269 
272 
274  uint8_t loglevel;
276  unsigned int use_color:1;
278  unsigned int print_timestamp:1;
280  unsigned int print_filename:1;
282  unsigned int print_category:1;
284  unsigned int print_ext_timestamp:1;
285 
287  enum log_target_type type;
288 
289  union {
290  struct {
291  FILE *out;
292  const char *fname;
294 
295  struct {
296  int priority;
297  int facility;
299 
300  struct {
301  void *vty;
303 
304  struct {
305  void *rb;
307 
308  struct {
310  const char *ident;
311  const char *hostname;
313  };
314 
321  void (*output) (struct log_target *target, unsigned int level,
322  const char *string);
323 
336  void (*raw_output)(struct log_target *target, int subsys,
337  unsigned int level, const char *file, int line,
338  int cont, const char *format, va_list ap);
339 
340  /* Should the log level be printed? */
342  /* Should we print the subsys in hex like '<000b>'? */
344  /* Should we print the source file and line, and in which way? */
346  /* Where on a log line to put the source file info. */
348 };
349 
350 /* use the above macros */
351 void logp2(int subsys, unsigned int level, const char *file,
352  int line, int cont, const char *format, ...)
353  __attribute__ ((format (printf, 6, 7)));
354 int log_init(const struct log_info *inf, void *talloc_ctx);
355 void log_fini(void);
356 int log_check_level(int subsys, unsigned int level);
357 
358 /* context management */
359 void log_reset_context(void);
360 int log_set_context(uint8_t ctx, void *value);
361 
362 /* filter on the targets */
363 void log_set_all_filter(struct log_target *target, int);
364 
365 void log_set_use_color(struct log_target *target, int);
366 void log_set_print_extended_timestamp(struct log_target *target, int);
367 void log_set_print_timestamp(struct log_target *target, int);
368 void log_set_print_filename(struct log_target *target, int);
369 void log_set_print_filename2(struct log_target *target, enum log_filename_type lft);
370 void log_set_print_filename_pos(struct log_target *target, enum log_filename_pos pos);
371 void log_set_print_category(struct log_target *target, int);
372 void log_set_print_category_hex(struct log_target *target, int);
373 void log_set_print_level(struct log_target *target, int);
374 void log_set_log_level(struct log_target *target, int log_level);
375 void log_parse_category_mask(struct log_target *target, const char* mask);
376 const char* log_category_name(int subsys);
378 const char *log_level_str(unsigned int lvl) OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE;
379 int log_parse_category(const char *category);
380 void log_set_category_filter(struct log_target *target, int category,
381  int enable, int level);
382 
383 /* management of the targets */
384 struct log_target *log_target_create(void);
385 void log_target_destroy(struct