[PATCH 1/3] logging: Implement subsecond resolution of extended timestamps

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/OpenBSC@lists.osmocom.org/.

Jacob Erlbeck jerlbeck at sysmocom.de
Tue Mar 17 09:21:15 UTC 2015


Currently when using 'logging print extended-timestamp 1', the
subsecond part (milliseconds) of the printed timestamp is always 0.
This makes it difficult to correlate log entries with PCAP file
entries if there are many of them per second.

This patch changes _output in logging.c to use gettimeofday() instead
of time() when extended timestamps are enabled and replaces the '000'
by the milliseconds computed from tv_usec.

Sponsored-by: On-Waves ehf
---
 src/logging.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/src/logging.c b/src/logging.c
index c007a45..20b0596 100644
--- a/src/logging.c
+++ b/src/logging.c
@@ -38,6 +38,7 @@
 #include <strings.h>
 #endif
 #include <time.h>
+#include <sys/time.h>
 #include <errno.h>
 
 #include <osmocom/core/talloc.h>
@@ -254,11 +255,13 @@ static void _output(struct log_target *target, unsigned int subsys,
 	if (!cont) {
 		if (target->print_ext_timestamp) {
 			struct tm tm;
-			time_t timep = time(NULL);
-			localtime_r(&timep, &tm);
-			ret = snprintf(buf + offset, rem, "%04d%02d%02d%02d%02d%02d000 ",
+			struct timeval tv;
+			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);
+					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);
-- 
1.9.1




More information about the OpenBSC mailing list