On Wed, Jul 24, 2024 at 11:50:10PM +0200, Stefano Brivio wrote: > For some reason, in commit 01efc71ddd25 ("log, conf: Add support for > logging to file"), I added calculations for relative logging > timestamps using the difference for the seconds part only, not for > accounting for the fractional part. > > Fix that by storing the initial timestamp, log_start, as a timespec > struct, and by calculating differences of both seconds and nanoseconds > from the starting time. Do this in a macro as we need the same > calculation and format in a few places. > > Signed-off-by: Stefano Brivio > --- > log.c | 42 ++++++++++++++++++++++-------------------- > 1 file changed, 22 insertions(+), 20 deletions(-) > > diff --git a/log.c b/log.c > index 54483e7..f57a54f 100644 > --- a/log.c > +++ b/log.c > @@ -40,12 +40,21 @@ static size_t log_written; /* Currently used bytes in log file */ > static size_t log_cut_size; /* Bytes to cut at start on rotation */ > static char log_header[BUFSIZ]; /* File header, written back on cuts */ > > -static time_t log_start; /* Start timestamp */ > +static struct timespec log_start; /* Start timestamp */ > > int log_trace; /* --trace mode enabled */ > bool log_conf_parsed; /* Logging options already parsed */ > bool log_runtime; /* Daemonised, or ready in foreground */ > > +/** > + * logtime_fmt_and_arg() - Build format and arguments to print relative log time > + * @x: Current timestamp > + */ > +#define logtime_fmt_and_arg(x) \ > + "%lli.%04lli", \ > + ((long long int)(x)->tv_sec - log_start.tv_sec), \ > + (((long long int)(x)->tv_nsec - log_start.tv_nsec) / (100L * 1000)) This doesn't look right. If x->tv_nsec - log_start.tv_nsec is negative it will produce weird results. Instead you need more complex logic to carry a sufficient difference in the nsec over into the seconds difference. -- David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson