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) --- 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