[PATCH] libosmocore[master]: logging: use enum to set timestamp format

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
Thu Jan 18 01:36:25 UTC 2018


Hello Jenkins Builder,

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

    https://gerrit.osmocom.org/5857

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

logging: use enum to set timestamp format

In preparation of adding a new timestamp format, refactor to use an enum to
pick the timestamp style.

Introduce log_set_print_timestamp2() to be able to pass an enum value directly
(for future additions). Make the old API of log_set_print_timestamp() and
log_set_print_extended_timestamp() write an enum value instead.

The struct log_target members print_timestamp and print_ext_timestamp now
become unused, only the new enum member print_timestamp2 has an effect.
The old API functions still work.

This slightly changes the API behavior, in that enabling the extended timestamp
now disables the epoch timestamp, even after switching the extended timestamp
off again. Before, switching off the extended timestamp would bring back the
epoch timestamp. The API arguably does behave as a reader would expect, in that
it switches the timestamp format absolutely, without "invisible state" in the
background. Note this in TODO-RELEASE.

Change-Id: I70ecee543c266df54191d3e86401466cd1a5c0a6
---
M TODO-RELEASE
M include/osmocom/core/logging.h
M src/logging.c
M src/vty/logging_vty.c
4 files changed, 78 insertions(+), 33 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/57/5857/4

diff --git a/TODO-RELEASE b/TODO-RELEASE
index 782ba19..8353be7 100644
--- a/TODO-RELEASE
+++ b/TODO-RELEASE
@@ -10,3 +10,11 @@
 core		msgb_queue_free()	add inline func to msgb.h
 coding		gsm0503_rach_ext-encode()	add func to gsm0503_coding.h
 codec		ecu.c / ecu.h			implement ECU for FR (Error Concealment Unit)
+core		log_target.print_timestamp,	These two flags are now replaced by
+		log_target.print_ext_timestamp	print_timestamp2 enum -- callers should use
+						log_set_print*() functions anyway.
+core		log_set_print_timestamp()	Calling any other log_set_print_timestamp*()
+						function now is like log_set_print_timestamp(0),
+						so the epoch timestamp does not come back after
+						log_set_print_extended_timestamp(0); callers
+						should use log_set_print_timestamp2() now.
diff --git a/include/osmocom/core/logging.h b/include/osmocom/core/logging.h
index 617d78e..f3f3f2b 100644
--- a/include/osmocom/core/logging.h
+++ b/include/osmocom/core/logging.h
@@ -227,6 +227,13 @@
 	LOG_FILENAME_BASENAME,
 };
 
+/*! Format of the timestamp printed */
+enum log_timestamp_format {
+	LOG_TIMESTAMP_NONE,
+	LOG_TIMESTAMP_EPOCH,
+	LOG_TIMESTAMP_DATE_PACKED,
+};
+
 /*! structure representing a logging target */
 struct log_target {
         struct llist_head entry;		/*!< linked list */
@@ -243,13 +250,15 @@
 	uint8_t loglevel;
 	/*! should color be used when printing log messages? */
 	unsigned int use_color:1;
-	/*! should log messages be prefixed with a timestamp? */
+	/*! should log messages be prefixed with a timestamp?
+	 * DEPRECATED: use print_timestamp2 instead. */
 	unsigned int print_timestamp:1;
 	/*! DEPRECATED: use print_filename2 instead. */
 	unsigned int print_filename:1;
 	/*! should log messages be prefixed with a category name? */
 	unsigned int print_category:1;
-	/*! should log messages be prefixed with an extended timestamp? */
+	/*! should log messages be prefixed with an extended timestamp?
+	 * DEPRECATED: use print_timestamp2 instead. */
 	unsigned int print_ext_timestamp:1;
 
 	/*! the type of this log taget */
@@ -312,6 +321,8 @@
 	bool print_category_hex;
 	/* Should we print the source file and line, and in which way? */
 	enum log_filename_type print_filename2;
+	/* Timestamp format */
+	enum log_timestamp_format print_timestamp2;
 };
 
 /* use the above macros */
@@ -332,6 +343,7 @@
 void log_set_use_color(struct log_target *target, int);
 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_timestamp2(struct log_target *target, enum log_timestamp_format format);
 void log_set_print_filename(struct log_target *target, int);
 void log_set_print_filename2(struct log_target *target, enum log_filename_type lft);
 void log_set_print_category(struct log_target *target, int);
diff --git a/src/logging.c b/src/logging.c
index 66074ea..7b3a2fd 100644
--- a/src/logging.c
+++ b/src/logging.c
@@ -350,28 +350,38 @@
 		}
 	}
 	if (!cont) {
-		if (target->print_ext_timestamp) {
-			struct tm tm;
-			struct timeval tv;
-			osmo_gettimeofday(&tv, NULL);
-			localtime_r(&tv.tv_sec, &tm);
-			ret = snprintf(buf + offset, rem, "%04d%02d%02d%02d%02d%02d%03d ",
-					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));
-			if (ret < 0)
-				goto err;
-			OSMO_SNPRINTF_RET(ret, rem, offset, len);
-		} else if (target->print_timestamp) {
-			char *timestr;
-			time_t tm;
-			tm = time(NULL);
-			timestr = ctime(&tm);
-			timestr[strlen(timestr)-1] = '\0';
-			ret = snprintf(buf + offset, rem, "%s ", timestr);
-			if (ret < 0)
-				goto err;
-			OSMO_SNPRINTF_RET(ret, rem, offset, len);
+		switch (target->print_timestamp2) {
+		case LOG_TIMESTAMP_NONE:
+			break;
+		case LOG_TIMESTAMP_EPOCH:
+			{
+				char *timestr;
+				time_t tm;
+				tm = time(NULL);
+				timestr = ctime(&tm);
+				timestr[strlen(timestr)-1] = '\0';
+				ret = snprintf(buf + offset, rem, "%s ", timestr);
+				if (ret < 0)
+					goto err;
+				OSMO_SNPRINTF_RET(ret, rem, offset, len);
+			}
+			break;
+		case LOG_TIMESTAMP_DATE_PACKED:
+			{
+				struct tm tm;
+				struct timeval tv;
+				const char *ts_fmt = "%04d%02d%02d%02d%02d%02d%03d ";
+				osmo_gettimeofday(&tv, NULL);
+				localtime_r(&tv.tv_sec, &tm);
+				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));
+				if (ret < 0)
+					goto err;
+				OSMO_SNPRINTF_RET(ret, rem, offset, len);
+			}
+			break;
 		}
 		if (target->print_category) {
 			ret = snprintf(buf + offset, rem, "%s%s%s%s ",
@@ -622,16 +632,18 @@
 	target->use_color = use_color;
 }
 
-/*! Enable or disable printing of timestamps while logging
+/*! Use log_set_print_timestamp2(LOG_TIMESTAMP_EPOCH) instead.
+ * Enable or disable printing of timestamps while logging.
  *  \param[in] target Log target to be affected
  *  \param[in] print_timestamp Enable (1) or disable (0) timestamps
  */
 void log_set_print_timestamp(struct log_target *target, int print_timestamp)
 {
-	target->print_timestamp = print_timestamp;
+	log_set_print_timestamp2(target, print_timestamp ? LOG_TIMESTAMP_EPOCH : LOG_TIMESTAMP_NONE);
 }
 
-/*! Enable or disable printing of extended timestamps while logging
+/*! Use log_set_print_timestamp2(LOG_TIMESTAMP_DATE_PACKED) instead.
+ * 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
  *
@@ -641,7 +653,20 @@
  */
 void log_set_print_extended_timestamp(struct log_target *target, int print_timestamp)
 {
-	target->print_ext_timestamp = print_timestamp;
+	log_set_print_timestamp2(target, print_timestamp ? LOG_TIMESTAMP_DATE_PACKED : LOG_TIMESTAMP_NONE);
+}
+
+/*! Enable or disable printing of timestamps while logging.
+ *  \param[in] target Log target to be affected
+ *  \param[in] fmt LOG_TIMESTAMP_* value to indicate the format.
+ *
+ * LOG_TIMESTAMP_NONE switches off the timestamp output.
+ * LOG_TIMESTAMP_EPOCH prints the seconds since the epoch (1970-1-1).
+ * LOG_TIMESTAMP_DATE_PACKED prints YYYYMMDDhhmmssnnn.
+ */
+void log_set_print_timestamp2(struct log_target *target, enum log_timestamp_format fmt)
+{
+	target->print_timestamp2 = fmt;
 }
 
 /*! Use log_set_print_filename2() instead.
@@ -771,7 +796,7 @@
 
 	/* global settings */
 	target->use_color = 1;
-	target->print_timestamp = 0;
+	target->print_timestamp2 = LOG_TIMESTAMP_NONE;
 	target->print_filename2 = LOG_FILENAME_PATH;
 	target->print_category_hex = true;
 
diff --git a/src/vty/logging_vty.c b/src/vty/logging_vty.c
index 0eaa7fd..e4c4bb8 100644
--- a/src/vty/logging_vty.c
+++ b/src/vty/logging_vty.c
@@ -165,7 +165,7 @@
 	if (!tgt)
 		return CMD_WARNING;
 
-	log_set_print_timestamp(tgt, atoi(argv[0]));
+	log_set_print_timestamp2(tgt, atoi(argv[0]) ? LOG_TIMESTAMP_EPOCH : LOG_TIMESTAMP_NONE);
 	return CMD_SUCCESS;
 }
 
@@ -182,7 +182,7 @@
 	if (!tgt)
 		return CMD_WARNING;
 
-	log_set_print_extended_timestamp(tgt, atoi(argv[0]));
+	log_set_print_timestamp2(tgt, atoi(argv[0]) ? LOG_TIMESTAMP_DATE_PACKED : LOG_TIMESTAMP_NONE);
 	return CMD_SUCCESS;
 }
 
@@ -786,11 +786,11 @@
 		VTY_NEWLINE);
 	vty_out(vty, "  logging print category %d%s",
 		tgt->print_category ? 1 : 0, VTY_NEWLINE);
-	if (tgt->print_ext_timestamp)
+	if (tgt->print_timestamp2 == LOG_TIMESTAMP_DATE_PACKED)
 		vty_out(vty, "  logging print extended-timestamp 1%s", VTY_NEWLINE);
 	else
 		vty_out(vty, "  logging timestamp %u%s",
-			tgt->print_timestamp ? 1 : 0, VTY_NEWLINE);
+			tgt->print_timestamp2 ? 1 : 0, VTY_NEWLINE);
 	if (tgt->print_level)
 		vty_out(vty, "  logging print level 1%s", VTY_NEWLINE);
 	if (tgt->print_filename)

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

Gerrit-MessageType: newpatchset
Gerrit-Change-Id: I70ecee543c266df54191d3e86401466cd1a5c0a6
Gerrit-PatchSet: 4
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Neels Hofmeyr <nhofmeyr at sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: Neels Hofmeyr <nhofmeyr at sysmocom.de>



More information about the gerrit-log mailing list