logging.h
Go to the documentation of this file.
#pragma once
#include <stdio.h>
#include <stdint.h>
#include <stdarg.h>
#include <osmocom/core/linuxlist.h>
#define LOG_MAX_CTX 8
#define LOG_MAX_FILTERS 8
#define DEBUG
#ifdef DEBUG
#define DEBUGP(ss, fmt, args...) logp(ss, __FILE__, __LINE__, 0, fmt, ## args)
#define DEBUGPC(ss, fmt, args...) logp(ss, __FILE__, __LINE__, 1, fmt, ## args)
#else
#define DEBUGP(xss, fmt, args...)
#define DEBUGPC(ss, fmt, args...)
#endif
void osmo_vlogp(int subsys, int level, const char *file, int line,
int cont, const char *format, va_list ap);
void logp(int subsys, const char *file, int line, int cont, const char *format, ...) __attribute__ ((format (printf, 5, 6)));
#define LOGP(ss, level, fmt, args...) \
logp2(ss, level, __FILE__, __LINE__, 0, fmt, ##args)
#define LOGPC(ss, level, fmt, args...) \
logp2(ss, level, __FILE__, __LINE__, 1, fmt, ##args)
#define LOGL_DEBUG 1
#define LOGL_INFO 3
#define LOGL_NOTICE 5
#define LOGL_ERROR 7
#define LOGL_FATAL 8
#define LOG_FILTER_ALL 0x0001
/* logging levels defined by the library itself */
#define DLGLOBAL -1
#define DLLAPD -2
#define DLINP -3
#define DLMUX -4
#define DLMI -5
#define DLMIB -6
#define DLSMS -7
#define DLCTRL -8
#define DLGTP -9
#define DLSTATS -10
#define OSMO_NUM_DLIB 10
struct log_category {
uint8_t loglevel;
uint8_t enabled;
};
struct log_info_cat {
const char *name;
const char *color;
const char *description;
uint8_t loglevel;
uint8_t enabled;
};
struct log_context {
void *ctx[LOG_MAX_CTX+1];
};
struct log_target;
typedef int log_filter(const struct log_context *ctx,
struct log_target *target);
struct log_info;
struct vty;
typedef void log_print_filters(struct vty *vty,
const struct log_info *info,
const struct log_target *tgt);
typedef void log_save_filters(struct vty *vty,
const struct log_info *info,
const struct log_target *tgt);
struct log_info {
/* \brief filter callback function */
log_filter *filter_fn;
const struct log_info_cat *cat;
unsigned int num_cat;
unsigned int num_cat_user;
/* \brief filter saving function */
log_save_filters *save_fn;
/* \brief filter saving function */
log_print_filters *print_fn;
};
enum log_target_type {
LOG_TGT_TYPE_VTY,
LOG_TGT_TYPE_SYSLOG,
LOG_TGT_TYPE_FILE,
LOG_TGT_TYPE_STDERR,
LOG_TGT_TYPE_STRRB,
};
struct log_target {
struct llist_head entry;
int filter_map;
void *filter_data[LOG_MAX_FILTERS+1];
struct log_category *categories;
uint8_t loglevel;
unsigned int use_color:1;
unsigned int print_timestamp:1;
unsigned int print_filename:1;
unsigned int print_category:1;
unsigned int print_ext_timestamp:1;
enum log_target_type type;
union {
struct {
FILE *out;
const char *fname;
} tgt_file;
struct {
int priority;
int facility;
} tgt_syslog;
struct {
void *vty;
} tgt_vty;
struct {
void *rb;
} tgt_rb;
};
void (*output) (struct log_target *target, unsigned int level,
const char *string);
};
/* use the above macros */
void logp2(int subsys, unsigned int level, const char *file,
int line, int cont, const char *format, ...)
__attribute__ ((format (printf, 6, 7)));
int log_init(const struct log_info *inf, void *talloc_ctx);
/* context management */
void log_reset_context(void);
int log_set_context(uint8_t ctx, void *value);
/* filter on the targets */
void log_set_all_filter(struct log_target *target, int);
void log_set_use_color(struct log_target *target, int);
void log_set_print_extended_timestamp(struct log_target *target, int);
void log_set_print_timestamp(struct log_target *target, int);
void log_set_print_filename(struct log_target *target, int);
void log_set_print_category(struct log_target *target, int);
void log_set_log_level(struct log_target *target, int log_level);
void log_parse_category_mask(struct log_target *target, const char* mask);
int log_parse_level(const char *lvl);
const char *log_level_str(unsigned int lvl);
int log_parse_category(const char *category);
void log_set_category_filter(struct log_target *target, int category,
int enable, int level);
/* management of the targets */
struct log_target *log_target_create(void);
void log_target_destroy(struct log_target *target);
struct log_target *log_target_create_stderr(void);
struct log_target *log_target_create_file(const char *fname);
struct log_target *log_target_create_syslog(const char *ident, int option,
int facility);
int log_target_file_reopen(struct log_target *tgt);
int log_targets_reopen(void);
void log_add_target(struct log_target *target);
void log_del_target(struct log_target *target);
/* Generate command string for VTY use */
const char *log_vty_command_string(const struct log_info *info);
const char *log_vty_command_description(const struct log_info *info);
struct log_target *log_target_find(int type, const char *fname);
extern struct llist_head osmo_log_target_list;
log_info_cat
Information regarding one logging category.
Definition: logging.h:81
log_target::filter_map
int filter_map
Internal data for filtering.
Definition: logging.h:143
log_set_all_filter
void log_set_all_filter(struct log_target *target, int)
Enable the LOG_FILTER_ALL log filter.
Definition: logging.c:435
log_parse_level
int log_parse_level(const char *lvl)
Parse a human-readable log level into a numeric value.
Definition: logging.c:146
log_parse_category
int log_parse_category(const char *category)
parse a human-readable log category into numeric form
Definition: logging.c:161
log_info_cat::name
const char * name
Definition: logging.h:82
LOG_TGT_TYPE_VTY
VTY logging.
Definition: logging.h:131
log_set_print_timestamp
void log_set_print_timestamp(struct log_target *target, int)
Enable or disable printing of timestamps while logging.
Definition: logging.c:456
log_target::type
enum log_target_type type
the type of this log taget
Definition: logging.h:164
log_category
Definition: logging.h:75
log_info_cat::enabled
uint8_t enabled
Definition: logging.h:86
LOG_TGT_TYPE_SYSLOG
syslog based logging
Definition: logging.h:132
log_level_str
const char * log_level_str(unsigned int lvl)
convert a numeric log level into human-readable string
Definition: logging.c:152
log_target_type
log_target_type
Type of logging target.
Definition: logging.h:130
log_target_destroy
void log_target_destroy(struct log_target *target)
Unregister, close and delete a log target.
Definition: logging.c:628
log_set_use_color
void log_set_use_color(struct log_target *target, int)
Enable or disable the use of colored output.
Definition: logging.c:447
LOG_MAX_FILTERS
#define LOG_MAX_FILTERS
Maximum number of logging filters.
Definition: logging.h:17
log_target::print_category
unsigned int print_category
should log messages be prefixed with a category name?
Definition: logging.h:159
log_add_target
void log_add_target(struct log_target *target)
Register a new log target with the logging core.
Definition: logging.c:388
log_filter
int log_filter(const struct log_context *ctx, struct log_target *target)
Log filter function.
Definition: logging.h:97
LOG_TGT_TYPE_STRRB
osmo_strrb-backed logging
Definition: logging.h:135
LOG_TGT_TYPE_FILE
text file logging
Definition: logging.h:133
log_del_target
void log_del_target(struct log_target *target)
Unregister a log target from the logging core.
Definition: logging.c:396
log_target_create_file
struct log_target * log_target_create_file(const char *fname)
Create a new file-based log target.
Definition: logging.c:586
LOG_TGT_TYPE_STDERR
stderr logging
Definition: logging.h:134
log_target::print_filename
unsigned int print_filename
should log messages be prefixed with a filename?
Definition: logging.h:157
llist_head
Definition: linuxlist.h:42
log_info_cat::color
const char * color
Definition: logging.h:83
log_set_print_filename
void log_set_print_filename(struct log_target *target, int)
Enable or disable printing of the filename while logging.
Definition: logging.c:478
log_vty_command_string
const char * log_vty_command_string(const struct log_info *info)
Generates the logging command string for VTY.
Definition: logging.c:686
log_set_context
int log_set_context(uint8_t ctx, void *value)
Set the logging context.
Definition: logging.c:417
log_set_print_category
void log_set_print_category(struct log_target *target, int)
Enable or disable printing of the category name.
Definition: logging.c:489
log_target::categories
struct log_category * categories
logging categories
Definition: logging.h:148
log_target::loglevel
uint8_t loglevel
global log level
Definition: logging.h:151
log_target_create_stderr
struct log_target * log_target_create_stderr(void)
Create the STDERR log target.
Definition: logging.c:563
log_parse_category_mask
void log_parse_category_mask(struct log_target *target, const char *mask)
parse the log category mask
Definition: logging.c:182
log_set_category_filter
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:509
log_init
void int log_init(const struct log_info *inf, void *talloc_ctx)
Initialize the Osmocom logging core.
Definition: logging.c:825
log_vty_command_description
const char * log_vty_command_description(const struct log_info *info)
Generates the logging command description for VTY.
Definition: logging.c:763
log_info
Logging configuration, passed to log_init.
Definition: logging.h:112
log_target::print_timestamp
unsigned int print_timestamp
should log messages be prefixed with a timestamp?
Definition: logging.h:155
log_info::num_cat_user
unsigned int num_cat_user
total number of user categories (not library)
Definition: logging.h:121
log_context
Log context information, passed to filter.
Definition: logging.h:90
log_target_create
struct log_target * log_target_create(void)
Create a new log target skeleton.
Definition: logging.c:526
log_info::cat
const struct log_info_cat * cat
per-category information
Definition: logging.h:117
log_target_create_syslog
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:72
log_target::use_color
unsigned int use_color
should color be used when printing log messages?
Definition: logging.h:153
log_target_find
struct log_target * log_target_find(int type, const char *fname)
Find a registered log target.
Definition: logging.c:611
log_target::print_ext_timestamp
unsigned int print_ext_timestamp
should log messages be prefixed with an extended timestamp?
Definition: logging.h:161
log_target_file_reopen
int log_target_file_reopen(struct log_target *tgt)
close and re-open a log file (for log file rotation)
Definition: logging.c:650
log_target
structure representing a logging target
Definition: logging.h:139
osmo_vlogp
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:314
log_set_log_level
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:498
log_reset_context
void log_reset_context(void)
Reset (clear) the logging context.
Definition: logging.c:402
LOG_MAX_CTX
#define LOG_MAX_CTX
Maximum number of logging contexts.
Definition: logging.h:15
log_info_cat::loglevel
uint8_t loglevel
Definition: logging.h:85
log_info::num_cat
unsigned int num_cat
total number of categories
Definition: logging.h:119
log_targets_reopen
int log_targets_reopen(void)
close and re-open a log file (for log file rotation)
Definition: logging.c:664
log_set_print_extended_timestamp
void log_set_print_extended_timestamp(struct log_target *target, int)
Enable or disable printing of extended timestamps while logging.
Definition: logging.c:469
log_info_cat::description
const char * description
Definition: logging.h:84