[PATCH] libosmocore[master]: logging: add ability to log the log-level with API and vty

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Neels Hofmeyr gerrit-no-reply at lists.osmocom.org
Tue Jan 16 02:34:07 UTC 2018


Review at  https://gerrit.osmocom.org/5809

logging: add ability to log the log-level with API and vty

Log the log level string after the category name, if enabled.

The default behavior remains unchanged.

Change-Id: Ie6be365cfa6aeabdf115bff19bac198440c9adf1
---
M include/osmocom/core/logging.h
M src/logging.c
M src/vty/logging_vty.c
3 files changed, 42 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/09/5809/1

diff --git a/include/osmocom/core/logging.h b/include/osmocom/core/logging.h
index 922cbf6..1e809d0 100644
--- a/include/osmocom/core/logging.h
+++ b/include/osmocom/core/logging.h
@@ -298,6 +298,9 @@
 	void (*raw_output)(struct log_target *target, int subsys,
 			   unsigned int level, const char *file, int line,
 			   int cont, const char *format, va_list ap);
+
+	/* Should the log level be printed? */
+	bool print_level;
 };
 
 /* use the above macros */
@@ -320,6 +323,7 @@
 void log_set_print_timestamp(struct log_target *target, int);
 void log_set_print_filename(struct log_target *target, int);
 void log_set_print_category(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);
 const char* log_category_name(int subsys);
diff --git a/src/logging.c b/src/logging.c
index e4f3e87..80fc7d2 100644
--- a/src/logging.c
+++ b/src/logging.c
@@ -353,6 +353,12 @@
 				goto err;
 			OSMO_SNPRINTF_RET(ret, rem, offset, len);
 		}
+		if (target->print_level) {
+			ret = snprintf(buf + offset, rem, "%s ", log_level_str(level));
+			if (ret < 0)
+				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);
@@ -608,6 +614,17 @@
 	target->print_category = print_category;
 }
 
+/*! 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
+ *
+ *  Print the log level name in front of every log message.
+ */
+void log_set_print_level(struct log_target *target, int print_level)
+{
+	target->print_level = (bool)print_level;
+}
+
 /*! Set the global log level for a given log target
  *  \param[in] target Log target to be affected
  *  \param[in] log_level New global log level
diff --git a/src/vty/logging_vty.c b/src/vty/logging_vty.c
index dc457ff..5914822 100644
--- a/src/vty/logging_vty.c
+++ b/src/vty/logging_vty.c
@@ -203,6 +203,23 @@
 	return CMD_SUCCESS;
 }
 
+DEFUN(logging_prnt_level,
+      logging_prnt_level_cmd,
+      "logging print level (0|1)",
+      LOGGING_STR "Log output settings\n"
+      "Configure log message\n"
+      "Don't prefix each log message\n"
+      "Prefix each log message with the log level name\n")
+{
+	struct log_target *tgt = osmo_log_vty2tgt(vty);
+
+	if (!tgt)
+		return CMD_WARNING;
+
+	log_set_print_level(tgt, atoi(argv[0]));
+	return CMD_SUCCESS;
+}
+
 DEFUN(logging_level,
       logging_level_cmd,
       NULL, /* cmdstr is dynamically set in logging_vty_add_cmds(). */
@@ -734,6 +751,8 @@
 	else
 		vty_out(vty, "  logging timestamp %u%s",
 			tgt->print_timestamp ? 1 : 0, VTY_NEWLINE);
+	if (tgt->print_level)
+		vty_out(vty, "  logging print level 1%s", VTY_NEWLINE);
 
 	/* stupid old osmo logging API uses uppercase strings... */
 	osmo_str2lower(level_lower, log_level_str(tgt->loglevel));
@@ -783,6 +802,7 @@
 	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_level_cmd);
 	install_element_ve(&logging_set_category_mask_cmd);
 	install_element_ve(&logging_set_category_mask_old_cmd);
 
@@ -799,6 +819,7 @@
 	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_level_cmd);
 	install_element(CFG_LOG_NODE, &logging_level_cmd);
 
 	install_element(CONFIG_NODE, &cfg_log_stderr_cmd);

-- 
To view, visit https://gerrit.osmocom.org/5809
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie6be365cfa6aeabdf115bff19bac198440c9adf1
Gerrit-PatchSet: 1
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Neels Hofmeyr <nhofmeyr at sysmocom.de>



More information about the gerrit-log mailing list