diff --git a/include/osmocom/core/logging.h b/include/osmocom/core/logging.h index da90d58ef..e58c25c3b 100644 --- a/include/osmocom/core/logging.h +++ b/include/osmocom/core/logging.h @@ -430,6 +430,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 */ diff --git a/src/core/libosmocore.map b/src/core/libosmocore.map index fa790956c..54eae9256 100644 --- a/src/core/libosmocore.map +++ b/src/core/libosmocore.map @@ -71,6 +71,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; diff --git a/src/core/logging.c b/src/core/logging.c index 2429a0c68..ba2291471 100644 --- a/src/core/logging.c +++ b/src/core/logging.c @@ -428,11 +428,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