libosmocore  1.6.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 
14 #ifndef DEBUG
15 #define DEBUG
16 #endif
17 
18 #ifdef DEBUG
24 #define DEBUGP(ss, fmt, args...) LOGP(ss, LOGL_DEBUG, fmt, ##args)
25 #define DEBUGPC(ss, fmt, args...) LOGPC(ss, LOGL_DEBUG, fmt, ##args)
26 #else
27 #define DEBUGP(xss, fmt, args...)
28 #define DEBUGPC(ss, fmt, args...)
29 #endif
30 
31 
32 void osmo_vlogp(int subsys, int level, const char *file, int line,
33  int cont, const char *format, va_list ap);
34 
35 void logp(int subsys, const char *file, int line, int cont, const char *format, ...) OSMO_DEPRECATED("Use DEBUGP* macros instead");
36 
43 #define LOGP(ss, level, fmt, args...) \
44  LOGPSRC(ss, level, NULL, 0, fmt, ## args)
45 
52 #define LOGPC(ss, level, fmt, args...) \
53  do { \
54  if (log_check_level(ss, level)) \
55  logp2(ss, level, __FILE__, __LINE__, 1, fmt, ##args); \
56  } while(0)
57 
70 #define LOGPSRC(ss, level, caller_file, caller_line, fmt, args...) \
71  LOGPSRCC(ss, level, caller_file, caller_line, 0, fmt, ##args)
72 
86 #define LOGPSRCC(ss, level, caller_file, caller_line, cont, fmt, args...) \
87  do { \
88  if (log_check_level(ss, level)) {\
89  if (caller_file) \
90  logp2(ss, level, caller_file, caller_line, cont, fmt, ##args); \
91  else \
92  logp2(ss, level, __FILE__, __LINE__, cont, fmt, ##args); \
93  }\
94  } while(0)
95 
97 #define LOGL_DEBUG 1
98 #define LOGL_INFO 3
99 #define LOGL_NOTICE 5
100 #define LOGL_ERROR 7
101 #define LOGL_FATAL 8
103 /* logging subsystems defined by the library itself */
104 #define DLGLOBAL -1
105 #define DLLAPD -2
106 #define DLINP -3
107 #define DLMUX -4
108 #define DLMI -5
109 #define DLMIB -6
110 #define DLSMS -7
111 #define DLCTRL -8
112 #define DLGTP -9
113 #define DLSTATS -10
114 #define DLGSUP -11
115 #define DLOAP -12
116 #define DLSS7 -13
117 #define DLSCCP -14
118 #define DLSUA -15
119 #define DLM3UA -16
120 #define DLMGCP -17
121 #define DLJIBUF -18
122 #define DLRSPRO -19
123 #define DLNS -20
124 #define DLBSSGP -21
125 #define DLNSDATA -22
126 #define DLNSSIGNAL -23
127 #define OSMO_NUM_DLIB 23
129 /* Colors that can be used in log_info_cat.color */
130 #define OSMO_LOGCOLOR_NORMAL NULL
131 #define OSMO_LOGCOLOR_RED "\033[1;31m"
132 #define OSMO_LOGCOLOR_GREEN "\033[1;32m"
133 #define OSMO_LOGCOLOR_YELLOW "\033[1;33m"
134 #define OSMO_LOGCOLOR_BLUE "\033[1;34m"
135 #define OSMO_LOGCOLOR_PURPLE "\033[1;35m"
136 #define OSMO_LOGCOLOR_CYAN "\033[1;36m"
137 #define OSMO_LOGCOLOR_DARKRED "\033[31m"
138 #define OSMO_LOGCOLOR_DARKGREEN "\033[32m"
139 #define OSMO_LOGCOLOR_DARKYELLOW "\033[33m"
140 #define OSMO_LOGCOLOR_DARKBLUE "\033[34m"
141 #define OSMO_LOGCOLOR_DARKPURPLE "\033[35m"
142 #define OSMO_LOGCOLOR_DARKCYAN "\033[36m"
143 #define OSMO_LOGCOLOR_DARKGREY "\033[1;30m"
144 #define OSMO_LOGCOLOR_GREY "\033[37m"
145 #define OSMO_LOGCOLOR_BRIGHTWHITE "\033[1;37m"
146 #define OSMO_LOGCOLOR_END "\033[0;m"
147 
149 struct log_category {
150  uint8_t loglevel;
151  uint8_t enabled;
152 };
153 
155 struct log_info_cat {
156  const char *name;
157  const char *color;
158  const char *description;
159  uint8_t loglevel;
160  uint8_t enabled;
161 };
162 
173 };
174 
187 };
188 
190 #define LOG_MAX_CTX _LOG_CTX_COUNT
192 #define LOG_MAX_FILTERS _LOG_FLT_COUNT
193 
195 struct log_context {
196  void *ctx[LOG_MAX_CTX+1];
197 };
198 
200 #define LOG_FILTER_ALL (1<<LOG_FLT_ALL)
202 #define GPRS_CTX_NSVC LOG_CTX_GB_NSVC
204 #define GPRS_CTX_BVC LOG_CTX_GB_BVC
208 struct log_target;
209 
211 typedef int log_filter(const struct log_context *ctx,
212  struct log_target *target);
213 
214 struct log_info;
215 struct vty;
216 struct gsmtap_inst;
217 
218 typedef void log_print_filters(struct vty *vty,
219  const struct log_info *info,
220  const struct log_target *tgt);
221 
222 typedef void log_save_filters(struct vty *vty,
223  const struct log_info *info,
224  const struct log_target *tgt);
225 
227 struct log_info {
228  /* filter callback function */
230 
232  const struct log_info_cat *cat;
234  unsigned int num_cat;
236  unsigned int num_cat_user;
237 
242 };
243 
253 };
254 
260 };
261 
266 };
267 
269 struct log_target {
270  struct llist_head entry;
276 
279 
281  uint8_t loglevel;
283  unsigned int use_color:1;
285  unsigned int print_timestamp:1;
287  unsigned int print_tid:1;
289  unsigned int print_filename:1;
291  unsigned int print_category:1;
293  unsigned int print_ext_timestamp:1;
294 
296  enum log_target_type type;
297 
298  union {
299  struct {
300  /* direct, blocking output via stdio */
301  FILE *out;
302  const char *fname;
303  /* indirect output via write_queue and osmo_select_main() */
306 
307  struct {
308  int priority;
309  int facility;
311 
312  struct {
313  void *vty;
315 
316  struct {
317  void *rb;
319 
320  struct {
322  const char *ident;
323  const char *hostname;
325 
326  struct {
327  bool raw;
329  };
330 
337  void (*output) (struct log_target *target, unsigned int level,
338  const char *string);
339 
352  void (*raw_output)(struct log_target *target, int subsys,
353  unsigned int level, const char *file, int line,
354  int cont, const char *format, va_list ap);
355 
356  /* Should the log level be printed? */
358  /* Should we print the subsys in hex like '<000b>'? */
360  /* Should we print the source file and line, and in which way? */
362  /* Where on a log line to put the source file info. */
364 };
365 
366 /* use the above macros */
367 void logp2(int subsys, unsigned int level, const char *file,
368  int line, int cont, const char *format, ...)
369  __attribute__ ((format (printf, 6, 7)));
370 int log_init(const struct log_info *inf, void *talloc_ctx);
371 void log_fini(void);
372 int log_check_level(int subsys, unsigned int level);
373 
374 /* context management */
375 void log_reset_context(void);
376 int log_set_context(uint8_t ctx, void *value);
377 
378 /* filter on the targets */
379 void log_set_all_filter(struct log_target *target, int);
380 
381 void log_set_use_color(struct log_target *target, int);
382 void log_set_print_extended_timestamp(struct log_target *target, int);
383 void log_set_print_timestamp(struct log_target *target, int);
384 void log_set_print_tid(struct log_target *target, int);
385 void log_set_print_filename(struct log_target *target, int) OSMO_DEPRECATED("Use log_set_print_filename2() instead");
386 void log_set_print_filename2(struct log_target *target, enum log_filename_type lft);
387 void log_set_print_filename_pos(struct log_target *target, enum log_filename_pos pos);
388 void log_set_print_category(struct log_target *target, int);
389 void log_set_print_category_hex(struct log_target *target, int);
390 void log_set_print_level(struct log_target *target, int);
391 void log_set_log_level(struct log_target *target, int log_level);
392 void log_parse_category_mask(struct log_target *target, const char* mask);
393 const char* log_category_name(int subsys);
395 const char *log_level_str(unsigned int lvl) OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE;
396 int log_parse_category(const char *category);
397 void log_set_category_filter(struct log_target *target, int category,
398  int enable, int level);
399 
400 /* management of the targets */
401 struct log_target *log_target_create(void);
402 void log_target_destroy(struct log_target *target);
404 struct log_target *log_target_create_file(const char *fname);
405 struct log_target *log_target_create_syslog(const char *ident, int option,
406  int facility);
407 struct log_target *log_target_create_gsmtap(const char *host, uint16_t port,
408  const char *ident,
409  bool ofd_wq_mode,
410  bool add_sink);
412 void log_target_systemd_set_raw(struct log_target *target, bool raw);
413 int log_target_file_reopen(struct log_target *tgt);
416 int log_targets_reopen(void);
417 
418 void log_add_target(struct log_target *target);
419 void log_del_target(struct log_target *target);
420 
421 struct log_target *log_target_find(int type, const char *fname);
422 
423 void log_enable_multithread(void);
424 
425 void log_tgt_mutex_lock_impl(void);
426 void log_tgt_mutex_unlock_impl(void);
427 #define LOG_MTX_DEBUG 0
428 #if LOG_MTX_DEBUG
429  #include <pthread.h>
430  #define log_tgt_mutex_lock() do { fprintf(stderr, "[%lu] %s:%d [%s] lock\n", pthread_self(), __FILE__, __LINE__, __func__); log_tgt_mutex_lock_impl(); } while (0)
431  #define log_tgt_mutex_unlock() do { fprintf(stderr, "[%lu] %s:%d [%s] unlock\n", pthread_self(), __FILE__, __LINE__, __func__); log_tgt_mutex_unlock_impl(); } while (0)
432 #else
433  #define log_tgt_mutex_lock() log_tgt_mutex_lock_impl()
434  #define log_tgt_mutex_unlock() log_tgt_mutex_unlock_impl()
435 #endif
436 
General definitions that are meant to be included from header files.
enum osmo_sub_auth_type type
write Write running configuration to or terminal n Write configuration to the file(same as write file)\n") ALIAS(config_write_file
struct gsm48_classmark3 __attribute__
Definition: log2.h:61
log_filter_index
Indexes to indicate objects that should be logged.
Definition: logging.h:178
struct log_target * log_target_create_stderr(void)
Create the STDERR log target.
Definition: logging.c:1047
log_ctx_index
Indexes to indicate the object currently acted upon.
Definition: logging.h:165
int log_target_file_switch_to_wqueue(struct log_target *tgt)
switch from blocking + buffered file output to non-blocking write-queue based output.
Definition: logging.c:1147
struct log_target * log_target_create_file(const char *fname)
Create a new file-based log target using non-blocking write_queue.
Definition: logging.c:1204
void log_tgt_mutex_unlock_impl(void)
Release the osmo_log_tgt_mutex.
Definition: logging.c:136
log_target_type
Type of logging target.
Definition: logging.h:245
void log_set_print_category(struct log_target *target, int)
Enable or disable printing of the category name.
Definition: logging.c:889
void log_tgt_mutex_lock_impl(void)
Acquire the osmo_log_tgt_mutex.
Definition: logging.c:120
void log_set_use_color(struct log_target *target, int)
Enable or disable the use of colored output.
Definition: logging.c:810
int log_target_file_reopen(struct log_target *tgt)
close and re-open a log file (for log file rotation)
Definition: logging.c:1327
void log_set_print_tid(struct log_target *target, int)
Enable or disable printing of timestamps while logging.
Definition: logging.c:841
void log_set_print_category_hex(struct log_target *target, int)
Enable or disable printing of the category number in hex ('<000b>').
Definition: logging.c:898
int log_parse_level(const char *lvl) OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE
Parse a human-readable log level into a numeric value.
Definition: logging.c:322
struct log_target * log_target_find(int type, const char *fname)
Find a registered log target.
Definition: logging.c:1255
void log_set_print_level(struct log_target *target, int)
Enable or disable printing of the log level name.
Definition: logging.c:909
void log_set_all_filter(struct log_target *target, int)
Enable the LOG_FLT_ALL log filter.
Definition: logging.c:798
void log_parse_category_mask(struct log_target *target, const char *mask)
parse the log category mask
Definition: logging.c:363
int log_set_context(uint8_t ctx, void *value)
Set the logging context.
Definition: logging.c:780
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:134
void log_set_print_filename_pos(struct log_target *target, enum log_filename_pos pos)
Set the position where on a log line the source file info should be logged.
Definition: logging.c:878
void log_print_filters(struct vty *vty, const struct log_info *info, const struct log_target *tgt)
Definition: logging.h:218
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:684
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:918
void log_save_filters(struct vty *vty, const struct log_info *info, const struct log_target *tgt)
Definition: logging.h:222
#define LOG_MAX_FILTERS
Maximum number of logging filters.
Definition: logging.h:192
void log_target_destroy(struct log_target *target)
Unregister, close and delete a log target.
Definition: logging.c:1280
log_filename_pos
Where on a log line source file and line should be logged.
Definition: logging.h:263
int log_check_level(int subsys, unsigned int level)
Check whether a log entry will be generated.
Definition: logging.c:1470
const char * log_level_str(unsigned int lvl) OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE
convert a numeric log level into human-readable string
Definition: logging.c:331
const char * log_category_name(int subsys)
Definition: logging.c:436
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:867
struct log_target * log_target_create_systemd(bool raw)
Create a new logging target for systemd journal logging.
Definition: logging_systemd.c:90
int log_filter(const struct log_context *ctx, struct log_target *target)
Log filter function.
Definition: logging.h:211
struct log_target * log_target_create(void)
Create a new log target skeleton.
Definition: logging.c:1005
void log_set_print_extended_timestamp(struct log_target *target, int)
Enable or disable printing of extended timestamps while logging.
Definition: logging.c:832
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:929
log_filename_type
Whether/how to log the source filename (and line number).
Definition: logging.h:256
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:719
void log_fini(void)
Definition: logging.c:1451
#define DEBUGP(ss, fmt, args...)
Log a debug message through the Osmocom logging framework.
Definition: logging.h:24
#define LOG_MAX_CTX
Maximum number of logging contexts.
Definition: logging.h:190
void int log_init(const struct log_info *inf, void *talloc_ctx)
Initialize the Osmocom logging core.
Definition: logging.c:1392
int log_parse_category(const char *category)
parse a human-readable log category into numeric form
Definition: logging.c:340
void log_enable_multithread(void)
Enable multithread support (mutex) in libosmocore logging system.
Definition: logging.c:110
int log_target_file_switch_to_stream(struct log_target *tgt)
switch from non-blocking/write-queue to blocking + buffered stream output
Definition: logging.c:1096
int log_targets_reopen(void)
close and re-open all log files (for log file rotation)
Definition: logging.c:1362
void log_set_print_filename(struct log_target *target, int) OSMO_DEPRECATED("Use log_set_print_filename2() instead")
Use log_set_print_filename2() instead.
Definition: logging.c:855
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:736
void log_set_print_timestamp(struct log_target *target, int)
Enable or disable printing of timestamps while logging.
Definition: logging.c:819
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
void log_add_target(struct log_target *target)
Register a new log target with the logging core.
Definition: logging.c:750
void log_del_target(struct log_target *target)
Unregister a log target from the logging core.
Definition: logging.c:758
void log_reset_context(void)
Reset (clear) the logging context.
Definition: logging.c:764
void log_target_systemd_set_raw(struct log_target *target, bool raw)
Change meta information handling of an existing logging target.
Definition: logging_systemd.c:109
@ LOG_FLT_VLR_SUBSCR
Definition: logging.h:183
@ _LOG_FLT_COUNT
Definition: logging.h:186
@ LOG_FLT_ALL
Definition: logging.h:179
@ LOG_FLT_GB_NSE
Definition: logging.h:185
@ LOG_FLT_BSC_SUBSCR
Definition: logging.h:182
@ LOG_FLT_GB_NSVC
Definition: logging.h:180
@ LOG_FLT_L1_SAPI
Definition: logging.h:184
@ LOG_FLT_GB_BVC
Definition: logging.h:181
@ LOG_CTX_GB_NSE
Definition: logging.h:171
@ LOG_CTX_BSC_SUBSCR
Definition: logging.h:168
@ LOG_CTX_GB_BVC
Definition: logging.h:167
@ _LOG_CTX_COUNT
Definition: logging.h:172
@ LOG_CTX_L1_SAPI
Definition: logging.h:170
@ LOG_CTX_GB_NSVC
Definition: logging.h:166
@ LOG_CTX_VLR_SUBSCR
Definition: logging.h:169
@ LOG_TGT_TYPE_VTY
VTY logging.
Definition: logging.h:246
@ LOG_TGT_TYPE_FILE
text file logging
Definition: logging.h:248
@ LOG_TGT_TYPE_SYSLOG
syslog based logging
Definition: logging.h:247
@ LOG_TGT_TYPE_STRRB
osmo_strrb-backed logging
Definition: logging.h:250
@ LOG_TGT_TYPE_STDERR
stderr logging
Definition: logging.h:249
@ LOG_TGT_TYPE_GSMTAP
GSMTAP network logging.
Definition: logging.h:251
@ LOG_TGT_TYPE_SYSTEMD
systemd journal logging
Definition: logging.h:252
@ LOG_FILENAME_POS_LINE_END
Definition: logging.h:265
@ LOG_FILENAME_POS_HEADER_END
Definition: logging.h:264
@ LOG_FILENAME_BASENAME
Definition: logging.h:259
@ LOG_FILENAME_NONE
Definition: logging.h:257
@ LOG_FILENAME_PATH
Definition: logging.h:258
#define OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE
Definition: defs.h:48
#define OSMO_DEPRECATED(text)
Set the deprecated attribute with a message.
Definition: defs.h:41
char subsys[16]
logging sub-system
Definition: gsmtap.h:9
uint8_t level
logging level
Definition: gsmtap.h:6
Simple doubly linked list implementation.
one gsmtap instance
Definition: gsmtap_util.h:27
(double) linked list header structure
Definition: linuxlist.h:46
Configuration of single log category / sub-system.
Definition: logging.h:149
uint8_t enabled
is logging enabled?
Definition: logging.h:151
uint8_t loglevel
configured log-level
Definition: logging.h:150
Log context information, passed to filter.
Definition: logging.h:195
void * ctx[LOG_MAX_CTX+1]
Definition: logging.h:196
Information regarding one logging category.
Definition: logging.h:155
uint8_t enabled
is this category enabled or not
Definition: logging.h:160
const char * name
name of category
Definition: logging.h:156
const char * description
description text
Definition: logging.h:158
const char * color
color string for cateyory
Definition: logging.h:157
uint8_t loglevel
currently selected log-level
Definition: logging.h:159
Logging configuration, passed to log_init.
Definition: logging.h:227
unsigned int num_cat
total number of categories
Definition: logging.h:234
log_print_filters * print_fn
filter saving function
Definition: logging.h:241
log_save_filters * save_fn
filter saving function
Definition: logging.h:239
log_filter * filter_fn
Definition: logging.h:229
const struct log_info_cat * cat
per-category information
Definition: logging.h:232
unsigned int num_cat_user
total number of user categories (not library)
Definition: logging.h:236
structure representing a logging target
Definition: logging.h:269
bool print_category_hex
Definition: logging.h:359
int priority
Definition: logging.h:308
unsigned int print_timestamp
should log messages be prefixed with a timestamp?
Definition: logging.h:285
void * vty
Definition: logging.h:313
struct log_target::@8::@15 sd_journal
struct log_target::@8::@14 tgt_gsmtap
struct log_category * categories
logging categories
Definition: logging.h:278
struct log_target::@8::@10 tgt_file
unsigned int print_tid
should log messages be prefixed with the logger Thread ID?
Definition: logging.h:287
struct llist_head entry
linked list
Definition: logging.h:270
int filter_map
Internal data for filtering.
Definition: logging.h:273
void * filter_data[LOG_MAX_FILTERS+1]
Internal data for filtering.
Definition: logging.h:275
struct osmo_wqueue * wqueue
Definition: logging.h:304
enum log_filename_type print_filename2
Definition: logging.h:361
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:352
int facility
Definition: logging.h:309
enum log_filename_pos print_filename_pos
Definition: logging.h:363
unsigned int print_filename
DEPRECATED: use print_filename2 instead.
Definition: logging.h:289
struct gsmtap_inst * gsmtap_inst
Definition: logging.h:321
unsigned int use_color
should color be used when printing log messages?
Definition: logging.h:283
struct log_target::@8::@12 tgt_vty
struct log_target::@8::@13 tgt_rb
unsigned int print_ext_timestamp
should log messages be prefixed with an extended timestamp?
Definition: logging.h:293
void * rb
Definition: logging.h:317
bool raw
Definition: logging.h:327
const char * fname
Definition: logging.h:302
unsigned int print_category
should log messages be prefixed with a category name?
Definition: logging.h:291
enum log_target_type type
the type of this log taget
Definition: logging.h:296
struct log_target::@8::@11 tgt_syslog
FILE * out
Definition: logging.h:301
bool print_level
Definition: logging.h:357
const char * hostname
Definition: logging.h:323
const char * ident
Definition: logging.h:322
void(* output)(struct log_target *target, unsigned int level, const char *string)
call-back function to be called when the logging framework wants to log a fully formatted string
Definition: logging.h:337
uint8_t loglevel
global log level
Definition: logging.h:281
write queue instance
Definition: write_queue.h:34