neels has uploaded this change for review. (
https://gerrit.osmocom.org/c/libosmocore/+/36546?usp=email )
Change subject: add API logging_vty_subsys_strip_leading_char()
......................................................................
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 -- normally, the VTY
would then strip the 'M' from "MAIN" and we'd get 'ain'.
- 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 an application to remove the odd 'D' from category names,
without any changes in any VTY configuration.
Background: I am hacking on some project where I want logging subsys
names without a leading 'D'. So I noticed that the cfg file has mangled
category names.
Change-Id: I5faedf7d6525d744a734ebe54c185fcc904f763e
---
M include/osmocom/core/logging.h
M src/core/libosmocore.map
M src/core/logging.c
3 files changed, 48 insertions(+), 1 deletion(-)
git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/46/36546/1
diff --git a/include/osmocom/core/logging.h b/include/osmocom/core/logging.h
index da90d58..e58c25c 100644
--- a/include/osmocom/core/logging.h
+++ b/include/osmocom/core/logging.h
@@ -430,6 +430,7 @@
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 3bb2abd..919897a 100644
--- a/src/core/libosmocore.map
+++ b/src/core/libosmocore.map
@@ -71,6 +71,7 @@
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 2429a0c..ba22914 100644
--- a/src/core/logging.c
+++ b/src/core/logging.c
@@ -428,11 +428,32 @@
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
--
To view, visit
https://gerrit.osmocom.org/c/libosmocore/+/36546?usp=email
To unsubscribe, or for help writing mail filters, visit
https://gerrit.osmocom.org/settings
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: I5faedf7d6525d744a734ebe54c185fcc904f763e
Gerrit-Change-Number: 36546
Gerrit-PatchSet: 1
Gerrit-Owner: neels <nhofmeyr(a)sysmocom.de>
Gerrit-MessageType: newchange