[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
Wed Jan 17 15:04:44 UTC 2018


Hello Harald Welte, Jenkins Builder,

I'd like you to reexamine a change.  Please visit

    https://gerrit.osmocom.org/5814

to look at the new patch set (#2).

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, 40 insertions(+), 3 deletions(-)


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

diff --git a/include/osmocom/core/logging.h b/include/osmocom/core/logging.h
index f68991e..68dac98 100644
--- a/include/osmocom/core/logging.h
+++ b/include/osmocom/core/logging.h
@@ -303,6 +303,8 @@
 	bool print_level;
 	/* Should we print the subsys in hex like '<000b>'? */
 	bool print_category_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_category_hex(struct log_target *target, int);
 void log_set_print_level(struct log_target *target, int);
diff --git a/src/logging.c b/src/logging.c
index 650598f..0ec12ab 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)
@@ -391,7 +399,12 @@
 				goto err;
 			OSMO_SNPRINTF_RET(ret, rem, offset, len);
 		}
-		if (target->print_filename) {
+		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);
+		} else if (target->print_filename) {
 			ret = snprintf(buf + offset, rem, "%s:%d ", file, line);
 			if (ret < 0)
 				goto err;
@@ -635,6 +648,18 @@
 	target->print_filename = print_filename;
 }
 
+/*! 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 bccc278..48f6871 100644
--- a/src/vty/logging_vty.c
+++ b/src/vty/logging_vty.c
@@ -239,17 +239,26 @@
 
 DEFUN(logging_prnt_file,
       logging_prnt_file_cmd,
-      "logging print file (0|1)",
+      "logging print file (0|1|basename)",
       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 and line\n"
+      "Prefix each log message with the source file's basename (strip leading paths) and line\n")
 {
 	struct log_target *tgt = osmo_log_vty2tgt(vty);
 
 	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);
+
 	log_set_print_filename(tgt, atoi(argv[0]));
 	return CMD_SUCCESS;
 }

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

Gerrit-MessageType: newpatchset
Gerrit-Change-Id: If3e4d5fb2066f8bf86e59c82d1752b1a843cf58e
Gerrit-PatchSet: 2
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Neels Hofmeyr <nhofmeyr at sysmocom.de>
Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Jenkins Builder



More information about the gerrit-log mailing list