aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNeels Hofmeyr <nhofmeyr@sysmocom.de>2023-04-22 22:41:33 +0200
committerNeels Hofmeyr <nhofmeyr@sysmocom.de>2023-04-24 18:05:38 +0200
commit957b8277a2b3e775bdfd3e5dbc46f477929f4129 (patch)
treeb5c81ffec2eb6cbf6c40a5b3fa68b28c7a45a220
parent6496cf16e95ac23d4e22357de41db2a319819c26 (diff)
add API logging_vty_subsys_strip_leading_char()neels/subsys
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
-rw-r--r--include/osmocom/core/logging.h1
-rw-r--r--src/core/libosmocore.map1
-rw-r--r--src/core/logging.c23
3 files changed, 24 insertions, 1 deletions
diff --git a/include/osmocom/core/logging.h b/include/osmocom/core/logging.h
index 2d2fabcc..a1a90008 100644
--- a/include/osmocom/core/logging.h
+++ b/include/osmocom/core/logging.h
@@ -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 */
diff --git a/src/core/libosmocore.map b/src/core/libosmocore.map
index 6ef0cea9..f88abfc7 100644
--- a/src/core/libosmocore.map
+++ b/src/core/libosmocore.map
@@ -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;
diff --git a/src/core/logging.c b/src/core/logging.c
index c8bd8d37..b96a282f 100644
--- a/src/core/logging.c
+++ b/src/core/logging.c
@@ -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