Thread (9 messages) 9 messages, 2 authors, 2026-02-23
STALE151d
Revisions (2)
  1. v1 current
  2. v2 [diff vs current]

[PATCH iproute2-next 2/3] utils: timestamp: add JSON support

From: "Matthieu Baerts (NGI0)" <matttbe@kernel.org>
Date: 2026-02-20 18:54:18
Also in: mptcp
Subsystem: library code, the rest · Maintainers: Andrew Morton, Linus Torvalds

Only if the output stream is 'stdout', because all JSON helpers like
print_string() only write on 'stdout'.

Supporting JSON is easy with the helpers. The biggest modification is to
extract the end value.

No behavioural changes intended for the moment, this is a preparation for a
future usage of print_timestamp() within a JSON context.

Signed-off-by: Matthieu Baerts (NGI0) <matttbe@kernel.org>
---
 lib/utils.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
diff --git a/lib/utils.c b/lib/utils.c
index 13e8c098..d7581709 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -1277,21 +1277,30 @@ int print_timestamp(FILE *fp)
 {
 	struct timeval tv;
 	struct tm *tm;
+	char ts[40];
 
 	gettimeofday(&tv, NULL);
 	tm = localtime(&tv.tv_sec);
 
 	if (timestamp_short) {
-		char tshort[40];
+		size_t len;
 
-		strftime(tshort, sizeof(tshort), "%Y-%m-%dT%H:%M:%S", tm);
-		fprintf(fp, "[%s.%06ld] ", tshort, tv.tv_usec);
+		len = strftime(ts, sizeof(ts), "%Y-%m-%dT%H:%M:%S", tm);
+		snprintf(ts + len, sizeof(ts) - len, ".%06ld", tv.tv_usec);
+		if (fp == stdout)
+			print_string(PRINT_ANY, "timestamp_short", "[%s] ", ts);
+		else
+			fprintf(fp, "[%s] ", ts);
 	} else {
 		char *tstr = asctime(tm);
 
 		tstr[strlen(tstr)-1] = 0;
-		fprintf(fp, "Timestamp: %s %ld usec\n",
-			tstr, tv.tv_usec);
+		snprintf(ts, sizeof(ts), "%s %ld usec", tstr, tv.tv_usec);
+		if (fp == stdout)
+			print_string(PRINT_ANY, "timestamp",
+				     "Timestamp: %s\n", ts);
+		else
+			fprintf(fp, "Timestamp: %s\n", ts);
 	}
 
 	return 0;
-- 
2.51.0
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help