[PATCH] libosmocore[master]: logging: allow to log only the basename of each source

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:09 UTC 2018


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

logging: allow to log only the basename of each source

In the VTY print filename command, add another parameter 'basename' to yield:

  logging print filename (0|1|basename|with-cat)

In the C API, add another function log_set_print_basename() (when set overrides
log_set_print_filename()), and to struct log_target add another flag
print_basename.

Rationale: especially when not building directly in the source dir, the paths
to the source files can become rather long. Usually, just the basename of the
file is sufficient to identify the source line.

I see printout of the hex representation of the logging category as legacy
behavior, so I don't bother to also allow printing the basename along with the
hex category.

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


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/14/5814/1

diff --git a/include/osmocom/core/logging.h b/include/osmocom/core/logging.h
index 149ef82..ba4c535 100644
--- a/include/osmocom/core/logging.h
+++ b/include/osmocom/core/logging.h
@@ -303,6 +303,8 @@
 	bool print_level;
 	/* With filename output, should the subsys be printed as hex like '<000b>'? */
 	bool print_filename_subsys_hex;
+	/* With filename output, print just the basename? */
+	bool print_basename;
 };
 
 /* use the above macros */
@@ -324,6 +326,7 @@
 void log_set_print_extended_timestamp(struct log_target *target, int);
 void log_set_print_timestamp(struct log_target *target, int);
 void log_set_print_filename(struct log_target *target, int);
+void log_set_print_basename(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);
diff --git a/src/logging.c b/src/logging.c
index 254afeb..969ad34 100644
--- a/src/logging.c
+++ b/src/logging.c
@@ -323,6 +323,14 @@
 	return NULL;
 }
 
+static const char *const_basename(const char *path)
+{
+	const char *bn = strrchr(path, '/');
+	if (!bn || !bn[1])
+		return path;
+	return bn + 1;
+}
+
 static void _output(struct log_target *target, unsigned int subsys,
 		    unsigned int level, const char *file, int line, int cont,
 		    const char *format, va_list ap)
@@ -385,13 +393,18 @@
 				goto err;
 			OSMO_SNPRINTF_RET(ret, rem, offset, len);
 		}
-		if (target->print_filename && target->print_filename_subsys_hex) {
-			ret = snprintf(buf + offset, rem, "<%4.4x> ", subsys);
+		if (target->print_basename) {
+			ret = snprintf(buf + offset, rem, "%s:%d ", const_basename(file), line);
 			if (ret < 0)
 				goto err;
 			OSMO_SNPRINTF_RET(ret, rem, offset, len);
-		}
-		if (target->print_filename) {
+		} else if (target->print_filename) {
+			if (target->print_filename_subsys_hex) {
+				ret = snprintf(buf + offset, rem, "<%4.4x> ", subsys);
+				if (ret < 0)
+					goto err;
+				OSMO_SNPRINTF_RET(ret, rem, offset, len);
+			}
 			ret = snprintf(buf + offset, rem, "%s:%d ", file, line);
 			if (ret < 0)
 				goto err;
@@ -643,6 +656,18 @@
 	target->print_filename_subsys_hex = (print_filename == 2)? false : true;
 }
 
+/*! Enable or disable printing of the filename's basename while logging.
+ * If both log_set_print_filename() and log_set_print_basename() are enabled,
+ * log_set_print_basename() overrides and only the basename (without hex
+ * category) is printed.
+ *  \param[in] target Log target to be affected.
+ *  \param[in] print_basename Enable (1) or disable (0) basenames.
+ */
+void log_set_print_basename(struct log_target *target, int print_basename)
+{
+	target->print_basename = (bool)print_basename;
+}
+
 /*! Enable or disable printing of the category name
  *  \param[in] target Log target to be affected
  *  \param[in] print_catname Enable (1) or disable (0) filenames
diff --git a/src/vty/logging_vty.c b/src/vty/logging_vty.c
index 4e2cefc..5f61067 100644
--- a/src/vty/logging_vty.c
+++ b/src/vty/logging_vty.c
@@ -222,11 +222,12 @@
 
 DEFUN(logging_prnt_file,
       logging_prnt_file_cmd,
-      "logging print file (0|1|with-cat)",
+      "logging print file (0|1|basename|with-cat)",
       LOGGING_STR "Log output settings\n"
       "Configure log message\n"
       "Don't prefix each log message\n"
       "Prefix each log message with the source file and line\n"
+      "Prefix each log message with the source file's basename (strip leading paths) and line\n"
       "Prefix each log message with the subsys in hex and the source file and line"
       " (legacy behavior)\n")
 {
@@ -236,6 +237,14 @@
 	if (!tgt)
 		return CMD_WARNING;
 
+	if (!strcmp(argv[0], "basename")) {
+		log_set_print_basename(tgt, 1);
+		/* log_set_print_basename() overrides log_set_print_filename(),
+		 * so no need to change its value. */
+		return CMD_SUCCESS;
+	} else
+		log_set_print_basename(tgt, 0);
+
 	if (!strcmp(argv[0], "with-cat"))
 		val = 1;
 	else

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: If3e4d5fb2066f8bf86e59c82d1752b1a843cf58e
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