From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 657765A004E for ; Wed, 07 Aug 2024 03:16:20 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1722993377; bh=gsPiSlx64FKNf1/CRaoPlI/Kc9Iwll++5pty5JamGV4=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=SgFPnijzIepY+eWb7Cf7VnJD7KGI5Kcw+nTgaEnVLohveDAHzXmGKjMqmsCRA6rRM Zs5wsu7z4jqMzloLWwIceu2MTEUnbD4eNufCSXCocwPd1vHQMFNHs/ntt4JVAdY3e+ A1rHQGxxMVIC0QQuQUDyJsSTE3Vazq2dJ33JLsyd8hyHSkoTFahVlrzJPejLMtVaAk wEaqBxEsu+sC4bCItdn5fN0LjH8oU6J0Y8fliPWEHRsp+b5lx/74518l7sI8ONMQAc ljzAM5YZb8wQ+ey1hcqho1Qd5+iHey5OlEfl+cNhvp3hOxCDmqNDTKmaRxfg+LYnOL mDm/+X4UXWQaQ== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4Wdsh15Nc9z4wd6; Wed, 7 Aug 2024 11:16:17 +1000 (AEST) Date: Wed, 7 Aug 2024 11:06:52 +1000 From: David Gibson To: Stefano Brivio Subject: Re: [PATCH v2 2/4] log: Correct formatting of timestamps Message-ID: References: <20240806061839.1950144-1-david@gibson.dropbear.id.au> <20240806061839.1950144-3-david@gibson.dropbear.id.au> <20240806210807.2672b2f7@elisabeth> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="E4RJwzeVnYFF9vwB" Content-Disposition: inline In-Reply-To: <20240806210807.2672b2f7@elisabeth> Message-ID-Hash: USEJMVME23IXZHDOWRQAEZ535XQJ4IYS X-Message-ID-Hash: USEJMVME23IXZHDOWRQAEZ535XQJ4IYS X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: passt-dev@passt.top X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: --E4RJwzeVnYFF9vwB Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Tue, Aug 06, 2024 at 09:08:07PM +0200, Stefano Brivio wrote: > On Tue, 6 Aug 2024 16:18:37 +1000 > David Gibson wrote: >=20 > > 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 redisp= lay > > the entire tenth-of-milliseconds difference again after the decimal poi= nt. >=20 > 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). >=20 > Just one nit: >=20 > > Replace the macro with an snprintf() like function which will format the > > timestamp, and use an explicit % to correct the display. > >=20 > > Signed-off-by: David Gibson > > --- > > log.c | 36 ++++++++++++++++++++++++------------ > > 1 file changed, 24 insertions(+), 12 deletions(-) > >=20 > > 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 */ > > =20 > > +#define LL_STRLEN (sizeof("-9223372036854775808")) > > +#define LOGTIME_STRLEN (LL_STRLEN + 5) > > + > > /** > > - * logtime_fmt_and_arg() - Build format and arguments to print relativ= e 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) >=20 > Shouldn't this be static? Yes, yes it should. > The rest of the series looks good to me, so I can also fix this up > on merge, as you prefer. If you can fix on merge that would be great. --=20 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 --E4RJwzeVnYFF9vwB Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmayyKgACgkQzQJF27ox 2Gdj9w//U1ivhanCECbiJgpGxbcOwecbeXA3yABrat8r76ZMmHyrJj34j/TEIFoU gUI7JE/1vKY6lpVwdEqZAvYzBTpqdFy+tYFtj5AbbNnVvaJl24SZCe9Npbp6Q2SK GsMx3LF4GVfiLpjpnBbdF1pQDQ8YqLNuLNU/aqlx4EPs+RXGLVXa30/+VKJFgsHs jkzuOO0QSPGcxC3nQbsUNAEBq6iMtr+Fs9hUnmdJ/+TjGs9LEWzeXbxFvCALYeVm qpm6DmQJSysxYJnuJZ38S4xZvUWeuDWVge602fW0Jc4ZlSqDEQOfjBWz72rHWNq1 iAhhZdOgbaVBknrLlyBKEC5PGTcL+ayHhh7r5BRUHHFgDGKqMyi7/sdKbrHM/gsD KOgEna6HzTlI38Kp2Z+lnmSzwSc/PUkiiXJRf1gHlWjPfZaBNLHGov+dtmFifrjw KsjhtSaSQ6CTJmxXPuxlRIBatnWFTPpn+3ux81RyccnNJa7dHndS9Go3k+Tx1UBe dFCrVPrW/FXewdJUtQPysBkXwcirJb+hNfQKpTA96t8RrN+YqQF/zFSgCnD2WvNu FBF337Y2ruOpoV5+uG1Btsah6anzPjoWAEdb95DyV3lDXIe3LqlSruJ9G5aDZ8gL dLkNJjFoF6mgu2dc9uPQTlQDcCoakG3+MqS1Dkp726IBNPxKalI= =5nNr -----END PGP SIGNATURE----- --E4RJwzeVnYFF9vwB--