From: Stefano Brivio <sbrivio@redhat.com>
To: David Gibson <david@gibson.dropbear.id.au>
Cc: passt-dev@passt.top
Subject: Re: [PATCH v2 2/4] log: Correct formatting of timestamps
Date: Tue, 6 Aug 2024 21:08:07 +0200 [thread overview]
Message-ID: <20240806210807.2672b2f7@elisabeth> (raw)
In-Reply-To: <20240806061839.1950144-3-david@gibson.dropbear.id.au>
On Tue, 6 Aug 2024 16:18:37 +1000
David Gibson <david@gibson.dropbear.id.au> wrote:
> logtime_fmt_and_arg() is a rather odd macro, producing both a format
> string and an argument, which can only be used in quite specific printf()
> like formulations. It also has a significant bug: it tries to display 4
> digits after the decimal point (so down to tenths of milliseconds) using
> %04i. But the field width in printf() is always a *minimum* not maximum
> field width, so this will not truncate the given value, but will redisplay
> the entire tenth-of-milliseconds difference again after the decimal point.
Weird, not for me (glibc 2.38). But anyway, yes, it's much better this
way, and definitely, it's specified as minimum width (I never noticed).
Just one nit:
> Replace the macro with an snprintf() like function which will format the
> timestamp, and use an explicit % to correct the display.
>
> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
> ---
> log.c | 36 ++++++++++++++++++++++++------------
> 1 file changed, 24 insertions(+), 12 deletions(-)
>
> diff --git a/log.c b/log.c
> index eb3a780a..e60852f7 100644
> --- a/log.c
> +++ b/log.c
> @@ -46,14 +46,24 @@ int log_trace; /* --trace mode enabled */
> bool log_conf_parsed; /* Logging options already parsed */
> bool log_runtime; /* Daemonised, or ready in foreground */
>
> +#define LL_STRLEN (sizeof("-9223372036854775808"))
> +#define LOGTIME_STRLEN (LL_STRLEN + 5)
> +
> /**
> - * logtime_fmt_and_arg() - Build format and arguments to print relative log time
> - * @x: Current timestamp
> + * logtime_fmt() - Format timestamp into a string for the log
> + * @buf: Buffer into which to format the time
> + * @size: Size of @buf
> + * @ts: Time to format
> + *
> + * Return: number of characters written to @buf (excluding \0)
> */
> -#define logtime_fmt_and_arg(x) \
> - "%lli.%04lli", \
> - (timespec_diff_us((x), &log_start) / 1000000LL), \
> - (timespec_diff_us((x), &log_start) / 100LL)
> +int logtime_fmt(char *buf, size_t size, const struct timespec *ts)
Shouldn't this be static?
The rest of the series looks good to me, so I can also fix this up
on merge, as you prefer.
--
Stefano
next prev parent reply other threads:[~2024-08-06 19:08 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-06 6:18 [PATCH v2 0/4] Fix several small errors in log time handling David Gibson
2024-08-06 6:18 ` [PATCH v2 1/4] util: Some corrections for timespec_diff_us David Gibson
2024-08-06 6:18 ` [PATCH v2 2/4] log: Correct formatting of timestamps David Gibson
2024-08-06 19:08 ` Stefano Brivio [this message]
2024-08-07 1:06 ` David Gibson
2024-08-07 8:17 ` Stefano Brivio
2024-08-06 6:18 ` [PATCH v2 3/4] log: Handle errors from clock_gettime() David Gibson
2024-08-06 6:18 ` [PATCH v2 4/4] log: Avoid duplicate calls to logtime() David Gibson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240806210807.2672b2f7@elisabeth \
--to=sbrivio@redhat.com \
--cc=david@gibson.dropbear.id.au \
--cc=passt-dev@passt.top \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://passt.top/passt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).