[PATCH 01/41] qga: use fixed-length for usecs formatting

marcandre.lureau@redhat.com posted 41 patches 3 years, 9 months ago
Maintainers: Richard Henderson <richard.henderson@linaro.org>, Paolo Bonzini <pbonzini@redhat.com>, Warner Losh <imp@bsdimp.com>, Kyle Evans <kevans@freebsd.org>, Stefan Hajnoczi <stefanha@redhat.com>, Beniamino Galvani <b.galvani@gmail.com>, Peter Maydell <peter.maydell@linaro.org>, Antony Pavlov <antonynpavlov@gmail.com>, Igor Mitsyanko <i.mitsyanko@gmail.com>, "Philippe Mathieu-Daudé" <f4bug@amsat.org>, Kevin Wolf <kwolf@redhat.com>, Hanna Reitz <hreitz@redhat.com>, Eduardo Habkost <eduardo@habkost.net>, Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, Yanan Wang <wangyanan55@huawei.com>, Markus Armbruster <armbru@redhat.com>, Laurent Vivier <laurent@vivier.eu>, Juan Quintela <quintela@redhat.com>, "Dr. David Alan Gilbert" <dgilbert@redhat.com>, Michael Roth <michael.roth@amd.com>, Eric Blake <eblake@redhat.com>, Vladimir Sementsov-Ogievskiy <v.sementsov-og@mail.ru>, Konstantin Kostiuk <kkostiuk@redhat.com>, "Daniel P. Berrangé" <berrange@redhat.com>, Fam Zheng <fam@euphon.net>, Taylor Simpson <tsimpson@quicinc.com>, "Edgar E. Iglesias" <edgar.iglesias@gmail.com>, Aurelien Jarno <aurelien@aurel32.net>, Jiaxun Yang <jiaxun.yang@flygoat.com>, Aleksandar Rikalo <aleksandar.rikalo@syrmia.com>, Chris Wulff <crwulff@gmail.com>, Marek Vasut <marex@denx.de>, Stafford Horne <shorne@gmail.com>, "Cédric Le Goater" <clg@kaod.org>, Daniel Henrique Barboza <danielhb413@gmail.com>, David Gibson <david@gibson.dropbear.id.au>, Greg Kurz <groug@kaod.org>, Palmer Dabbelt <palmer@dabbelt.com>, Alistair Francis <alistair.francis@wdc.com>, Bin Meng <bin.meng@windriver.com>, Yoshinori Sato <ysato@users.sourceforge.jp>, David Hildenbrand <david@redhat.com>, Cornelia Huck <cohuck@redhat.com>, Thomas Huth <thuth@redhat.com>, Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>, Artyom Tarasenko <atar4qemu@gmail.com>, Bastian Koppelmann <kbastian@mail.uni-paderborn.de>, Max Filippov <jcmvbkbc@gmail.com>, "Alex Bennée" <alex.bennee@linaro.org>, John Snow <jsnow@redhat.com>, Alexander Bulekov <alxndr@bu.edu>, Bandan Das <bsd@redhat.com>, Darren Kenny <darren.kenny@oracle.com>, Qiuhao Li <Qiuhao.Li@outlook.com>, Akihiko Odaki <akihiko.odaki@gmail.com>, Gerd Hoffmann <kraxel@redhat.com>, Stefan Weil <sw@weilnetz.de>
There is a newer version of this series
[PATCH 01/41] qga: use fixed-length for usecs formatting
Posted by marcandre.lureau@redhat.com 3 years, 9 months ago
From: Marc-André Lureau <marcandre.lureau@redhat.com>

The old code is kind of wrong. Say it's 1649309843.000001 seconds past
the epoch. Prints "1649309843.1". 9us later, it prints "1649309843.10".
Should really use %06lu for the microseconds part.

Use int64_t/PRId64 instead of old GLib-style.

Suggested-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 qga/main.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/qga/main.c b/qga/main.c
index 1deb0ee2fbfe..ac63d8e47802 100644
--- a/qga/main.c
+++ b/qga/main.c
@@ -328,11 +328,10 @@ static void ga_log(const gchar *domain, GLogLevelFlags level,
 #else
     if (level & s->log_level) {
 #endif
-        gint64 t = g_get_real_time();
+        int64_t t = g_get_real_time();
         fprintf(s->log_file,
-                "%" G_GINT64_FORMAT ".%" G_GINT64_FORMAT
-                ": %s: %s\n", t / G_USEC_PER_SEC, t % G_USEC_PER_SEC,
-                level_str, msg);
+                "%" PRId64 ".%06" PRId64 ": %s: %s\n",
+                t / G_USEC_PER_SEC, t % G_USEC_PER_SEC, level_str, msg);
         fflush(s->log_file);
     }
 }
-- 
2.35.1.693.g805e0a68082a


Re: [PATCH 01/41] qga: use fixed-length for usecs formatting
Posted by Daniel P. Berrangé 3 years, 9 months ago
On Wed, Apr 20, 2022 at 05:25:44PM +0400, marcandre.lureau@redhat.com wrote:
> From: Marc-André Lureau <marcandre.lureau@redhat.com>
> 
> The old code is kind of wrong. Say it's 1649309843.000001 seconds past
> the epoch. Prints "1649309843.1". 9us later, it prints "1649309843.10".
> Should really use %06lu for the microseconds part.
> 
> Use int64_t/PRId64 instead of old GLib-style.
> 
> Suggested-by: Markus Armbruster <armbru@redhat.com>
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> ---
>  qga/main.c | 7 +++----
>  1 file changed, 3 insertions(+), 4 deletions(-)
> 
> diff --git a/qga/main.c b/qga/main.c
> index 1deb0ee2fbfe..ac63d8e47802 100644
> --- a/qga/main.c
> +++ b/qga/main.c
> @@ -328,11 +328,10 @@ static void ga_log(const gchar *domain, GLogLevelFlags level,
>  #else
>      if (level & s->log_level) {
>  #endif
> -        gint64 t = g_get_real_time();
> +        int64_t t = g_get_real_time();
>          fprintf(s->log_file,
> -                "%" G_GINT64_FORMAT ".%" G_GINT64_FORMAT
> -                ": %s: %s\n", t / G_USEC_PER_SEC, t % G_USEC_PER_SEC,
> -                level_str, msg);
> +                "%" PRId64 ".%06" PRId64 ": %s: %s\n",
> +                t / G_USEC_PER_SEC, t % G_USEC_PER_SEC, level_str, msg);
>          fflush(s->log_file);

IMHO better to get rid of the manual date formatting entirely and
use GDateTime :

  g_autoptr(GDateTime) now = g_date_time_now_utc();
  g_autofree char *nowstr = g_date_time_format("%s.%f", now)
  fprintf(s->log_file, "%s: %s: %s\n", nowstr, level_str, msg);
  fflush(s->log_file);


With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|