add API logging_vty_subsys_strip_leading_char()

Allow an application to use subsystem names without a leading 'D'. With
this patch, a program can:
- remove the 'D' from struct log_info_cat[] entries -- now VTY would
  strip the 'M' from "MAIN" and we'd get 'ain' on the VTY.
- call logging_vty_subsys_strip_leading_char(false) during startup --
  hence the VTY does not strip anything, and we get 'main' on the VTY
  config.

So this allows removing the odd 'D' from category names without any
changes in the VTY configuration.

Background: I am hacking on some project and decided to use libosmovty
and libosmocore logging, but I want logging subsys names without a
leading 'D'. So I noticed that the cfg file has mangled category names.

Change-Id: I5faedf7d6525d744a734ebe54c185fcc904f763e
This commit is contained in:
Neels Hofmeyr 2023-04-22 22:41:33 +02:00
parent 6496cf16e9
commit 957b8277a2
3 changed files with 24 additions and 1 deletions

View File

@ -428,6 +428,7 @@ int log_parse_category(const char *category);
void log_set_category_filter(struct log_target *target, int category,
int enable, int level);
void log_subsys_strip_leading_char(bool do_strip);
const char *log_subsys_name(const struct log_info *log_info, int cat_idx);
/* management of the targets */

View File

@ -67,6 +67,7 @@ logp2;
log_parse_category;
log_parse_category_mask;
log_parse_level;
log_subsys_strip_leading_char;
log_subsys_name;
logp_stub;
log_reset_context;

View File

@ -348,11 +348,32 @@ const char *log_level_str(unsigned int lvl)
return get_value_string(loglevel_strs, lvl);
}
static bool g_subsys_strip_leading_char = true;
/** Set whether on the VTY, the leading 'D' commonly in use for category names should be stripped.
* If true, a category name 'DMAIN' will be identified on VTY as 'main'.
* If false, a category name 'FOO' will be identified on VTY as 'foo' (instead of 'oo').
*
* This must be called *before* logging_vty_add_cmds() to take effect!
*
* There is a common coding style in osmocom that all category names start with a 'D'.
* This flag allows programs to name logging categories without a leading 'D'.
* \param[in] do_strip true to strip leading D on VTY, false to use names as-is.
*/
void log_subsys_strip_leading_char(bool do_strip)
{
g_subsys_strip_leading_char = do_strip;
}
/* skip the leading 'D' in category name */
const char *log_subsys_name(const struct log_info *log_info, int cat_idx)
{
const char *name = log_info->cat[cat_idx].name;
return name + 1;
/* The category names in internal_cat[] will always have a leading 'D'. */
if (g_subsys_strip_leading_char
|| cat_idx >= log_info->num_cat_user)
return name + 1;
return name;
}
/*! parse a human-readable log category into numeric form