logging: Make it possible to print category/subsys and timestamps

We want to see from which category/subsystem a certain log message
is coming from and use a different timestamp format as well. Add
two new bitfields. This doesn't change the size of the structure
and on 32bit we still have 27bits left.

The extended timestamp will take preference over the current and
default timestamp format.

Fixes: SYS#602
This commit is contained in:
Holger Hans Peter Freyther 2014-12-05 09:35:30 +01:00
parent a5dc19dc40
commit 2d6ad13d8d
3 changed files with 101 additions and 4 deletions

View File

@ -154,6 +154,10 @@ struct log_target {
unsigned int print_timestamp:1;
/*! \brief should log messages be prefixed with a filename? */
unsigned int print_filename:1;
/*! \brief should log messages be prefixed with a category name? */
unsigned int print_category:1;
/*! \brief should log messages be prefixed with an extended timestamp? */
unsigned int print_ext_timestamp:1;
/*! \brief the type of this log taget */
enum log_target_type type;
@ -202,8 +206,10 @@ int log_set_context(uint8_t ctx, void *value);
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);

View File

@ -226,6 +226,14 @@ static const char* color(int subsys)
return NULL;
}
static const char* category_name(int subsys)
{
if (subsys < osmo_log_info->num_cat)
return osmo_log_info->cat[subsys].name;
return NULL;
}
static void _output(struct log_target *target, unsigned int subsys,
unsigned int level, const char *file, int line, int cont,
const char *format, va_list ap)
@ -244,7 +252,17 @@ static void _output(struct log_target *target, unsigned int subsys,
}
}
if (!cont) {
if (target->print_timestamp) {
if (target->print_ext_timestamp) {
struct tm tm;
time_t timep = time(NULL);
localtime_r(&timep, &tm);
ret = snprintf(buf + offset, rem, "%04d%02d%02d%02d%02d%02d000 ",
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec);
if (ret < 0)
goto err;
OSMO_SNPRINTF_RET(ret, rem, offset, len);
} else if (target->print_timestamp) {
char *timestr;
time_t tm;
tm = time(NULL);
@ -255,6 +273,12 @@ static void _output(struct log_target *target, unsigned int subsys,
goto err;
OSMO_SNPRINTF_RET(ret, rem, offset, len);
}
if (target->print_category) {
ret = snprintf(buf + offset, rem, "%s ", category_name(subsys));
if (ret < 0)
goto err;
OSMO_SNPRINTF_RET(ret, rem, offset, len);
}
if (target->print_filename) {
ret = snprintf(buf + offset, rem, "<%4.4x> %s:%d ",
subsys, file, line);
@ -426,6 +450,19 @@ void log_set_print_timestamp(struct log_target *target, int print_timestamp)
target->print_timestamp = print_timestamp;
}
/*! \brief Enable or disable printing of extended timestamps while logging
* \param[in] target Log target to be affected
* \param[in] print_timestamp Enable (1) or disable (0) timestamps
*
* When both timestamp and extended timestamp is enabled then only
* the extended timestamp will be used. The format of the timestamp
* is YYYYMMDDhhmmssnnn.
*/
void log_set_print_extended_timestamp(struct log_target *target, int print_timestamp)
{
target->print_ext_timestamp = print_timestamp;
}
/*! \brief Enable or disable printing of the filename while logging
* \param[in] target Log target to be affected
* \param[in] print_filename Enable (1) or disable (0) filenames
@ -435,6 +472,17 @@ void log_set_print_filename(struct log_target *target, int print_filename)
target->print_filename = print_filename;
}
/*! \brief Enable or disable printing of the category name
* \param[in] target Log target to be affected
* \param[in] print_catname Enable (1) or disable (0) filenames
*
* Print the category/subsys name in front of every log message.
*/
void log_set_print_category(struct log_target *target, int print_category)
{
target->print_category = print_category;
}
/*! \brief Set the global log level for a given log target
* \param[in] target Log target to be affected
* \param[in] log_level New global log level

View File

@ -1,6 +1,6 @@
/* OpenBSC logging helper for the VTY */
/* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
* (C) 2009-2010 by Holger Hans Peter Freyther
* (C) 2009-2014 by Holger Hans Peter Freyther
* All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
@ -149,6 +149,40 @@ DEFUN(logging_prnt_timestamp,
return CMD_SUCCESS;
}
DEFUN(logging_prnt_ext_timestamp,
logging_prnt_ext_timestamp_cmd,
"logging print extended-timestamp (0|1)",
LOGGING_STR "Log output settings\n"
"Configure log message timestamping\n"
"Don't prefix each log message\n"
"Prefix each log message with current timestamp with YYYYMMDDhhmmssnnn\n")
{
struct log_target *tgt = osmo_log_vty2tgt(vty);
if (!tgt)
return CMD_WARNING;
log_set_print_extended_timestamp(tgt, atoi(argv[0]));
return CMD_SUCCESS;
}
DEFUN(logging_prnt_cat,
logging_prnt_cat_cmd,
"logging print category (0|1)",
LOGGING_STR "Log output settings\n"
"Configure log message\n"
"Don't prefix each log message\n"
"Prefix each log message with category/subsystem name\n")
{
struct log_target *tgt = osmo_log_vty2tgt(vty);
if (!tgt)
return CMD_WARNING;
log_set_print_category(tgt, atoi(argv[0]));
return CMD_SUCCESS;
}
DEFUN(logging_level,
logging_level_cmd,
NULL, /* cmdstr is dynamically set in logging_vty_add_cmds(). */
@ -625,8 +659,13 @@ static int config_write_log_single(struct vty *vty, struct log_target *tgt)
vty_out(vty, " logging color %u%s", tgt->use_color ? 1 : 0,
VTY_NEWLINE);
vty_out(vty, " logging timestamp %u%s", tgt->print_timestamp ? 1 : 0,
VTY_NEWLINE);
vty_out(vty, " logging print cateyory %d%s",
tgt->print_ext_timestamp ? 1 : 0, VTY_NEWLINE);
if (tgt->print_ext_timestamp)
vty_out(vty, " logging print extended-timestamp 1%s", VTY_NEWLINE);
else
vty_out(vty, " logging timestamp %u%s",
tgt->print_timestamp ? 1 : 0, VTY_NEWLINE);
/* stupid old osmo logging API uses uppercase strings... */
osmo_str2lower(level_lower, log_level_str(tgt->loglevel));
@ -670,6 +709,8 @@ void logging_vty_add_cmds(const struct log_info *cat)
install_element_ve(&logging_fltr_all_cmd);
install_element_ve(&logging_use_clr_cmd);
install_element_ve(&logging_prnt_timestamp_cmd);
install_element_ve(&logging_prnt_ext_timestamp_cmd);
install_element_ve(&logging_prnt_cat_cmd);
install_element_ve(&logging_set_category_mask_cmd);
install_element_ve(&logging_set_category_mask_old_cmd);
@ -685,6 +726,8 @@ void logging_vty_add_cmds(const struct log_info *cat)
install_element(CFG_LOG_NODE, &logging_fltr_all_cmd);
install_element(CFG_LOG_NODE, &logging_use_clr_cmd);
install_element(CFG_LOG_NODE, &logging_prnt_timestamp_cmd);
install_element(CFG_LOG_NODE, &logging_prnt_ext_timestamp_cmd);
install_element(CFG_LOG_NODE, &logging_prnt_cat_cmd);
install_element(CFG_LOG_NODE, &logging_level_cmd);
install_element(CONFIG_NODE, &cfg_log_stderr_cmd);