logging: separate the '<000b>' subsys from filename logging

Add a separate flag and API to switch the category-in-hex output:
log_set_print_category_hex().

Add log_set_print_filename2() to modify only the print_filename flag. The old
log_set_print_filename() function still affects both flags. Explain the
rationale in the comment for log_set_print_filename().

There is no need to deprecate log_set_print_filename(); it might cause compiler
warnings and break strict builds unnecessarily.

Add VTY command 'logging print category-hex (0|1)'.

Since there is no VTY command to switch filename output, nothing needs to be
adjusted there (a command will be added in a subsequent patch).

Change-Id: Iba03a2b7915853c6dccaf6c393c31405320538b4
This commit is contained in:
Neels Hofmeyr 2018-01-16 01:52:29 +01:00 committed by Neels Hofmeyr
parent 1ac277dcef
commit bd7bd3947d
3 changed files with 74 additions and 7 deletions

View File

@ -220,6 +220,12 @@ enum log_target_type {
LOG_TGT_TYPE_GSMTAP, /*!< GSMTAP network logging */
};
/*! Whether/how to log the source filename (and line number). */
enum log_filename_type {
LOG_FILENAME_NONE,
LOG_FILENAME_PATH,
};
/*! structure representing a logging target */
struct log_target {
struct llist_head entry; /*!< linked list */
@ -238,7 +244,7 @@ struct log_target {
unsigned int use_color:1;
/*! should log messages be prefixed with a timestamp? */
unsigned int print_timestamp:1;
/*! should log messages be prefixed with a filename? */
/*! DEPRECATED: use print_filename2 instead. */
unsigned int print_filename:1;
/*! should log messages be prefixed with a category name? */
unsigned int print_category:1;
@ -301,6 +307,10 @@ struct log_target {
/* Should the log level be printed? */
bool print_level;
/* Should we print the subsys in hex like '<000b>'? */
bool print_category_hex;
/* Should we print the source file and line, and in which way? */
enum log_filename_type print_filename2;
};
/* use the above macros */
@ -322,7 +332,9 @@ 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_filename2(struct log_target *target, enum log_filename_type lft);
void log_set_print_category(struct log_target *target, int);
void log_set_print_category_hex(struct log_target *target, int);
void log_set_print_level(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);

View File

@ -385,13 +385,22 @@ static void _output(struct log_target *target, unsigned int subsys,
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);
if (target->print_category_hex) {
ret = snprintf(buf + offset, rem, "<%4.4x> ", subsys);
if (ret < 0)
goto err;
OSMO_SNPRINTF_RET(ret, rem, offset, len);
}
switch (target->print_filename2) {
case LOG_FILENAME_NONE:
break;
case LOG_FILENAME_PATH:
ret = snprintf(buf + offset, rem, "%s:%d ", file, line);
if (ret < 0)
goto err;
OSMO_SNPRINTF_RET(ret, rem, offset, len);
break;
}
}
ret = vsnprintf(buf + offset, rem, format, ap);
if (ret < 0)
@ -621,13 +630,30 @@ void log_set_print_extended_timestamp(struct log_target *target, int print_times
target->print_ext_timestamp = print_timestamp;
}
/*! Enable or disable printing of the filename while logging
/*! Use log_set_print_filename2() instead.
* Call log_set_print_filename2() with LOG_FILENAME_PATH or LOG_FILENAME_NONE, *as well as* call
* log_set_print_category_hex() with the argument passed to this function. This is to mirror legacy
* behavior, which combined the category in hex with the filename. For example, if the category-hex
* output were no longer affected by log_set_print_filename(), many unit tests (in libosmocore as well as
* dependent projects) would fail since they expect the category to disappear along with the filename.
* \param[in] target Log target to be affected
* \param[in] print_filename Enable (1) or disable (0) filenames
*/
void log_set_print_filename(struct log_target *target, int print_filename)
{
target->print_filename = print_filename;
log_set_print_filename2(target, print_filename ? LOG_FILENAME_PATH : LOG_FILENAME_NONE);
log_set_print_category_hex(target, print_filename);
}
/*! Enable or disable printing of the filename while logging.
* \param[in] target Log target to be affected.
* \param[in] print_filename An LOG_FILENAME_* enum value.
* LOG_FILENAME_NONE omits the source file and line information from logs.
* LOG_FILENAME_PATH prints the entire source file path as passed to LOGP macros.
*/
void log_set_print_filename2(struct log_target *target, enum log_filename_type lft)
{
target->print_filename2 = lft;
}
/*! Enable or disable printing of the category name
@ -641,6 +667,15 @@ void log_set_print_category(struct log_target *target, int print_category)
target->print_category = print_category;
}
/*! Enable or disable printing of the category number in hex ('<000b>').
* \param[in] target Log target to be affected.
* \param[in] print_category_hex Enable (1) or disable (0) hex category.
*/
void log_set_print_category_hex(struct log_target *target, int print_category_hex)
{
target->print_category_hex = print_category_hex;
}
/*! Enable or disable printing of the log level name.
* \param[in] target Log target to be affected
* \param[in] print_catname Enable (1) or disable (0) filenames
@ -723,7 +758,8 @@ struct log_target *log_target_create(void)
/* global settings */
target->use_color = 1;
target->print_timestamp = 0;
target->print_filename = 1;
target->print_filename2 = LOG_FILENAME_PATH;
target->print_category_hex = true;
/* global log level */
target->loglevel = 0;

View File

@ -203,6 +203,23 @@ DEFUN(logging_prnt_cat,
return CMD_SUCCESS;
}
DEFUN(logging_prnt_cat_hex,
logging_prnt_cat_hex_cmd,
"logging print category-hex (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 nr in hex ('<000b>')\n")
{
struct log_target *tgt = osmo_log_vty2tgt(vty);
if (!tgt)
return CMD_WARNING;
log_set_print_category_hex(tgt, atoi(argv[0]));
return CMD_SUCCESS;
}
DEFUN(logging_prnt_level,
logging_prnt_level_cmd,
"logging print level (0|1)",
@ -802,6 +819,7 @@ void logging_vty_add_cmds()
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_prnt_cat_hex_cmd);
install_element_ve(&logging_prnt_level_cmd);
install_element_ve(&logging_set_category_mask_cmd);
install_element_ve(&logging_set_category_mask_old_cmd);
@ -819,6 +837,7 @@ void logging_vty_add_cmds()
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_prnt_cat_hex_cmd);
install_element(CFG_LOG_NODE, &logging_prnt_level_cmd);
install_element(CFG_LOG_NODE, &logging_level_cmd);