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
On Fri, 20 Feb 2026 19:54:02 +0100 "Matthieu Baerts (NGI0)" <matttbe@kernel.org> wrote: > 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> I would go farther, fp is always stdout. Drop the argument to the function.
Hi Stephen, On 21/02/2026 06:39, Stephen Hemminger wrote: > On Fri, 20 Feb 2026 19:54:02 +0100 > "Matthieu Baerts (NGI0)" <matttbe@kernel.org> wrote: > >> 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> > > I would go farther, fp is always stdout. Drop the argument to the function. Good idea! To be coherent with the rest, I started to look at removing the same argument from the caller functions, and similar ones. I did a very quick draft with a few sed, etc. but I'm not sure whether I should take this direction: https://github.com/matttbe/iproute2/commit/585b0109 WDYT? Or should I only drop fp from print_timestamp()? Or simply not use it like it is done in many other helpers supporting JSON? Cheers, Matt -- Sponsored by the NGI0 Core fund.
Hi Stephen, On 23/02/2026 14:20, Matthieu Baerts wrote: > Hi Stephen, > > On 21/02/2026 06:39, Stephen Hemminger wrote: >> On Fri, 20 Feb 2026 19:54:02 +0100 >> "Matthieu Baerts (NGI0)" <matttbe@kernel.org> wrote: >> >>> 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> >> >> I would go farther, fp is always stdout. Drop the argument to the function. > > Good idea! > > To be coherent with the rest, I started to look at removing the same > argument from the caller functions, and similar ones. I did a very quick > draft with a few sed, etc. but I'm not sure whether I should take this > direction: > > https://github.com/matttbe/iproute2/commit/585b0109 > > WDYT? Or should I only drop fp from print_timestamp()? Or simply not use > it like it is done in many other helpers supporting JSON? I just sent a v2 with the minimal modifications around print_timestamp: the argument is now simply ignored like it is the case in other helpers supporting JSON. If there is a need to continue the cleanup, probably better to do that in a different series I guess. Cheers, Matt -- Sponsored by the NGI0 Core fund.
© 2016 - 2026 Red Hat, Inc.