diff mbox series

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

Message ID 20220420132624.2439741-2-marcandre.lureau@redhat.com
State New
Headers show
Series Misc cleanups | expand

Commit Message

Marc-André Lureau April 20, 2022, 1:25 p.m. UTC
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(-)

Comments

Daniel P. Berrangé April 20, 2022, 2:26 p.m. UTC | #1
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
diff mbox series

Patch

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