[PATCH] libosmocore[master]: logging: allow adding separators to the extended-timestamp

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/5815

logging: allow adding separators to the extended-timestamp

Add a log target config to allow separating the extended timestamp.
Before: 20180116014434681
After:  2018-01-16 01:44:34.681

The default behavior, previous API argument behavior and VTY command behavior
remain unchanged.

In the C API, enable the separators by passing 2 to
log_set_print_extended_timestamp(). Passing 1 still produces the unseparated
extended-timestamp as before.

In the struct log_target, add a separate flag to the end to stay ABI
compatible. The print_ext_timestamp is a single-bit field, which we could
enlarge assuming the struct is never packed, but adding a new flag is safer.

In the VTY, add a new argument 'separated':
  logging print extended-timestamp (0|1|separated)

Rationale: when reading log files, parsing the timestamp manually can be
cumbersome. Human eyes aren't good at counting digits, I keep getting the
minutes, seconds and millis wrong and waste my time on every glance.

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


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/15/5815/1

diff --git a/include/osmocom/core/logging.h b/include/osmocom/core/logging.h
index ba4c535..87452ec 100644
--- a/include/osmocom/core/logging.h
+++ b/include/osmocom/core/logging.h
@@ -305,6 +305,8 @@
 	bool print_filename_subsys_hex;
 	/* With filename output, print just the basename? */
 	bool print_basename;
+	/* Add human-readable separators to the extended-timestamp */
+	bool print_ext_timestamp_separators;
 };
 
 /* use the above macros */
diff --git a/src/logging.c b/src/logging.c
index 969ad34..1ce8878 100644
--- a/src/logging.c
+++ b/src/logging.c
@@ -353,9 +353,12 @@
 		if (target->print_ext_timestamp) {
 			struct tm tm;
 			struct timeval tv;
+			const char *ts_fmt = target->print_ext_timestamp_separators
+				? "%04d-%02d-%02d %02d:%02d:%02d.%03d "
+				: "%04d%02d%02d%02d%02d%02d%03d ";
 			osmo_gettimeofday(&tv, NULL);
 			localtime_r(&tv.tv_sec, &tm);
-			ret = snprintf(buf + offset, rem, "%04d%02d%02d%02d%02d%02d%03d ",
+			ret = snprintf(buf + offset, rem, ts_fmt,
 					tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
 					tm.tm_hour, tm.tm_min, tm.tm_sec,
 					(int)(tv.tv_usec / 1000));
@@ -627,15 +630,17 @@
 
 /*! Enable or disable printing of extended timestamps while logging
  *  \param[in] target Log target to be affected
- *  \param[in] print_timestamp Enable (1) or disable (0) timestamps
+ *  \param[in] print_timestamp Enable (1) or disable (0) timestamps,
+ *                             or 2 to enable with human readable separators.
  *
  * When both timestamp and extended timestamp is enabled then only
  * the extended timestamp will be used. The format of the timestamp
- * is YYYYMMDDhhmmssnnn.
+ * is YYYYMMDDhhmmssnnn, or YYYY-MM-DD hh:mm:ss.nnn if 2 is passed as argument.
  */
 void log_set_print_extended_timestamp(struct log_target *target, int print_timestamp)
 {
-	target->print_ext_timestamp = print_timestamp;
+	target->print_ext_timestamp = print_timestamp ? 1 : 0;
+	target->print_ext_timestamp_separators = (print_timestamp == 2);
 }
 
 /*! Enable or disable printing of the filename while logging.
diff --git a/src/vty/logging_vty.c b/src/vty/logging_vty.c
index 5f61067..e4e9bfe 100644
--- a/src/vty/logging_vty.c
+++ b/src/vty/logging_vty.c
@@ -171,18 +171,24 @@
 
 DEFUN(logging_prnt_ext_timestamp,
       logging_prnt_ext_timestamp_cmd,
-      "logging print extended-timestamp (0|1)",
+      "logging print extended-timestamp (0|1|separated)",
 	LOGGING_STR "Log output settings\n"
 	"Configure log message timestamping\n"
 	"Don't prefix each log message\n"
-	"Prefix each log message with current timestamp with YYYYMMDDhhmmssnnn\n")
+	"Prefix each log message with current timestamp with YYYYMMDDhhmmssnnn\n"
+	"Prefix each log message with current timestamp with YYYY-MM-DD hh:mm:ss.nnn\n")
 {
 	struct log_target *tgt = osmo_log_vty2tgt(vty);
+	int val;
 
 	if (!tgt)
 		return CMD_WARNING;
 
-	log_set_print_extended_timestamp(tgt, atoi(argv[0]));
+	if (!strcmp(argv[0], "separated"))
+		val = 2;
+	else
+		val = atoi(argv[0]);
+	log_set_print_extended_timestamp(tgt, val);
 	return CMD_SUCCESS;
 }
 
@@ -780,7 +786,9 @@
 	vty_out(vty, "  logging print category %d%s",
 		tgt->print_category ? 1 : 0, VTY_NEWLINE);
 	if (tgt->print_ext_timestamp)
-		vty_out(vty, "  logging print extended-timestamp 1%s", VTY_NEWLINE);
+		vty_out(vty, "  logging print extended-timestamp %s%s",
+			tgt->print_ext_timestamp_separators? "separated" : "1",
+			VTY_NEWLINE);
 	else
 		vty_out(vty, "  logging timestamp %u%s",
 			tgt->print_timestamp ? 1 : 0, VTY_NEWLINE);

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

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