libosmocore  0.12.1
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
16 
17 #define LOG_MAX_FILTERS 8
18 
19 #define DEBUG
20 
21 #ifdef DEBUG
22 
27 #define DEBUGP(ss, fmt, args...) LOGP(ss, LOGL_DEBUG, fmt, ##args)
28 #define DEBUGPC(ss, fmt, args...) LOGPC(ss, LOGL_DEBUG, fmt, ##args)
29 #else
30 #define DEBUGP(xss, fmt, args...)
31 #define DEBUGPC(ss, fmt, args...)
32 #endif
33 
34 
35 void osmo_vlogp(int subsys, int level, const char *file, int line,
36  int cont, const char *format, va_list ap);
37 
38 void logp(int subsys, const char *file, int line, int cont, const char *format, ...) OSMO_DEPRECATED("Use DEBUGP* macros instead");
39 
46 #define LOGP(ss, level, fmt, args...) \
47  LOGPSRC(ss, level, NULL, 0, fmt, ## args)
48 
55 #define LOGPC(ss, level, fmt, args...) \
56  do { \
57  if (log_check_level(ss, level)) \
58  logp2(ss, level, __BASE_FILE__, __LINE__, 1, fmt, ##args); \
59  } while(0)
60 
73 #define LOGPSRC(ss, level, caller_file, caller_line, fmt, args...) \
74  LOGPSRCC(ss, level, caller_file, caller_line, 0, fmt, ##args)
75 
89 #define LOGPSRCC(ss, level, caller_file, caller_line, cont, fmt, args...) \
90  do { \
91  if (log_check_level(ss, level)) {\
92  if (caller_file) \
93  logp2(ss, level, caller_file, caller_line, cont, fmt, ##args); \
94  else \
95  logp2(ss, level, __BASE_FILE__, __LINE__, cont, fmt, ##args); \
96  }\
97  } while(0)
98 
100 #define LOGL_DEBUG 1
101 #define LOGL_INFO 3
102 #define LOGL_NOTICE 5
103 #define LOGL_ERROR 7
104 #define LOGL_FATAL 8
106 /* logging levels defined by the library itself */
107 #define DLGLOBAL -1
108 #define DLLAPD -2
109 #define DLINP -3
110 #define DLMUX -4
111 #define DLMI -5
112 #define DLMIB -6
113 #define DLSMS -7
114 #define DLCTRL -8
115 #define DLGTP -9
116 #define DLSTATS -10
117 #define DLGSUP -11
118 #define DLOAP -12
119 #define DLSS7 -13
120 #define DLSCCP -14
121 #define DLSUA -15
122 #define DLM3UA -16
123 #define DLMGCP -17
124 #define DLJIBUF -18
125 #define OSMO_NUM_DLIB 18
128 struct log_category {
129  uint8_t loglevel;
130  uint8_t enabled;
131 };
132 
134 struct log_info_cat {
135  const char *name;
136  const char *color;
137  const char *description;
138  uint8_t loglevel;
139  uint8_t enabled;
140 };
141 
143 struct log_context {
144  void *ctx[LOG_MAX_CTX+1];
145 };
146 
155 };
156 
167 };
168 
170 #define LOG_FILTER_ALL (1<<LOG_FLT_ALL)
171 
172 #define GPRS_CTX_NSVC LOG_CTX_GB_NSVC
173 
174 #define GPRS_CTX_BVC LOG_CTX_GB_BVC
175 
178 struct log_target;
179 
181 typedef int log_filter(const struct log_context *ctx,
182  struct log_target *target);
183 
184 struct log_info;
185 struct vty;
186 struct gsmtap_inst;
187 
188 typedef void log_print_filters(struct vty *vty,
189  const struct log_info *info,
190  const struct log_target *tgt);
191 
192 typedef void log_save_filters(struct vty *vty,
193  const struct log_info *info,
194  const struct log_target *tgt);
195 
197 struct log_info {
198  /* filter callback function */
200 
202  const struct log_info_cat *cat;
204  unsigned int num_cat;
206  unsigned int num_cat_user;
207 
212 };
213 
222 };
223 
229 };
230 
232 struct log_target {
233  struct llist_head entry;
238  void *filter_data[LOG_MAX_FILTERS+1];
239 
242 
244  uint8_t loglevel;
246  unsigned int use_color:1;
248  unsigned int print_timestamp:1;
250  unsigned int print_filename:1;
252  unsigned int print_category:1;
254  unsigned int print_ext_timestamp:1;
255 
258 
259  union {
260  struct {
261  FILE *out;
262  const char *fname;
263  } tgt_file;
264 
265  struct {
266  int priority;
267  int facility;
268  } tgt_syslog;
269 
270  struct {
271  void *vty;
272  } tgt_vty;
273 
274  struct {
275  void *rb;
276  } tgt_rb;
277 
278  struct {
280  const char *ident;
281  const char *hostname;
282  } tgt_gsmtap;
283  };
284 
291  void (*output) (struct log_target *target, unsigned int level,
292  const char *string);
293 
306  void (*raw_output)(struct log_target *target, int subsys,
307  unsigned int level, const char *file, int line,
308  int cont, const char *format, va_list ap);
309 
310  /* Should the log level be printed? */
312  /* Should we print the subsys in hex like '<000b>'? */
314  /* Should we print the source file and line, and in which way? */
316 };
317 
318 /* use the above macros */
319 void logp2(int subsys, unsigned int level, const char *file,
320  int line, int cont, const char *format, ...)
321  __attribute__ ((format (printf, 6, 7)));
322 int log_init(const struct log_info *inf, void *talloc_ctx);
323 void log_fini(void);
324 int log_check_level(int subsys, unsigned int level);
325 
326 /* context management */
327 void log_reset_context(void);
328 int log_set_context(uint8_t ctx, void *value);
329 
330 /* filter on the targets */
331 void log_set_all_filter(struct log_target *target, int);
332 
333 void log_set_use_color(struct log_target *target, int);
334 void log_set_print_extended_timestamp(struct log_target *target, int);
335 void log_set_print_timestamp(struct log_target *target, int);
336 void log_set_print_filename(struct log_target *target, int);
337 void log_set_print_filename2(struct log_target *target, enum log_filename_type lft);
338 void log_set_print_category(struct log_target *target, int);
339 void log_set_print_category_hex(struct log_target *target, int);
340 void log_set_print_level(struct log_target *target, int);
341 void log_set_log_level(struct log_target *target, int log_level);
342 void log_parse_category_mask(struct log_target *target, const char* mask);
343 const char* log_category_name(int subsys);
345 const char *log_level_str(unsigned int lvl) OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE;
346 int log_parse_category(const char *category);
347 void log_set_category_filter(struct log_target *target, int category,
348  int enable, int level);
349 
350 /* management of the targets */
351 struct log_target *log_target_create(void);
352 void log_target_destroy(struct log_target *target);
354 struct log_target *log_target_create_file(const char *fname);
355 struct log_target *log_target_create_syslog(const char *ident, int option,
356  int facility);
357 struct log_target *log_target_create_gsmtap(const char *host, uint16_t port,
358  const char *ident,
359  bool ofd_wq_mode,
360  bool add_sink);
361 int log_target_file_reopen(struct log_target *tgt);
362 int log_targets_reopen(void);
363 
364 void log_add_target(struct log_target *target);
365 void log_del_target(struct log_target *target);
366 
367 /* Generate command string for VTY use */
368 const char *log_vty_command_string() OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE;
369 const char *log_vty_command_description() OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE;
370 
371 struct log_target *log_target_find(int type, const char *fname);
372 extern struct llist_head osmo_log_target_list;
373 
void log_set_print_category_hex(struct log_target *target, int)
Enable or disable printing of the category number in hex (&#39;<000b>&#39;).
Definition: logging.c:695
Information regarding one logging category.
Definition: logging.h:134
int filter_map
Internal data for filtering.
Definition: logging.h:236
int log_parse_level(const char *lvl) OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE
Parse a human-readable log level into a numeric value.
Definition: logging.c:209
#define OSMO_DEPRECATED(text)
Set the deprecated attribute with a message.
Definition: defs.h:41
int log_parse_category(const char *category)
parse a human-readable log category into numeric form
Definition: logging.c:227
const char * name
name of category
Definition: logging.h:135
VTY logging.
Definition: logging.h:216
log_save_filters * save_fn
filter saving function
Definition: logging.h:209
Definition: logging.h:166
log_filename_type
Whether/how to log the source filename (and line number).
Definition: logging.h:225
bool print_level
Definition: logging.h:311
void * rb
Definition: logging.h:275
char subsys[16]
logging sub-system
Definition: gsmtap.h:122
void log_set_print_timestamp(struct log_target *target, int)
Enable or disable printing of timestamps while logging.
Definition: logging.c:636
const char * fname
Definition: logging.h:262
void logp(int subsys, const char *file, int line, int cont, const char *format,...) OSMO_DEPRECATED("Use DEBUGP* macros instead")
logging function used by DEBUGP() macro
Definition: logging.c:538
void log_set_print_filename(struct log_target *target, int)
Use log_set_print_filename2() instead.
Definition: logging.c:663
Configuration of single log category / sub-system.
Definition: logging.h:128
void log_set_print_extended_timestamp(struct log_target *target, int)
Enable or disable printing of extended timestamps while logging.
Definition: logging.c:649
log_ctx_index
Indexes to indicate the object currently acted upon.
Definition: logging.h:149
struct rb_root __attribute__
Definition: conv_acc_generic.c:140
Definition: logging.h:151
#define OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE
Definition: defs.h:47
Definition: logging.h:227
struct log_target * log_target_create_gsmtap(const char *host, uint16_t port, const char *ident, bool ofd_wq_mode, bool add_sink)
Create a new logging target for GSMTAP logging.
Definition: logging_gsmtap.c:117
Definition: logging.h:163
uint8_t enabled
is this category enabled or not
Definition: logging.h:139
void log_set_print_filename2(struct log_target *target, enum log_filename_type lft)
Enable or disable printing of the filename while logging.
Definition: logging.c:675
log_filter_index
Indexes to indicate objects that should be logged.
Definition: logging.h:160
Definition: logging.h:161
int log_set_context(uint8_t ctx, void *value)
Set the logging context.
Definition: logging.c:597
syslog based logging
Definition: logging.h:217
const char * ident
Definition: logging.h:280
const char * log_vty_command_string() OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE
Generates the logging command string for VTY.
Definition: logging.c:931
Definition: logging.h:164
General definitions that are meant to be included from header files.
log_target_type
Type of logging target.
Definition: logging.h:215
void log_target_destroy(struct log_target *target)
Unregister, close and delete a log target.
Definition: logging.c:867
#define DEBUGP(ss, fmt, args...)
Log a debug message through the Osmocom logging framework.
Definition: logging.h:27
void * vty
Definition: logging.h:271
#define LOG_MAX_FILTERS
Maximum number of logging filters.
Definition: logging.h:17
struct llist_head osmo_log_target_list
struct log_target * log_target_find(int type, const char *fname)
Find a registered log target.
Definition: logging.c:842
one gsmtap instance
Definition: gsmtap_util.h:23
uint8_t enabled
is logging enabled?
Definition: logging.h:130
enum log_filename_type print_filename2
Definition: logging.h:315
int facility
Definition: logging.h:267
uint8_t level
logging level
Definition: gsmtap.h:119
void log_add_target(struct log_target *target)
Register a new log target with the logging core.
Definition: logging.c:567
int log_filter(const struct log_context *ctx, struct log_target *target)
Log filter function.
Definition: logging.h:181
osmo_strrb-backed logging
Definition: logging.h:220
Definition: logging.h:165
int priority
Definition: logging.h:266
text file logging
Definition: logging.h:218
Definition: logging.h:226
bool print_category_hex
Definition: logging.h:313
const char * log_vty_command_description() OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE
Generates the logging command description for VTY.
Definition: logging.c:1011
void log_del_target(struct log_target *target)
Unregister a log target from the logging core.
Definition: logging.c:575
void(* raw_output)(struct log_target *target, int subsys, unsigned int level, const char *file, int line, int cont, const char *format, va_list ap)
alternative call-back function to which the logging framework passes the unfortmatted input arguments...
Definition: logging.h:306
FILE * out
Definition: logging.h:261
void log_set_all_filter(struct log_target *target, int)
Enable the LOG_FLT_ALL log filter.
Definition: logging.c:615
stderr logging
Definition: logging.h:219
void log_save_filters(struct vty *vty, const struct log_info *info, const struct log_target *tgt)
Definition: logging.h:192
struct gsmtap_inst * gsmtap_inst
Definition: logging.h:279
Simple doubly linked list implementation.
void log_print_filters(struct vty *vty, const struct log_info *info, const struct log_target *tgt)
Definition: logging.h:188
(double) linked list header structure
Definition: linuxlist.h:46
void log_set_use_color(struct log_target *target, int)
Enable or disable the use of colored output.
Definition: logging.c:627
const char * color
color string for cateyory
Definition: logging.h:136
Definition: logging.h:162
void log_set_print_category(struct log_target *target, int)
Enable or disable printing of the category name.
Definition: logging.c:686
log_filter * filter_fn
Definition: logging.h:199
Definition: logging.h:152
int log_target_file_reopen(struct log_target *tgt)
close and re-open a log file (for log file rotation)
Definition: logging.c:893
Definition: logging.h:153
void int log_init(const struct log_info *inf, void *talloc_ctx)
Initialize the Osmocom logging core.
Definition: logging.c:1075
struct log_target * log_target_create_stderr(void)
Create the STDERR log target.
Definition: logging.c:792
const char * log_level_str(unsigned int lvl) OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE
convert a numeric log level into human-readable string
Definition: logging.c:218
struct log_category * categories
logging categories
Definition: logging.h:241
uint8_t loglevel
global log level
Definition: logging.h:244
Definition: logging.h:228
void log_set_category_filter(struct log_target *target, int category, int enable, int level)
Set a category filter on a given log target.
Definition: logging.c:726
GSMTAP network logging.
Definition: logging.h:221
void log_fini(void)
Definition: logging.c:1120
int log_check_level(int subsys, unsigned int level)
Check whether a log entry will be generated.
Definition: logging.c:1135
uint8_t loglevel
configured log-level
Definition: logging.h:129
Definition: logging.h:154
struct log_target * log_target_create_file(const char *fname)
Create a new file-based log target.
Definition: logging.c:816
Logging configuration, passed to log_init.
Definition: logging.h:197
uint8_t type
see GSMTAP_TYPE_*
Definition: gsmtap.h:115
void log_parse_category_mask(struct log_target *target, const char *mask)
parse the log category mask
Definition: logging.c:250
Definition: logging.h:150
log_print_filters * print_fn
filter saving function
Definition: logging.h:211
unsigned int num_cat_user
total number of user categories (not library)
Definition: logging.h:206
const char * log_category_name(int subsys)
Definition: logging.c:323
Log context information, passed to filter.
Definition: logging.h:143
const struct log_info_cat * cat
per-category information
Definition: logging.h:202
struct log_target * log_target_create_syslog(const char *ident, int option, int facility)
Create a new logging target for syslog logging.
Definition: logging_syslog.c:73
struct log_target * log_target_create(void)
Create a new log target skeleton.
Definition: logging.c:751
void log_set_print_level(struct log_target *target, int)
Enable or disable printing of the log level name.
Definition: logging.c:706
const char * hostname
Definition: logging.h:281
structure representing a logging target
Definition: logging.h:232
void osmo_vlogp(int subsys, int level, const char *file, int line, int cont, const char *format, va_list ap)
vararg version of logging function
Definition: logging.c:507
void log_set_log_level(struct log_target *target, int log_level)
Set the global log level for a given log target.
Definition: logging.c:715
void log_reset_context(void)
Reset (clear) the logging context.
Definition: logging.c:581
#define LOG_MAX_CTX
Maximum number of logging contexts.
Definition: logging.h:15
uint8_t loglevel
currently selected log-level
Definition: logging.h:138
unsigned int num_cat
total number of categories
Definition: logging.h:204
void logp2(int subsys, unsigned int level, const char *file, int line, int cont, const char *format,...) __attribute__((format(printf
logging function used by LOGP() macro
Definition: logging.c:555
int log_targets_reopen(void)
close and re-open all log files (for log file rotation)
Definition: logging.c:908
const char * description
description text
Definition: logging.h:137