libosmocore 1.7.0.61-0d9b6.202211030053
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>
13
14extern struct log_info *osmo_log_info;
15
16#ifndef DEBUG
17#define DEBUG
18#endif
19
20#ifdef LIBOSMOCORE_NO_LOGGING
21#undef DEBUG
22#endif
23
24#ifdef DEBUG
30#define DEBUGP(ss, fmt, args...) LOGP(ss, LOGL_DEBUG, fmt, ##args)
31#define DEBUGPC(ss, fmt, args...) LOGPC(ss, LOGL_DEBUG, fmt, ##args)
32#else
33#define DEBUGP(xss, fmt, args...)
34#define DEBUGPC(ss, fmt, args...)
35#endif
36
37
38void osmo_vlogp(int subsys, int level, const char *file, int line,
39 int cont, const char *format, va_list ap);
40
41void logp(int subsys, const char *file, int line, int cont, const char *format, ...) OSMO_DEPRECATED("Use DEBUGP* macros instead");
42
49#define LOGP(ss, level, fmt, args...) \
50 LOGPSRC(ss, level, NULL, 0, fmt, ## args)
51
58#ifndef LIBOSMOCORE_NO_LOGGING
59#define LOGPC(ss, level, fmt, args...) \
60 do { \
61 if (!osmo_log_info) { \
62 logp_stub(__FILE__, __LINE__, 1, fmt, ##args); \
63 break; \
64 } \
65 if (log_check_level(ss, level)) \
66 logp2(ss, level, __FILE__, __LINE__, 1, fmt, ##args); \
67 } while(0)
68#else
69#define LOGPC(ss, level, fmt, args...)
70#endif
71
84#define LOGPSRC(ss, level, caller_file, caller_line, fmt, args...) \
85 LOGPSRCC(ss, level, caller_file, caller_line, 0, fmt, ##args)
86
100#ifndef LIBOSMOCORE_NO_LOGGING
101#define LOGPSRCC(ss, level, caller_file, caller_line, cont, fmt, args...) \
102 do { \
103 if (!osmo_log_info) { \
104 if (caller_file) \
105 logp_stub(caller_file, caller_line, cont, fmt, ##args); \
106 else \
107 logp_stub(__FILE__, __LINE__, cont, fmt, ##args); \
108 break; \
109 } \
110 if (log_check_level(ss, level)) {\
111 if (caller_file) \
112 logp2(ss, level, caller_file, caller_line, cont, fmt, ##args); \
113 else \
114 logp2(ss, level, __FILE__, __LINE__, cont, fmt, ##args); \
115 }\
116 } while(0)
117#else
118#define LOGPSRCC(ss, level, caller_file, caller_line, cont, fmt, args...)
119#endif
120
122#define LOGL_DEBUG 1
123#define LOGL_INFO 3
124#define LOGL_NOTICE 5
125#define LOGL_ERROR 7
126#define LOGL_FATAL 8
128/* logging subsystems defined by the library itself */
129#define DLGLOBAL -1
130#define DLLAPD -2
131#define DLINP -3
132#define DLMUX -4
133#define DLMI -5
134#define DLMIB -6
135#define DLSMS -7
136#define DLCTRL -8
137#define DLGTP -9
138#define DLSTATS -10
139#define DLGSUP -11
140#define DLOAP -12
141#define DLSS7 -13
142#define DLSCCP -14
143#define DLSUA -15
144#define DLM3UA -16
145#define DLMGCP -17
146#define DLJIBUF -18
147#define DLRSPRO -19
148#define DLNS -20
149#define DLBSSGP -21
150#define DLNSDATA -22
151#define DLNSSIGNAL -23
152#define DLIUUP -24
153#define DLPFCP -25
154#define DLCSN1 -26
155#define DLM2PA -27
156#define DLM2UA -28
157#define OSMO_NUM_DLIB 28
159/* Colors that can be used in log_info_cat.color */
160#define OSMO_LOGCOLOR_NORMAL NULL
161#define OSMO_LOGCOLOR_RED "\033[1;31m"
162#define OSMO_LOGCOLOR_GREEN "\033[1;32m"
163#define OSMO_LOGCOLOR_YELLOW "\033[1;33m"
164#define OSMO_LOGCOLOR_BLUE "\033[1;34m"
165#define OSMO_LOGCOLOR_PURPLE "\033[1;35m"
166#define OSMO_LOGCOLOR_CYAN "\033[1;36m"
167#define OSMO_LOGCOLOR_DARKRED "\033[31m"
168#define OSMO_LOGCOLOR_DARKGREEN "\033[32m"
169#define OSMO_LOGCOLOR_DARKYELLOW "\033[33m"
170#define OSMO_LOGCOLOR_DARKBLUE "\033[34m"
171#define OSMO_LOGCOLOR_DARKPURPLE "\033[35m"
172#define OSMO_LOGCOLOR_DARKCYAN "\033[36m"
173#define OSMO_LOGCOLOR_DARKGREY "\033[1;30m"
174#define OSMO_LOGCOLOR_GREY "\033[37m"
175#define OSMO_LOGCOLOR_BRIGHTWHITE "\033[1;37m"
176#define OSMO_LOGCOLOR_END "\033[0;m"
177
180 uint8_t loglevel;
181 uint8_t enabled;
182};
183
186 const char *name;
187 const char *color;
188 const char *description;
189 uint8_t loglevel;
190 uint8_t enabled;
191};
192
204
218
220#define LOG_MAX_CTX _LOG_CTX_COUNT
222#define LOG_MAX_FILTERS _LOG_FLT_COUNT
223
226 void *ctx[LOG_MAX_CTX+1];
227};
228
230#define LOG_FILTER_ALL (1<<LOG_FLT_ALL)
232#define GPRS_CTX_NSVC LOG_CTX_GB_NSVC
234#define GPRS_CTX_BVC LOG_CTX_GB_BVC
238struct log_target;
239
241typedef int log_filter(const struct log_context *ctx,
242 struct log_target *target);
243
244struct log_info;
245struct vty;
246struct gsmtap_inst;
247
248typedef void log_print_filters(struct vty *vty,
249 const struct log_info *info,
250 const struct log_target *tgt);
251
252typedef void log_save_filters(struct vty *vty,
253 const struct log_info *info,
254 const struct log_target *tgt);
255
257struct log_info {
258 /* filter callback function */
260
262 const struct log_info_cat *cat;
264 unsigned int num_cat;
266 unsigned int num_cat_user;
267
272};
273
283};
284
290};
291
296};
297
306
309
311 uint8_t loglevel;
313 unsigned int use_color:1;
315 unsigned int print_timestamp:1;
317 unsigned int print_tid:1;
319 unsigned int print_filename:1;
321 unsigned int print_category:1;
323 unsigned int print_ext_timestamp:1;
324
327
328 union {
329 struct {
330 /* direct, blocking output via stdio */
331 FILE *out;
332 const char *fname;
333 /* indirect output via write_queue and osmo_select_main() */
336
337 struct {
341
342 struct {
343 void *vty;
345
346 struct {
347 void *rb;
349
350 struct {
352 const char *ident;
353 const char *hostname;
355
356 struct {
357 bool raw;
359 };
360
367 void (*output) (struct log_target *target, unsigned int level,
368 const char *string);
369
382 void (*raw_output)(struct log_target *target, int subsys,
383 unsigned int level, const char *file, int line,
384 int cont, const char *format, va_list ap);
385
386 /* Should the log level be printed? */
388 /* Should we print the subsys in hex like '<000b>'? */
390 /* Should we print the source file and line, and in which way? */
392 /* Where on a log line to put the source file info. */
394};
395
396/* use the above macros */
397void logp2(int subsys, unsigned int level, const char *file,
398 int line, int cont, const char *format, ...)
399 __attribute__ ((format (printf, 6, 7)));
400void logp_stub(const char *file, int line, int cont, const char *format, ...);
401int log_init(const struct log_info *inf, void *talloc_ctx);
403void log_fini(void);
404int log_check_level(int subsys, unsigned int level);
405
406/* context management */
407void log_reset_context(void);
408int log_set_context(uint8_t ctx, void *value);
409
410/* filter on the targets */
411void log_set_all_filter(struct log_target *target, int);
412
413void log_set_use_color(struct log_target *target, int);
414void log_set_print_extended_timestamp(struct log_target *target, int);
415void log_set_print_timestamp(struct log_target *target, int);
416void log_set_print_tid(struct log_target *target, int);
417void log_set_print_filename(struct log_target *target, int) OSMO_DEPRECATED("Use log_set_print_filename2() instead");
418void log_set_print_filename2(struct log_target *target, enum log_filename_type lft);
419void log_set_print_filename_pos(struct log_target *target, enum log_filename_pos pos);
420void log_set_print_category(struct log_target *target, int);
421void log_set_print_category_hex(struct log_target *target, int);
422void log_set_print_level(struct log_target *target, int);
423void log_set_log_level(struct log_target *target, int log_level);
424void log_parse_category_mask(struct log_target *target, const char* mask);
425const char* log_category_name(int subsys);
427const char *log_level_str(unsigned int lvl) OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE;
428int log_parse_category(const char *category);
429void log_set_category_filter(struct log_target *target, int category,
430 int enable, int level);
431
432/* management of the targets */
433struct log_target *log_target_create(void);
434void log_target_destroy(struct log_target *target);
436struct log_target *log_target_create_file(const char *fname);
437struct log_target *log_target_create_syslog(const char *ident, int option,
438 int facility);
439struct log_target *log_target_create_gsmtap(const char *host, uint16_t port,
440 const char *ident,
441 bool ofd_wq_mode,
442 bool add_sink);
444void log_target_systemd_set_raw(struct log_target *target, bool raw);
445int log_target_file_reopen(struct log_target *tgt);
448int log_targets_reopen(void);
449
450void log_add_target(struct log_target *target);
451void log_del_target(struct log_target *target);
452
453struct log_target *log_target_find(enum log_target_type type, const char *fname);
454
455void log_enable_multithread(void);
456
457void log_tgt_mutex_lock_impl(void);
459#define LOG_MTX_DEBUG 0
460#if LOG_MTX_DEBUG
461 #include <pthread.h>
462 #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)
463 #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)
464#else
465 #define log_tgt_mutex_lock() log_tgt_mutex_lock_impl()
466 #define log_tgt_mutex_unlock() log_tgt_mutex_unlock_impl()
467#endif
468
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
log_filter_index
Indexes to indicate objects that should be logged.
Definition: logging.h:208
struct log_target * log_target_create_stderr(void)
Create the STDERR log target.
Definition: logging.c:1080
log_ctx_index
Indexes to indicate the object currently acted upon.
Definition: logging.h:195
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:1180
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:1237
void log_tgt_mutex_unlock_impl(void)
Release the osmo_log_tgt_mutex.
Definition: logging.c:133
log_target_type
Type of logging target.
Definition: logging.h:275
void log_set_print_category(struct log_target *target, int)
Enable or disable printing of the category name.
Definition: logging.c:919
void log_tgt_mutex_lock_impl(void)
Acquire the osmo_log_tgt_mutex.
Definition: logging.c:117
void log_set_use_color(struct log_target *target, int)
Enable or disable the use of colored output.
Definition: logging.c:840
int log_target_file_reopen(struct log_target *tgt)
close and re-open a log file (for log file rotation)
Definition: logging.c:1363
void log_set_print_tid(struct log_target *target, int)
Enable or disable printing of timestamps while logging.
Definition: logging.c:871
int log_initialized(void)
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:928
int log_parse_level(const char *lvl) OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE
Parse a human-readable log level into a numeric value.
Definition: logging.c:337
void log_set_print_level(struct log_target *target, int)
Enable or disable printing of the log level name.
Definition: logging.c:939
void log_set_all_filter(struct log_target *target, int)
Enable the LOG_FLT_ALL log filter.
Definition: logging.c:828
void log_parse_category_mask(struct log_target *target, const char *mask)
parse the log category mask
Definition: logging.c:378
int log_set_context(uint8_t ctx, void *value)
Set the logging context.
Definition: logging.c:810
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:130
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:908
void log_print_filters(struct vty *vty, const struct log_info *info, const struct log_target *tgt)
Definition: logging.h:248
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:702
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:948
void log_save_filters(struct vty *vty, const struct log_info *info, const struct log_target *tgt)
Definition: logging.h:252
#define LOG_MAX_FILTERS
Maximum number of logging filters.
Definition: logging.h:222
void log_target_destroy(struct log_target *target)
Unregister, close and delete a log target.
Definition: logging.c:1313
log_filename_pos
Where on a log line source file and line should be logged.
Definition: logging.h:293
int log_check_level(int subsys, unsigned int level)
Check whether a log entry will be generated.
Definition: logging.c:1506
const char * log_level_str(unsigned int lvl) OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE
convert a numeric log level into human-readable string
Definition: logging.c:346
const char * log_category_name(int subsys)
Definition: logging.c:451
struct log_info * osmo_log_info
Definition: logging.c:87
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:897
struct log_target * log_target_create_systemd(bool raw)
Create a new logging target for systemd journal logging.
Definition: logging_systemd.c:86
int log_filter(const struct log_context *ctx, struct log_target *target)
Log filter function.
Definition: logging.h:241
struct log_target * log_target_create(void)
Create a new log target skeleton.
Definition: logging.c:1038
void log_set_print_extended_timestamp(struct log_target *target, int)
Enable or disable printing of extended timestamps while logging.
Definition: logging.c:862
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:959
log_filename_type
Whether/how to log the source filename (and line number).
Definition: logging.h:286
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:737
void log_fini(void)
Definition: logging.c:1487
#define DEBUGP(ss, fmt, args...)
Log a debug message through the Osmocom logging framework.
Definition: logging.h:30
#define LOG_MAX_CTX
Maximum number of logging contexts.
Definition: logging.h:220
int log_init(const struct log_info *inf, void *talloc_ctx)
Initialize the Osmocom logging core.
Definition: logging.c:1428
void void logp_stub(const char *file, int line, int cont, const char *format,...)
Definition: logging.c:767
int log_parse_category(const char *category)
parse a human-readable log category into numeric form
Definition: logging.c:355
struct log_target * log_target_find(enum log_target_type type, const char *fname)
Find a registered log target.
Definition: logging.c:1288
void log_enable_multithread(void)
Enable multithread support (mutex) in libosmocore logging system.
Definition: logging.c:107
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:1129
int log_targets_reopen(void)
close and re-open all log files (for log file rotation)
Definition: logging.c:1398
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:885
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:754
void log_set_print_timestamp(struct log_target *target, int)
Enable or disable printing of timestamps while logging.
Definition: logging.c:849
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:69
void log_add_target(struct log_target *target)
Register a new log target with the logging core.
Definition: logging.c:780
void log_del_target(struct log_target *target)
Unregister a log target from the logging core.
Definition: logging.c:788
void log_reset_context(void)
Reset (clear) the logging context.
Definition: logging.c:794
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:105
@ LOG_FLT_VLR_SUBSCR
Definition: logging.h:213
@ _LOG_FLT_COUNT
Definition: logging.h:216
@ LOG_FLT_ALL
Definition: logging.h:209
@ LOG_FLT_GB_NSE
Definition: logging.h:215
@ LOG_FLT_BSC_SUBSCR
Definition: logging.h:212
@ LOG_FLT_GB_NSVC
Definition: logging.h:210
@ LOG_FLT_L1_SAPI
Definition: logging.h:214
@ LOG_FLT_GB_BVC
Definition: logging.h:211
@ LOG_CTX_GB_NSE
Definition: logging.h:201
@ LOG_CTX_BSC_SUBSCR
Definition: logging.h:198
@ LOG_CTX_GB_BVC
Definition: logging.h:197
@ _LOG_CTX_COUNT
Definition: logging.h:202
@ LOG_CTX_L1_SAPI
Definition: logging.h:200
@ LOG_CTX_GB_NSVC
Definition: logging.h:196
@ LOG_CTX_VLR_SUBSCR
Definition: logging.h:199
@ LOG_TGT_TYPE_VTY
VTY logging.
Definition: logging.h:276
@ LOG_TGT_TYPE_FILE
text file logging
Definition: logging.h:278
@ LOG_TGT_TYPE_SYSLOG
syslog based logging
Definition: logging.h:277
@ LOG_TGT_TYPE_STRRB
osmo_strrb-backed logging
Definition: logging.h:280
@ LOG_TGT_TYPE_STDERR
stderr logging
Definition: logging.h:279
@ LOG_TGT_TYPE_GSMTAP
GSMTAP network logging.
Definition: logging.h:281
@ LOG_TGT_TYPE_SYSTEMD
systemd journal logging
Definition: logging.h:282
@ LOG_FILENAME_POS_LINE_END
Definition: logging.h:295
@ LOG_FILENAME_POS_HEADER_END
Definition: logging.h:294
@ LOG_FILENAME_BASENAME
Definition: logging.h:289
@ LOG_FILENAME_NONE
Definition: logging.h:287
@ LOG_FILENAME_PATH
Definition: logging.h:288
#define OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE
Definition: defs.h:48
#define OSMO_DEPRECATED(text)
Set the deprecated attribute with a message.
Definition: defs.h:41
struct gsm48_classmark3 __attribute__
Definition: log2.h:61
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:179
uint8_t enabled
is logging enabled?
Definition: logging.h:181
uint8_t loglevel
configured log-level
Definition: logging.h:180
Log context information, passed to filter.
Definition: logging.h:225
void * ctx[LOG_MAX_CTX+1]
Definition: logging.h:226
Information regarding one logging category.
Definition: logging.h:185
uint8_t enabled
is this category enabled or not
Definition: logging.h:190
const char * name
name of category
Definition: logging.h:186
const char * description
description text
Definition: logging.h:188
const char * color
color string for cateyory
Definition: logging.h:187
uint8_t loglevel
currently selected log-level
Definition: logging.h:189
Logging configuration, passed to log_init.
Definition: logging.h:257
unsigned int num_cat
total number of categories
Definition: logging.h:264
log_print_filters * print_fn
filter saving function
Definition: logging.h:271
log_save_filters * save_fn
filter saving function
Definition: logging.h:269
log_filter * filter_fn
Definition: logging.h:259
const struct log_info_cat * cat
per-category information
Definition: logging.h:262
unsigned int num_cat_user
total number of user categories (not library)
Definition: logging.h:266
structure representing a logging target
Definition: logging.h:299
bool print_category_hex
Definition: logging.h:389
int priority
Definition: logging.h:338
unsigned int print_timestamp
should log messages be prefixed with a timestamp?
Definition: logging.h:315
void * vty
Definition: logging.h:343
struct log_target::@8::@15 sd_journal
struct log_target::@8::@14 tgt_gsmtap
struct log_category * categories
logging categories
Definition: logging.h:308
struct log_target::@8::@10 tgt_file
unsigned int print_tid
should log messages be prefixed with the logger Thread ID?
Definition: logging.h:317
struct llist_head entry
linked list
Definition: logging.h:300
int filter_map
Internal data for filtering.
Definition: logging.h:303
void * filter_data[LOG_MAX_FILTERS+1]
Internal data for filtering.
Definition: logging.h:305
struct osmo_wqueue * wqueue
Definition: logging.h:334
enum log_filename_type print_filename2
Definition: logging.h:391
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:382
int facility
Definition: logging.h:339
enum log_filename_pos print_filename_pos
Definition: logging.h:393
unsigned int print_filename
DEPRECATED: use print_filename2 instead.
Definition: logging.h:319
struct gsmtap_inst * gsmtap_inst
Definition: logging.h:351
unsigned int use_color
should color be used when printing log messages?
Definition: logging.h:313
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:323
void * rb
Definition: logging.h:347
bool raw
Definition: logging.h:357
const char * fname
Definition: logging.h:332
unsigned int print_category
should log messages be prefixed with a category name?
Definition: logging.h:321
enum log_target_type type
the type of this log taget
Definition: logging.h:326
struct log_target::@8::@11 tgt_syslog
FILE * out
Definition: logging.h:331
bool print_level
Definition: logging.h:387
const char * hostname
Definition: logging.h:353
const char * ident
Definition: logging.h:352
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:367
uint8_t loglevel
global log level
Definition: logging.h:311
write queue instance
Definition: write_queue.h:30