qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/7] replace all use of strftime() with g_date_time_format()
@ 2021-05-05 10:36 Daniel P. Berrangé
  2021-05-05 10:36 ` [PATCH 1/7] migration: use GDateTime for formatting timestamp in snapshot names Daniel P. Berrangé
                   ` (6 more replies)
  0 siblings, 7 replies; 21+ messages in thread
From: Daniel P. Berrangé @ 2021-05-05 10:36 UTC (permalink / raw)
  To: qemu-devel
  Cc: Kevin Wolf, Jiri Pirko, qemu-block, Juan Quintela,
	Markus Armbruster, Jason Wang, Laurent Vivier, Max Reitz,
	virtio-fs, Gerd Hoffmann, Stefan Hajnoczi,
	Daniel P. Berrangé,
	Dr. David Alan Gilbert, Brad Smith

The GDateTime APIs provided by GLib avoid portability pitfalls, such
as some platforms where 'struct timeval.tv_sec' field is still 'long'
instead of 'time_t'. When combined with automatic cleanup, GDateTime
often results in simpler code too.

Daniel P. Berrangé (7):
  migration: use GDateTime for formatting timestamp in snapshot names
  block: use GDateTime for formatting timestamp when dumping snapshot
    info
  net/rocker: use GDateTime for formatting timestamp in debug messages
  usb/dev-mtp: use GDateTime for formatting timestamp for objects
  io: use GDateTime for formatting timestamp for websock headers
  linux-user: use GDateTime for formatting timestamp for core file
  virtiofsd: use GDateTime for formatting timestamp for debug messages

 block/qapi.c                     | 11 ++++------
 hw/net/rocker/rocker.h           | 11 +++-------
 hw/usb/dev-mtp.c                 |  9 +++-----
 io/channel-websock.c             | 10 ++-------
 linux-user/elfload.c             | 36 ++++++++------------------------
 migration/savevm.c               | 13 +++++-------
 tools/virtiofsd/passthrough_ll.c | 25 ++++------------------
 7 files changed, 30 insertions(+), 85 deletions(-)

-- 
2.31.1




^ permalink raw reply	[flat|nested] 21+ messages in thread

* [PATCH 1/7] migration: use GDateTime for formatting timestamp in snapshot names
  2021-05-05 10:36 [PATCH 0/7] replace all use of strftime() with g_date_time_format() Daniel P. Berrangé
@ 2021-05-05 10:36 ` Daniel P. Berrangé
  2021-05-05 14:42   ` Dr. David Alan Gilbert
  2021-05-06  3:14   ` Brad Smith
  2021-05-05 10:36 ` [PATCH 2/7] block: use GDateTime for formatting timestamp when dumping snapshot info Daniel P. Berrangé
                   ` (5 subsequent siblings)
  6 siblings, 2 replies; 21+ messages in thread
From: Daniel P. Berrangé @ 2021-05-05 10:36 UTC (permalink / raw)
  To: qemu-devel
  Cc: Kevin Wolf, Jiri Pirko, qemu-block, Juan Quintela,
	Markus Armbruster, Jason Wang, Laurent Vivier, Max Reitz,
	virtio-fs, Gerd Hoffmann, Stefan Hajnoczi,
	Daniel P. Berrangé,
	Dr. David Alan Gilbert, Brad Smith

The GDateTime APIs provided by GLib avoid portability pitfalls, such
as some platforms where 'struct timeval.tv_sec' field is still 'long'
instead of 'time_t'. When combined with automatic cleanup, GDateTime
often results in simpler code too.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 migration/savevm.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/migration/savevm.c b/migration/savevm.c
index 52e2d72e4b..72848b946c 100644
--- a/migration/savevm.c
+++ b/migration/savevm.c
@@ -2775,8 +2775,7 @@ bool save_snapshot(const char *name, bool overwrite, const char *vmstate,
     QEMUFile *f;
     int saved_vm_running;
     uint64_t vm_state_size;
-    qemu_timeval tv;
-    struct tm tm;
+    g_autoptr(GDateTime) now = g_date_time_new_now_local();
     AioContext *aio_context;
 
     if (migration_is_blocked(errp)) {
@@ -2836,9 +2835,8 @@ bool save_snapshot(const char *name, bool overwrite, const char *vmstate,
     memset(sn, 0, sizeof(*sn));
 
     /* fill auxiliary fields */
-    qemu_gettimeofday(&tv);
-    sn->date_sec = tv.tv_sec;
-    sn->date_nsec = tv.tv_usec * 1000;
+    sn->date_sec = g_date_time_to_unix(now);
+    sn->date_nsec = g_date_time_get_microsecond(now) * 1000;
     sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
     if (replay_mode != REPLAY_MODE_NONE) {
         sn->icount = replay_get_current_icount();
@@ -2849,9 +2847,8 @@ bool save_snapshot(const char *name, bool overwrite, const char *vmstate,
     if (name) {
         pstrcpy(sn->name, sizeof(sn->name), name);
     } else {
-        /* cast below needed for OpenBSD where tv_sec is still 'long' */
-        localtime_r((const time_t *)&tv.tv_sec, &tm);
-        strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
+        g_autofree char *autoname = g_date_time_format(now,  "vm-%Y%m%d%H%M%S");
+        pstrcpy(sn->name, sizeof(sn->name), autoname);
     }
 
     /* save the VM state */
-- 
2.31.1



^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH 2/7] block: use GDateTime for formatting timestamp when dumping snapshot info
  2021-05-05 10:36 [PATCH 0/7] replace all use of strftime() with g_date_time_format() Daniel P. Berrangé
  2021-05-05 10:36 ` [PATCH 1/7] migration: use GDateTime for formatting timestamp in snapshot names Daniel P. Berrangé
@ 2021-05-05 10:36 ` Daniel P. Berrangé
  2021-05-07 17:05   ` Max Reitz
  2021-05-05 10:36 ` [PATCH 3/7] net/rocker: use GDateTime for formatting timestamp in debug messages Daniel P. Berrangé
                   ` (4 subsequent siblings)
  6 siblings, 1 reply; 21+ messages in thread
From: Daniel P. Berrangé @ 2021-05-05 10:36 UTC (permalink / raw)
  To: qemu-devel
  Cc: Kevin Wolf, Jiri Pirko, qemu-block, Juan Quintela,
	Markus Armbruster, Jason Wang, Laurent Vivier, Max Reitz,
	virtio-fs, Gerd Hoffmann, Stefan Hajnoczi,
	Daniel P. Berrangé,
	Dr. David Alan Gilbert, Brad Smith

The GDateTime APIs provided by GLib avoid portability pitfalls, such
as some platforms where 'struct timeval.tv_sec' field is still 'long'
instead of 'time_t'. When combined with automatic cleanup, GDateTime
often results in simpler code too.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 block/qapi.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/block/qapi.c b/block/qapi.c
index 943e7b15ad..06851fb469 100644
--- a/block/qapi.c
+++ b/block/qapi.c
@@ -663,10 +663,8 @@ BlockStatsList *qmp_query_blockstats(bool has_query_nodes,
 
 void bdrv_snapshot_dump(QEMUSnapshotInfo *sn)
 {
-    char date_buf[128], clock_buf[128];
+    char clock_buf[128];
     char icount_buf[128] = {0};
-    struct tm tm;
-    time_t ti;
     int64_t secs;
     char *sizing = NULL;
 
@@ -674,10 +672,9 @@ void bdrv_snapshot_dump(QEMUSnapshotInfo *sn)
         qemu_printf("%-10s%-17s%8s%20s%13s%11s",
                     "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK", "ICOUNT");
     } else {
-        ti = sn->date_sec;
-        localtime_r(&ti, &tm);
-        strftime(date_buf, sizeof(date_buf),
-                 "%Y-%m-%d %H:%M:%S", &tm);
+        g_autoptr(GDateTime) date = g_date_time_new_from_unix_local(sn->date_sec);
+        g_autofree char *date_buf = g_date_time_format(date, "%Y-%m-%d %H:%M:%S");
+
         secs = sn->vm_clock_nsec / 1000000000;
         snprintf(clock_buf, sizeof(clock_buf),
                  "%02d:%02d:%02d.%03d",
-- 
2.31.1



^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH 3/7] net/rocker: use GDateTime for formatting timestamp in debug messages
  2021-05-05 10:36 [PATCH 0/7] replace all use of strftime() with g_date_time_format() Daniel P. Berrangé
  2021-05-05 10:36 ` [PATCH 1/7] migration: use GDateTime for formatting timestamp in snapshot names Daniel P. Berrangé
  2021-05-05 10:36 ` [PATCH 2/7] block: use GDateTime for formatting timestamp when dumping snapshot info Daniel P. Berrangé
@ 2021-05-05 10:36 ` Daniel P. Berrangé
  2021-06-11 16:28   ` Daniel P. Berrangé
  2021-05-05 10:36 ` [PATCH 4/7] usb/dev-mtp: use GDateTime for formatting timestamp for objects Daniel P. Berrangé
                   ` (3 subsequent siblings)
  6 siblings, 1 reply; 21+ messages in thread
From: Daniel P. Berrangé @ 2021-05-05 10:36 UTC (permalink / raw)
  To: qemu-devel
  Cc: Kevin Wolf, Jiri Pirko, qemu-block, Juan Quintela,
	Markus Armbruster, Jason Wang, Laurent Vivier, Max Reitz,
	virtio-fs, Gerd Hoffmann, Stefan Hajnoczi,
	Daniel P. Berrangé,
	Dr. David Alan Gilbert, Brad Smith

The GDateTime APIs provided by GLib avoid portability pitfalls, such
as some platforms where 'struct timeval.tv_sec' field is still 'long'
instead of 'time_t'. When combined with automatic cleanup, GDateTime
often results in simpler code too.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 hw/net/rocker/rocker.h | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)

diff --git a/hw/net/rocker/rocker.h b/hw/net/rocker/rocker.h
index 941c932265..412fa44d01 100644
--- a/hw/net/rocker/rocker.h
+++ b/hw/net/rocker/rocker.h
@@ -25,14 +25,9 @@
 #if defined(DEBUG_ROCKER)
 #  define DPRINTF(fmt, ...) \
     do {                                                           \
-        struct timeval tv;                                         \
-        char timestr[64];                                          \
-        time_t now;                                                \
-        gettimeofday(&tv, NULL);                                   \
-        now = tv.tv_sec;                                           \
-        strftime(timestr, sizeof(timestr), "%T", localtime(&now)); \
-        fprintf(stderr, "%s.%06ld ", timestr, tv.tv_usec);         \
-        fprintf(stderr, "ROCKER: " fmt, ## __VA_ARGS__);           \
+        g_autoptr(GDateTime) now = g_date_time_new_now_local();    \
+        g_autofree char *nowstr = g_date_time_format(now, "%T.%f");\
+        fprintf(stderr, "%s ROCKER: " fmt, nowstr, ## __VA_ARGS__);\
     } while (0)
 #else
 static inline GCC_FMT_ATTR(1, 2) int DPRINTF(const char *fmt, ...)
-- 
2.31.1



^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH 4/7] usb/dev-mtp: use GDateTime for formatting timestamp for objects
  2021-05-05 10:36 [PATCH 0/7] replace all use of strftime() with g_date_time_format() Daniel P. Berrangé
                   ` (2 preceding siblings ...)
  2021-05-05 10:36 ` [PATCH 3/7] net/rocker: use GDateTime for formatting timestamp in debug messages Daniel P. Berrangé
@ 2021-05-05 10:36 ` Daniel P. Berrangé
  2021-05-05 13:08   ` Gerd Hoffmann
  2021-05-05 10:37 ` [PATCH 5/7] io: use GDateTime for formatting timestamp for websock headers Daniel P. Berrangé
                   ` (2 subsequent siblings)
  6 siblings, 1 reply; 21+ messages in thread
From: Daniel P. Berrangé @ 2021-05-05 10:36 UTC (permalink / raw)
  To: qemu-devel
  Cc: Kevin Wolf, Jiri Pirko, qemu-block, Juan Quintela,
	Markus Armbruster, Jason Wang, Laurent Vivier, Max Reitz,
	virtio-fs, Gerd Hoffmann, Stefan Hajnoczi,
	Daniel P. Berrangé,
	Dr. David Alan Gilbert, Brad Smith

The GDateTime APIs provided by GLib avoid portability pitfalls, such
as some platforms where 'struct timeval.tv_sec' field is still 'long'
instead of 'time_t'. When combined with automatic cleanup, GDateTime
often results in simpler code too.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 hw/usb/dev-mtp.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/hw/usb/dev-mtp.c b/hw/usb/dev-mtp.c
index bbb8274344..a71fcc326d 100644
--- a/hw/usb/dev-mtp.c
+++ b/hw/usb/dev-mtp.c
@@ -772,12 +772,9 @@ static void usb_mtp_add_str(MTPData *data, const char *str)
 
 static void usb_mtp_add_time(MTPData *data, time_t time)
 {
-    char buf[16];
-    struct tm tm;
-
-    gmtime_r(&time, &tm);
-    strftime(buf, sizeof(buf), "%Y%m%dT%H%M%S", &tm);
-    usb_mtp_add_str(data, buf);
+    g_autoptr(GDateTime) then = g_date_time_new_from_unix_utc(time);
+    g_autofree char *thenstr = g_date_time_format(then, "%Y%m%dT%H%M%S");
+    usb_mtp_add_str(data, thenstr);
 }
 
 /* ----------------------------------------------------------------------- */
-- 
2.31.1



^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH 5/7] io: use GDateTime for formatting timestamp for websock headers
  2021-05-05 10:36 [PATCH 0/7] replace all use of strftime() with g_date_time_format() Daniel P. Berrangé
                   ` (3 preceding siblings ...)
  2021-05-05 10:36 ` [PATCH 4/7] usb/dev-mtp: use GDateTime for formatting timestamp for objects Daniel P. Berrangé
@ 2021-05-05 10:37 ` Daniel P. Berrangé
  2021-06-11 16:29   ` Daniel P. Berrangé
  2021-05-05 10:37 ` [PATCH 6/7] linux-user: use GDateTime for formatting timestamp for core file Daniel P. Berrangé
  2021-05-05 10:37 ` [PATCH 7/7] virtiofsd: use GDateTime for formatting timestamp for debug messages Daniel P. Berrangé
  6 siblings, 1 reply; 21+ messages in thread
From: Daniel P. Berrangé @ 2021-05-05 10:37 UTC (permalink / raw)
  To: qemu-devel
  Cc: Kevin Wolf, Jiri Pirko, qemu-block, Juan Quintela,
	Markus Armbruster, Jason Wang, Laurent Vivier, Max Reitz,
	virtio-fs, Gerd Hoffmann, Stefan Hajnoczi,
	Daniel P. Berrangé,
	Dr. David Alan Gilbert, Brad Smith

The GDateTime APIs provided by GLib avoid portability pitfalls, such
as some platforms where 'struct timeval.tv_sec' field is still 'long'
instead of 'time_t'. When combined with automatic cleanup, GDateTime
often results in simpler code too.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 io/channel-websock.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)

diff --git a/io/channel-websock.c b/io/channel-websock.c
index 03c1f7cb62..70889bb54d 100644
--- a/io/channel-websock.c
+++ b/io/channel-websock.c
@@ -177,15 +177,9 @@ qio_channel_websock_handshake_send_res(QIOChannelWebsock *ioc,
 
 static gchar *qio_channel_websock_date_str(void)
 {
-    struct tm tm;
-    time_t now = time(NULL);
-    char datebuf[128];
+    g_autoptr(GDateTime) now = g_date_time_new_now_utc();
 
-    gmtime_r(&now, &tm);
-
-    strftime(datebuf, sizeof(datebuf), "%a, %d %b %Y %H:%M:%S GMT", &tm);
-
-    return g_strdup(datebuf);
+    return g_date_time_format(now, "%a, %d %b %Y %H:%M:%S GMT");
 }
 
 static void qio_channel_websock_handshake_send_res_err(QIOChannelWebsock *ioc,
-- 
2.31.1



^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH 6/7] linux-user: use GDateTime for formatting timestamp for core file
  2021-05-05 10:36 [PATCH 0/7] replace all use of strftime() with g_date_time_format() Daniel P. Berrangé
                   ` (4 preceding siblings ...)
  2021-05-05 10:37 ` [PATCH 5/7] io: use GDateTime for formatting timestamp for websock headers Daniel P. Berrangé
@ 2021-05-05 10:37 ` Daniel P. Berrangé
  2021-05-05 11:18   ` Laurent Vivier
  2021-05-15 19:51   ` Laurent Vivier
  2021-05-05 10:37 ` [PATCH 7/7] virtiofsd: use GDateTime for formatting timestamp for debug messages Daniel P. Berrangé
  6 siblings, 2 replies; 21+ messages in thread
From: Daniel P. Berrangé @ 2021-05-05 10:37 UTC (permalink / raw)
  To: qemu-devel
  Cc: Kevin Wolf, Jiri Pirko, qemu-block, Juan Quintela,
	Markus Armbruster, Jason Wang, Laurent Vivier, Max Reitz,
	virtio-fs, Gerd Hoffmann, Stefan Hajnoczi,
	Daniel P. Berrangé,
	Dr. David Alan Gilbert, Brad Smith

The GDateTime APIs provided by GLib avoid portability pitfalls, such
as some platforms where 'struct timeval.tv_sec' field is still 'long'
instead of 'time_t'. When combined with automatic cleanup, GDateTime
often results in simpler code too.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 linux-user/elfload.c | 36 +++++++++---------------------------
 1 file changed, 9 insertions(+), 27 deletions(-)

diff --git a/linux-user/elfload.c b/linux-user/elfload.c
index c6731013fd..c38b7b4d37 100644
--- a/linux-user/elfload.c
+++ b/linux-user/elfload.c
@@ -3386,7 +3386,6 @@ static size_t note_size(const struct memelfnote *);
 static void free_note_info(struct elf_note_info *);
 static int fill_note_info(struct elf_note_info *, long, const CPUArchState *);
 static void fill_thread_info(struct elf_note_info *, const CPUArchState *);
-static int core_dump_filename(const TaskState *, char *, size_t);
 
 static int dump_write(int, const void *, size_t);
 static int write_note(struct memelfnote *, int);
@@ -3685,32 +3684,16 @@ static void fill_auxv_note(struct memelfnote *note, const TaskState *ts)
  * for the name:
  *     qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
  *
- * Returns 0 in case of success, -1 otherwise (errno is set).
+ * Returns the filename
  */
-static int core_dump_filename(const TaskState *ts, char *buf,
-                              size_t bufsize)
+static char *core_dump_filename(const TaskState *ts)
 {
-    char timestamp[64];
-    char *base_filename = NULL;
-    struct timeval tv;
-    struct tm tm;
+    g_autoptr(GDateTime) now = g_date_time_new_now_local();
+    g_autofree char *nowstr = g_date_time_format(now, "%Y%m%d-%H%M%S");
+    g_autofree char *base_filename = g_path_get_basename(ts->bprm->filename);
 
-    assert(bufsize >= PATH_MAX);
-
-    if (gettimeofday(&tv, NULL) < 0) {
-        (void) fprintf(stderr, "unable to get current timestamp: %s",
-                       strerror(errno));
-        return (-1);
-    }
-
-    base_filename = g_path_get_basename(ts->bprm->filename);
-    (void) strftime(timestamp, sizeof (timestamp), "%Y%m%d-%H%M%S",
-                    localtime_r(&tv.tv_sec, &tm));
-    (void) snprintf(buf, bufsize, "qemu_%s_%s_%d.core",
-                    base_filename, timestamp, (int)getpid());
-    g_free(base_filename);
-
-    return (0);
+    return g_strdup_printf("qemu_%s_%s_%d.core",
+                           base_filename, nowstr, (int)getpid());
 }
 
 static int dump_write(int fd, const void *ptr, size_t size)
@@ -3938,7 +3921,7 @@ static int elf_core_dump(int signr, const CPUArchState *env)
     const CPUState *cpu = env_cpu((CPUArchState *)env);
     const TaskState *ts = (const TaskState *)cpu->opaque;
     struct vm_area_struct *vma = NULL;
-    char corefile[PATH_MAX];
+    g_autofree char *corefile = NULL;
     struct elf_note_info info;
     struct elfhdr elf;
     struct elf_phdr phdr;
@@ -3955,8 +3938,7 @@ static int elf_core_dump(int signr, const CPUArchState *env)
     if (dumpsize.rlim_cur == 0)
         return 0;
 
-    if (core_dump_filename(ts, corefile, sizeof (corefile)) < 0)
-        return (-errno);
+    corefile = core_dump_filename(ts);
 
     if ((fd = open(corefile, O_WRONLY | O_CREAT,
                    S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
-- 
2.31.1



^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH 7/7] virtiofsd: use GDateTime for formatting timestamp for debug messages
  2021-05-05 10:36 [PATCH 0/7] replace all use of strftime() with g_date_time_format() Daniel P. Berrangé
                   ` (5 preceding siblings ...)
  2021-05-05 10:37 ` [PATCH 6/7] linux-user: use GDateTime for formatting timestamp for core file Daniel P. Berrangé
@ 2021-05-05 10:37 ` Daniel P. Berrangé
  2021-05-05 14:51   ` Dr. David Alan Gilbert
  6 siblings, 1 reply; 21+ messages in thread
From: Daniel P. Berrangé @ 2021-05-05 10:37 UTC (permalink / raw)
  To: qemu-devel
  Cc: Kevin Wolf, Jiri Pirko, qemu-block, Juan Quintela,
	Markus Armbruster, Jason Wang, Laurent Vivier, Max Reitz,
	virtio-fs, Gerd Hoffmann, Stefan Hajnoczi,
	Daniel P. Berrangé,
	Dr. David Alan Gilbert, Brad Smith

The GDateTime APIs provided by GLib avoid portability pitfalls, such
as some platforms where 'struct timeval.tv_sec' field is still 'long'
instead of 'time_t'. When combined with automatic cleanup, GDateTime
often results in simpler code too.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 tools/virtiofsd/passthrough_ll.c | 25 ++++---------------------
 1 file changed, 4 insertions(+), 21 deletions(-)

diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
index 1553d2ef45..cdd224918c 100644
--- a/tools/virtiofsd/passthrough_ll.c
+++ b/tools/virtiofsd/passthrough_ll.c
@@ -3558,10 +3558,6 @@ static void setup_nofile_rlimit(unsigned long rlimit_nofile)
 static void log_func(enum fuse_log_level level, const char *fmt, va_list ap)
 {
     g_autofree char *localfmt = NULL;
-    struct timespec ts;
-    struct tm tm;
-    char sec_fmt[sizeof "2020-12-07 18:17:54"];
-    char zone_fmt[sizeof "+0100"];
 
     if (current_log_level < level) {
         return;
@@ -3573,23 +3569,10 @@ static void log_func(enum fuse_log_level level, const char *fmt, va_list ap)
             localfmt = g_strdup_printf("[ID: %08ld] %s", syscall(__NR_gettid),
                                        fmt);
         } else {
-            /* try formatting a broken-down timestamp */
-            if (clock_gettime(CLOCK_REALTIME, &ts) != -1 &&
-                localtime_r(&ts.tv_sec, &tm) != NULL &&
-                strftime(sec_fmt, sizeof sec_fmt, "%Y-%m-%d %H:%M:%S",
-                         &tm) != 0 &&
-                strftime(zone_fmt, sizeof zone_fmt, "%z", &tm) != 0) {
-                localfmt = g_strdup_printf("[%s.%02ld%s] [ID: %08ld] %s",
-                                           sec_fmt,
-                                           ts.tv_nsec / (10L * 1000 * 1000),
-                                           zone_fmt, syscall(__NR_gettid),
-                                           fmt);
-            } else {
-                /* fall back to a flat timestamp */
-                localfmt = g_strdup_printf("[%" PRId64 "] [ID: %08ld] %s",
-                                           get_clock(), syscall(__NR_gettid),
-                                           fmt);
-            }
+            g_autoptr(GDateTime) now = g_date_time_new_now_local();
+            g_autofree char *nowstr = g_date_time_format(now, "%Y-%m-%d %H:%M:%S.%f%z");
+            localfmt = g_strdup_printf("[%s] [ID: %08ld] %s",
+                                       nowstr, syscall(__NR_gettid), fmt);
         }
         fmt = localfmt;
     }
-- 
2.31.1



^ permalink raw reply related	[flat|nested] 21+ messages in thread

* Re: [PATCH 6/7] linux-user: use GDateTime for formatting timestamp for core file
  2021-05-05 10:37 ` [PATCH 6/7] linux-user: use GDateTime for formatting timestamp for core file Daniel P. Berrangé
@ 2021-05-05 11:18   ` Laurent Vivier
  2021-05-15 19:51   ` Laurent Vivier
  1 sibling, 0 replies; 21+ messages in thread
From: Laurent Vivier @ 2021-05-05 11:18 UTC (permalink / raw)
  To: Daniel P. Berrangé, qemu-devel
  Cc: Kevin Wolf, Jiri Pirko, qemu-block, Juan Quintela, Jason Wang,
	Markus Armbruster, Max Reitz, virtio-fs, Gerd Hoffmann,
	Stefan Hajnoczi, Dr. David Alan Gilbert, Brad Smith

Le 05/05/2021 à 12:37, Daniel P. Berrangé a écrit :
> The GDateTime APIs provided by GLib avoid portability pitfalls, such
> as some platforms where 'struct timeval.tv_sec' field is still 'long'
> instead of 'time_t'. When combined with automatic cleanup, GDateTime
> often results in simpler code too.
> 
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>  linux-user/elfload.c | 36 +++++++++---------------------------
>  1 file changed, 9 insertions(+), 27 deletions(-)
> 

Reviewed-by: Laurent Vivier <laurent@vivier.eu>



^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH 4/7] usb/dev-mtp: use GDateTime for formatting timestamp for objects
  2021-05-05 10:36 ` [PATCH 4/7] usb/dev-mtp: use GDateTime for formatting timestamp for objects Daniel P. Berrangé
@ 2021-05-05 13:08   ` Gerd Hoffmann
  0 siblings, 0 replies; 21+ messages in thread
From: Gerd Hoffmann @ 2021-05-05 13:08 UTC (permalink / raw)
  To: Daniel P. Berrangé
  Cc: Kevin Wolf, Jiri Pirko, qemu-block, Juan Quintela,
	Markus Armbruster, Jason Wang, qemu-devel, Laurent Vivier,
	virtio-fs, Stefan Hajnoczi, Max Reitz, Dr. David Alan Gilbert,
	Brad Smith

On Wed, May 05, 2021 at 11:36:59AM +0100, Daniel P. Berrangé wrote:
> The GDateTime APIs provided by GLib avoid portability pitfalls, such
> as some platforms where 'struct timeval.tv_sec' field is still 'long'
> instead of 'time_t'. When combined with automatic cleanup, GDateTime
> often results in simpler code too.
> 
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>

Reviewed-by: Gerd Hoffmann <kraxel@redhat.com>



^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH 1/7] migration: use GDateTime for formatting timestamp in snapshot names
  2021-05-05 10:36 ` [PATCH 1/7] migration: use GDateTime for formatting timestamp in snapshot names Daniel P. Berrangé
@ 2021-05-05 14:42   ` Dr. David Alan Gilbert
  2021-05-06  3:14   ` Brad Smith
  1 sibling, 0 replies; 21+ messages in thread
From: Dr. David Alan Gilbert @ 2021-05-05 14:42 UTC (permalink / raw)
  To: Daniel P. Berrangé
  Cc: Kevin Wolf, Jiri Pirko, qemu-block, Juan Quintela,
	Markus Armbruster, Jason Wang, qemu-devel, Laurent Vivier,
	virtio-fs, Gerd Hoffmann, Stefan Hajnoczi, Max Reitz, Brad Smith

* Daniel P. Berrangé (berrange@redhat.com) wrote:
> The GDateTime APIs provided by GLib avoid portability pitfalls, such
> as some platforms where 'struct timeval.tv_sec' field is still 'long'
> instead of 'time_t'. When combined with automatic cleanup, GDateTime
> often results in simpler code too.
> 
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>

Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>

> ---
>  migration/savevm.c | 13 +++++--------
>  1 file changed, 5 insertions(+), 8 deletions(-)
> 
> diff --git a/migration/savevm.c b/migration/savevm.c
> index 52e2d72e4b..72848b946c 100644
> --- a/migration/savevm.c
> +++ b/migration/savevm.c
> @@ -2775,8 +2775,7 @@ bool save_snapshot(const char *name, bool overwrite, const char *vmstate,
>      QEMUFile *f;
>      int saved_vm_running;
>      uint64_t vm_state_size;
> -    qemu_timeval tv;
> -    struct tm tm;
> +    g_autoptr(GDateTime) now = g_date_time_new_now_local();
>      AioContext *aio_context;
>  
>      if (migration_is_blocked(errp)) {
> @@ -2836,9 +2835,8 @@ bool save_snapshot(const char *name, bool overwrite, const char *vmstate,
>      memset(sn, 0, sizeof(*sn));
>  
>      /* fill auxiliary fields */
> -    qemu_gettimeofday(&tv);
> -    sn->date_sec = tv.tv_sec;
> -    sn->date_nsec = tv.tv_usec * 1000;
> +    sn->date_sec = g_date_time_to_unix(now);
> +    sn->date_nsec = g_date_time_get_microsecond(now) * 1000;
>      sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
>      if (replay_mode != REPLAY_MODE_NONE) {
>          sn->icount = replay_get_current_icount();
> @@ -2849,9 +2847,8 @@ bool save_snapshot(const char *name, bool overwrite, const char *vmstate,
>      if (name) {
>          pstrcpy(sn->name, sizeof(sn->name), name);
>      } else {
> -        /* cast below needed for OpenBSD where tv_sec is still 'long' */
> -        localtime_r((const time_t *)&tv.tv_sec, &tm);
> -        strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
> +        g_autofree char *autoname = g_date_time_format(now,  "vm-%Y%m%d%H%M%S");
> +        pstrcpy(sn->name, sizeof(sn->name), autoname);
>      }
>  
>      /* save the VM state */
> -- 
> 2.31.1
> 
-- 
Dr. David Alan Gilbert / dgilbert@redhat.com / Manchester, UK



^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH 7/7] virtiofsd: use GDateTime for formatting timestamp for debug messages
  2021-05-05 10:37 ` [PATCH 7/7] virtiofsd: use GDateTime for formatting timestamp for debug messages Daniel P. Berrangé
@ 2021-05-05 14:51   ` Dr. David Alan Gilbert
  2021-05-06 16:00     ` Dr. David Alan Gilbert
  0 siblings, 1 reply; 21+ messages in thread
From: Dr. David Alan Gilbert @ 2021-05-05 14:51 UTC (permalink / raw)
  To: Daniel P. Berrangé
  Cc: Kevin Wolf, Jiri Pirko, qemu-block, Juan Quintela,
	Markus Armbruster, Jason Wang, qemu-devel, Laurent Vivier,
	virtio-fs, Gerd Hoffmann, Stefan Hajnoczi, Max Reitz, Brad Smith

* Daniel P. Berrangé (berrange@redhat.com) wrote:
> The GDateTime APIs provided by GLib avoid portability pitfalls, such
> as some platforms where 'struct timeval.tv_sec' field is still 'long'
> instead of 'time_t'. When combined with automatic cleanup, GDateTime
> often results in simpler code too.
> 
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>

Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>

> ---
>  tools/virtiofsd/passthrough_ll.c | 25 ++++---------------------
>  1 file changed, 4 insertions(+), 21 deletions(-)
> 
> diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
> index 1553d2ef45..cdd224918c 100644
> --- a/tools/virtiofsd/passthrough_ll.c
> +++ b/tools/virtiofsd/passthrough_ll.c
> @@ -3558,10 +3558,6 @@ static void setup_nofile_rlimit(unsigned long rlimit_nofile)
>  static void log_func(enum fuse_log_level level, const char *fmt, va_list ap)
>  {
>      g_autofree char *localfmt = NULL;
> -    struct timespec ts;
> -    struct tm tm;
> -    char sec_fmt[sizeof "2020-12-07 18:17:54"];
> -    char zone_fmt[sizeof "+0100"];
>  
>      if (current_log_level < level) {
>          return;
> @@ -3573,23 +3569,10 @@ static void log_func(enum fuse_log_level level, const char *fmt, va_list ap)
>              localfmt = g_strdup_printf("[ID: %08ld] %s", syscall(__NR_gettid),
>                                         fmt);
>          } else {
> -            /* try formatting a broken-down timestamp */
> -            if (clock_gettime(CLOCK_REALTIME, &ts) != -1 &&
> -                localtime_r(&ts.tv_sec, &tm) != NULL &&
> -                strftime(sec_fmt, sizeof sec_fmt, "%Y-%m-%d %H:%M:%S",
> -                         &tm) != 0 &&
> -                strftime(zone_fmt, sizeof zone_fmt, "%z", &tm) != 0) {
> -                localfmt = g_strdup_printf("[%s.%02ld%s] [ID: %08ld] %s",
> -                                           sec_fmt,
> -                                           ts.tv_nsec / (10L * 1000 * 1000),
> -                                           zone_fmt, syscall(__NR_gettid),
> -                                           fmt);
> -            } else {
> -                /* fall back to a flat timestamp */
> -                localfmt = g_strdup_printf("[%" PRId64 "] [ID: %08ld] %s",
> -                                           get_clock(), syscall(__NR_gettid),
> -                                           fmt);
> -            }
> +            g_autoptr(GDateTime) now = g_date_time_new_now_local();
> +            g_autofree char *nowstr = g_date_time_format(now, "%Y-%m-%d %H:%M:%S.%f%z");
> +            localfmt = g_strdup_printf("[%s] [ID: %08ld] %s",
> +                                       nowstr, syscall(__NR_gettid), fmt);
>          }
>          fmt = localfmt;
>      }
> -- 
> 2.31.1
> 
-- 
Dr. David Alan Gilbert / dgilbert@redhat.com / Manchester, UK



^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH 1/7] migration: use GDateTime for formatting timestamp in snapshot names
  2021-05-05 10:36 ` [PATCH 1/7] migration: use GDateTime for formatting timestamp in snapshot names Daniel P. Berrangé
  2021-05-05 14:42   ` Dr. David Alan Gilbert
@ 2021-05-06  3:14   ` Brad Smith
  1 sibling, 0 replies; 21+ messages in thread
From: Brad Smith @ 2021-05-06  3:14 UTC (permalink / raw)
  To: Daniel P. Berrangé, qemu-devel
  Cc: Laurent Vivier, Jiri Pirko, Stefan Hajnoczi, Gerd Hoffmann,
	Jason Wang, qemu-block, Kevin Wolf, Max Reitz, Markus Armbruster,
	Dr. David Alan Gilbert, Juan Quintela, virtio-fs

Thank you.

On 5/5/2021 6:36 AM, Daniel P. Berrangé wrote:
> The GDateTime APIs provided by GLib avoid portability pitfalls, such
> as some platforms where 'struct timeval.tv_sec' field is still 'long'
> instead of 'time_t'. When combined with automatic cleanup, GDateTime
> often results in simpler code too.
>
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>   migration/savevm.c | 13 +++++--------
>   1 file changed, 5 insertions(+), 8 deletions(-)
>
> diff --git a/migration/savevm.c b/migration/savevm.c
> index 52e2d72e4b..72848b946c 100644
> --- a/migration/savevm.c
> +++ b/migration/savevm.c
> @@ -2775,8 +2775,7 @@ bool save_snapshot(const char *name, bool overwrite, const char *vmstate,
>       QEMUFile *f;
>       int saved_vm_running;
>       uint64_t vm_state_size;
> -    qemu_timeval tv;
> -    struct tm tm;
> +    g_autoptr(GDateTime) now = g_date_time_new_now_local();
>       AioContext *aio_context;
>   
>       if (migration_is_blocked(errp)) {
> @@ -2836,9 +2835,8 @@ bool save_snapshot(const char *name, bool overwrite, const char *vmstate,
>       memset(sn, 0, sizeof(*sn));
>   
>       /* fill auxiliary fields */
> -    qemu_gettimeofday(&tv);
> -    sn->date_sec = tv.tv_sec;
> -    sn->date_nsec = tv.tv_usec * 1000;
> +    sn->date_sec = g_date_time_to_unix(now);
> +    sn->date_nsec = g_date_time_get_microsecond(now) * 1000;
>       sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
>       if (replay_mode != REPLAY_MODE_NONE) {
>           sn->icount = replay_get_current_icount();
> @@ -2849,9 +2847,8 @@ bool save_snapshot(const char *name, bool overwrite, const char *vmstate,
>       if (name) {
>           pstrcpy(sn->name, sizeof(sn->name), name);
>       } else {
> -        /* cast below needed for OpenBSD where tv_sec is still 'long' */
> -        localtime_r((const time_t *)&tv.tv_sec, &tm);
> -        strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
> +        g_autofree char *autoname = g_date_time_format(now,  "vm-%Y%m%d%H%M%S");
> +        pstrcpy(sn->name, sizeof(sn->name), autoname);
>       }
>   
>       /* save the VM state */


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH 7/7] virtiofsd: use GDateTime for formatting timestamp for debug messages
  2021-05-05 14:51   ` Dr. David Alan Gilbert
@ 2021-05-06 16:00     ` Dr. David Alan Gilbert
  2021-05-06 17:45       ` Dr. David Alan Gilbert
  0 siblings, 1 reply; 21+ messages in thread
From: Dr. David Alan Gilbert @ 2021-05-06 16:00 UTC (permalink / raw)
  To: Daniel P. Berrangé
  Cc: Kevin Wolf, Jiri Pirko, qemu-block, Juan Quintela,
	Markus Armbruster, Jason Wang, qemu-devel, Laurent Vivier,
	virtio-fs, Gerd Hoffmann, Stefan Hajnoczi, Max Reitz, Brad Smith

* Dr. David Alan Gilbert (dgilbert@redhat.com) wrote:
> * Daniel P. Berrangé (berrange@redhat.com) wrote:
> > The GDateTime APIs provided by GLib avoid portability pitfalls, such
> > as some platforms where 'struct timeval.tv_sec' field is still 'long'
> > instead of 'time_t'. When combined with automatic cleanup, GDateTime
> > often results in simpler code too.
> > 
> > Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> 
> Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>

Queued this 7/7 via virtiofsd

> > ---
> >  tools/virtiofsd/passthrough_ll.c | 25 ++++---------------------
> >  1 file changed, 4 insertions(+), 21 deletions(-)
> > 
> > diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
> > index 1553d2ef45..cdd224918c 100644
> > --- a/tools/virtiofsd/passthrough_ll.c
> > +++ b/tools/virtiofsd/passthrough_ll.c
> > @@ -3558,10 +3558,6 @@ static void setup_nofile_rlimit(unsigned long rlimit_nofile)
> >  static void log_func(enum fuse_log_level level, const char *fmt, va_list ap)
> >  {
> >      g_autofree char *localfmt = NULL;
> > -    struct timespec ts;
> > -    struct tm tm;
> > -    char sec_fmt[sizeof "2020-12-07 18:17:54"];
> > -    char zone_fmt[sizeof "+0100"];
> >  
> >      if (current_log_level < level) {
> >          return;
> > @@ -3573,23 +3569,10 @@ static void log_func(enum fuse_log_level level, const char *fmt, va_list ap)
> >              localfmt = g_strdup_printf("[ID: %08ld] %s", syscall(__NR_gettid),
> >                                         fmt);
> >          } else {
> > -            /* try formatting a broken-down timestamp */
> > -            if (clock_gettime(CLOCK_REALTIME, &ts) != -1 &&
> > -                localtime_r(&ts.tv_sec, &tm) != NULL &&
> > -                strftime(sec_fmt, sizeof sec_fmt, "%Y-%m-%d %H:%M:%S",
> > -                         &tm) != 0 &&
> > -                strftime(zone_fmt, sizeof zone_fmt, "%z", &tm) != 0) {
> > -                localfmt = g_strdup_printf("[%s.%02ld%s] [ID: %08ld] %s",
> > -                                           sec_fmt,
> > -                                           ts.tv_nsec / (10L * 1000 * 1000),
> > -                                           zone_fmt, syscall(__NR_gettid),
> > -                                           fmt);
> > -            } else {
> > -                /* fall back to a flat timestamp */
> > -                localfmt = g_strdup_printf("[%" PRId64 "] [ID: %08ld] %s",
> > -                                           get_clock(), syscall(__NR_gettid),
> > -                                           fmt);
> > -            }
> > +            g_autoptr(GDateTime) now = g_date_time_new_now_local();
> > +            g_autofree char *nowstr = g_date_time_format(now, "%Y-%m-%d %H:%M:%S.%f%z");
> > +            localfmt = g_strdup_printf("[%s] [ID: %08ld] %s",
> > +                                       nowstr, syscall(__NR_gettid), fmt);
> >          }
> >          fmt = localfmt;
> >      }
> > -- 
> > 2.31.1
> > 
> -- 
> Dr. David Alan Gilbert / dgilbert@redhat.com / Manchester, UK
> 
> 
-- 
Dr. David Alan Gilbert / dgilbert@redhat.com / Manchester, UK



^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH 7/7] virtiofsd: use GDateTime for formatting timestamp for debug messages
  2021-05-06 16:00     ` Dr. David Alan Gilbert
@ 2021-05-06 17:45       ` Dr. David Alan Gilbert
  0 siblings, 0 replies; 21+ messages in thread
From: Dr. David Alan Gilbert @ 2021-05-06 17:45 UTC (permalink / raw)
  To: Daniel P. Berrangé
  Cc: Kevin Wolf, Jiri Pirko, qemu-block, Juan Quintela,
	Markus Armbruster, Jason Wang, qemu-devel, Laurent Vivier,
	virtio-fs, Gerd Hoffmann, Stefan Hajnoczi, Max Reitz, Brad Smith

* Dr. David Alan Gilbert (dgilbert@redhat.com) wrote:
> * Dr. David Alan Gilbert (dgilbert@redhat.com) wrote:
> > * Daniel P. Berrangé (berrange@redhat.com) wrote:
> > > The GDateTime APIs provided by GLib avoid portability pitfalls, such
> > > as some platforms where 'struct timeval.tv_sec' field is still 'long'
> > > instead of 'time_t'. When combined with automatic cleanup, GDateTime
> > > often results in simpler code too.
> > > 
> > > Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> > 
> > Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
> 
> Queued this 7/7 via virtiofsd

Hi Dan,
  I've had to drop this because it triggers seccomp:

#0  0x00007f0afec4d21b in readlink () from /lib64/libc.so.6
#1  0x00007f0afeecff24 in g_file_read_link () from /lib64/libglib-2.0.so.0
#2  0x00007f0afef16390 in g_time_zone_new_identifier () from /lib64/libglib-2.0.so.0
#3  0x00007f0afef16fe8 in g_time_zone_new_local () from /lib64/libglib-2.0.so.0
#4  0x00007f0afeec9520 in g_date_time_new_now_local () from /lib64/libglib-2.0.so.0
#5  0x000055a6bd6bc27a in log_func (level=FUSE_LOG_INFO, fmt=0x55a6bd6e3cda "%s: Entry\n", ap=0x7ffece4b2f50) at ../tools/virtiofsd/passthrough_ll.c:3578
#6  0x000055a6bd6b3dd5 in fuse_log (level=level@entry=FUSE_LOG_INFO, fmt=fmt@entry=0x55a6bd6e3cda "%s: Entry\n") at ../tools/virtiofsd/fuse_log.c:37
#7  0x000055a6bd6ba9fd in virtio_loop (se=se@entry=0x55a6bee29a30) at ../tools/virtiofsd/fuse_virtio.c:878
#8  0x000055a6bd6b2017 in main (argc=<optimized out>, argv=<optimized out>) at ../tools/virtiofsd/passthrough_ll.c:3868


so you either need to add seccomp rules and/or persuade it not to moan;
maybe dropping down to UTC would work.

Dave

> > > ---
> > >  tools/virtiofsd/passthrough_ll.c | 25 ++++---------------------
> > >  1 file changed, 4 insertions(+), 21 deletions(-)
> > > 
> > > diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
> > > index 1553d2ef45..cdd224918c 100644
> > > --- a/tools/virtiofsd/passthrough_ll.c
> > > +++ b/tools/virtiofsd/passthrough_ll.c
> > > @@ -3558,10 +3558,6 @@ static void setup_nofile_rlimit(unsigned long rlimit_nofile)
> > >  static void log_func(enum fuse_log_level level, const char *fmt, va_list ap)
> > >  {
> > >      g_autofree char *localfmt = NULL;
> > > -    struct timespec ts;
> > > -    struct tm tm;
> > > -    char sec_fmt[sizeof "2020-12-07 18:17:54"];
> > > -    char zone_fmt[sizeof "+0100"];
> > >  
> > >      if (current_log_level < level) {
> > >          return;
> > > @@ -3573,23 +3569,10 @@ static void log_func(enum fuse_log_level level, const char *fmt, va_list ap)
> > >              localfmt = g_strdup_printf("[ID: %08ld] %s", syscall(__NR_gettid),
> > >                                         fmt);
> > >          } else {
> > > -            /* try formatting a broken-down timestamp */
> > > -            if (clock_gettime(CLOCK_REALTIME, &ts) != -1 &&
> > > -                localtime_r(&ts.tv_sec, &tm) != NULL &&
> > > -                strftime(sec_fmt, sizeof sec_fmt, "%Y-%m-%d %H:%M:%S",
> > > -                         &tm) != 0 &&
> > > -                strftime(zone_fmt, sizeof zone_fmt, "%z", &tm) != 0) {
> > > -                localfmt = g_strdup_printf("[%s.%02ld%s] [ID: %08ld] %s",
> > > -                                           sec_fmt,
> > > -                                           ts.tv_nsec / (10L * 1000 * 1000),
> > > -                                           zone_fmt, syscall(__NR_gettid),
> > > -                                           fmt);
> > > -            } else {
> > > -                /* fall back to a flat timestamp */
> > > -                localfmt = g_strdup_printf("[%" PRId64 "] [ID: %08ld] %s",
> > > -                                           get_clock(), syscall(__NR_gettid),
> > > -                                           fmt);
> > > -            }
> > > +            g_autoptr(GDateTime) now = g_date_time_new_now_local();
> > > +            g_autofree char *nowstr = g_date_time_format(now, "%Y-%m-%d %H:%M:%S.%f%z");
> > > +            localfmt = g_strdup_printf("[%s] [ID: %08ld] %s",
> > > +                                       nowstr, syscall(__NR_gettid), fmt);
> > >          }
> > >          fmt = localfmt;
> > >      }
> > > -- 
> > > 2.31.1
> > > 
> > -- 
> > Dr. David Alan Gilbert / dgilbert@redhat.com / Manchester, UK
> > 
> > 
> -- 
> Dr. David Alan Gilbert / dgilbert@redhat.com / Manchester, UK
-- 
Dr. David Alan Gilbert / dgilbert@redhat.com / Manchester, UK



^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH 2/7] block: use GDateTime for formatting timestamp when dumping snapshot info
  2021-05-05 10:36 ` [PATCH 2/7] block: use GDateTime for formatting timestamp when dumping snapshot info Daniel P. Berrangé
@ 2021-05-07 17:05   ` Max Reitz
  0 siblings, 0 replies; 21+ messages in thread
From: Max Reitz @ 2021-05-07 17:05 UTC (permalink / raw)
  To: Daniel P. Berrangé, qemu-devel
  Cc: Kevin Wolf, Jiri Pirko, qemu-block, Juan Quintela, Jason Wang,
	Laurent Vivier, Markus Armbruster, virtio-fs, Gerd Hoffmann,
	Stefan Hajnoczi, Dr. David Alan Gilbert, Brad Smith

On 05.05.21 12:36, Daniel P. Berrangé wrote:
> The GDateTime APIs provided by GLib avoid portability pitfalls, such
> as some platforms where 'struct timeval.tv_sec' field is still 'long'
> instead of 'time_t'. When combined with automatic cleanup, GDateTime
> often results in simpler code too.
> 
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>   block/qapi.c | 11 ++++-------
>   1 file changed, 4 insertions(+), 7 deletions(-)

Reviewed-by: Max Reitz <mreitz@redhat.com>



^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH 6/7] linux-user: use GDateTime for formatting timestamp for core file
  2021-05-05 10:37 ` [PATCH 6/7] linux-user: use GDateTime for formatting timestamp for core file Daniel P. Berrangé
  2021-05-05 11:18   ` Laurent Vivier
@ 2021-05-15 19:51   ` Laurent Vivier
  1 sibling, 0 replies; 21+ messages in thread
From: Laurent Vivier @ 2021-05-15 19:51 UTC (permalink / raw)
  To: Daniel P. Berrangé, qemu-devel
  Cc: Kevin Wolf, Jiri Pirko, qemu-block, Juan Quintela, Jason Wang,
	Markus Armbruster, Max Reitz, virtio-fs, Gerd Hoffmann,
	Stefan Hajnoczi, Dr. David Alan Gilbert, Brad Smith

Le 05/05/2021 à 12:37, Daniel P. Berrangé a écrit :
> The GDateTime APIs provided by GLib avoid portability pitfalls, such
> as some platforms where 'struct timeval.tv_sec' field is still 'long'
> instead of 'time_t'. When combined with automatic cleanup, GDateTime
> often results in simpler code too.
> 
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>  linux-user/elfload.c | 36 +++++++++---------------------------
>  1 file changed, 9 insertions(+), 27 deletions(-)
> 
> diff --git a/linux-user/elfload.c b/linux-user/elfload.c
> index c6731013fd..c38b7b4d37 100644
> --- a/linux-user/elfload.c
> +++ b/linux-user/elfload.c
> @@ -3386,7 +3386,6 @@ static size_t note_size(const struct memelfnote *);
>  static void free_note_info(struct elf_note_info *);
>  static int fill_note_info(struct elf_note_info *, long, const CPUArchState *);
>  static void fill_thread_info(struct elf_note_info *, const CPUArchState *);
> -static int core_dump_filename(const TaskState *, char *, size_t);
>  
>  static int dump_write(int, const void *, size_t);
>  static int write_note(struct memelfnote *, int);
> @@ -3685,32 +3684,16 @@ static void fill_auxv_note(struct memelfnote *note, const TaskState *ts)
>   * for the name:
>   *     qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
>   *
> - * Returns 0 in case of success, -1 otherwise (errno is set).
> + * Returns the filename
>   */
> -static int core_dump_filename(const TaskState *ts, char *buf,
> -                              size_t bufsize)
> +static char *core_dump_filename(const TaskState *ts)
>  {
> -    char timestamp[64];
> -    char *base_filename = NULL;
> -    struct timeval tv;
> -    struct tm tm;
> +    g_autoptr(GDateTime) now = g_date_time_new_now_local();
> +    g_autofree char *nowstr = g_date_time_format(now, "%Y%m%d-%H%M%S");
> +    g_autofree char *base_filename = g_path_get_basename(ts->bprm->filename);
>  
> -    assert(bufsize >= PATH_MAX);
> -
> -    if (gettimeofday(&tv, NULL) < 0) {
> -        (void) fprintf(stderr, "unable to get current timestamp: %s",
> -                       strerror(errno));
> -        return (-1);
> -    }
> -
> -    base_filename = g_path_get_basename(ts->bprm->filename);
> -    (void) strftime(timestamp, sizeof (timestamp), "%Y%m%d-%H%M%S",
> -                    localtime_r(&tv.tv_sec, &tm));
> -    (void) snprintf(buf, bufsize, "qemu_%s_%s_%d.core",
> -                    base_filename, timestamp, (int)getpid());
> -    g_free(base_filename);
> -
> -    return (0);
> +    return g_strdup_printf("qemu_%s_%s_%d.core",
> +                           base_filename, nowstr, (int)getpid());
>  }
>  
>  static int dump_write(int fd, const void *ptr, size_t size)
> @@ -3938,7 +3921,7 @@ static int elf_core_dump(int signr, const CPUArchState *env)
>      const CPUState *cpu = env_cpu((CPUArchState *)env);
>      const TaskState *ts = (const TaskState *)cpu->opaque;
>      struct vm_area_struct *vma = NULL;
> -    char corefile[PATH_MAX];
> +    g_autofree char *corefile = NULL;
>      struct elf_note_info info;
>      struct elfhdr elf;
>      struct elf_phdr phdr;
> @@ -3955,8 +3938,7 @@ static int elf_core_dump(int signr, const CPUArchState *env)
>      if (dumpsize.rlim_cur == 0)
>          return 0;
>  
> -    if (core_dump_filename(ts, corefile, sizeof (corefile)) < 0)
> -        return (-errno);
> +    corefile = core_dump_filename(ts);
>  
>      if ((fd = open(corefile, O_WRONLY | O_CREAT,
>                     S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
> 

Applied to my linux-user-for-6.1 branch.

Thanks,
Laurent




^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH 3/7] net/rocker: use GDateTime for formatting timestamp in debug messages
  2021-05-05 10:36 ` [PATCH 3/7] net/rocker: use GDateTime for formatting timestamp in debug messages Daniel P. Berrangé
@ 2021-06-11 16:28   ` Daniel P. Berrangé
  2021-06-14 16:43     ` Juan Quintela
  0 siblings, 1 reply; 21+ messages in thread
From: Daniel P. Berrangé @ 2021-06-11 16:28 UTC (permalink / raw)
  To: qemu-devel
  Cc: Kevin Wolf, Jiri Pirko, qemu-block, Juan Quintela,
	Markus Armbruster, Jason Wang, Laurent Vivier, Max Reitz,
	virtio-fs, Gerd Hoffmann, Stefan Hajnoczi,
	Dr. David Alan Gilbert, Brad Smith

ping: anyone willing to give a review of this one

On Wed, May 05, 2021 at 11:36:58AM +0100, Daniel P. Berrangé wrote:
> The GDateTime APIs provided by GLib avoid portability pitfalls, such
> as some platforms where 'struct timeval.tv_sec' field is still 'long'
> instead of 'time_t'. When combined with automatic cleanup, GDateTime
> often results in simpler code too.
> 
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>  hw/net/rocker/rocker.h | 11 +++--------
>  1 file changed, 3 insertions(+), 8 deletions(-)
> 
> diff --git a/hw/net/rocker/rocker.h b/hw/net/rocker/rocker.h
> index 941c932265..412fa44d01 100644
> --- a/hw/net/rocker/rocker.h
> +++ b/hw/net/rocker/rocker.h
> @@ -25,14 +25,9 @@
>  #if defined(DEBUG_ROCKER)
>  #  define DPRINTF(fmt, ...) \
>      do {                                                           \
> -        struct timeval tv;                                         \
> -        char timestr[64];                                          \
> -        time_t now;                                                \
> -        gettimeofday(&tv, NULL);                                   \
> -        now = tv.tv_sec;                                           \
> -        strftime(timestr, sizeof(timestr), "%T", localtime(&now)); \
> -        fprintf(stderr, "%s.%06ld ", timestr, tv.tv_usec);         \
> -        fprintf(stderr, "ROCKER: " fmt, ## __VA_ARGS__);           \
> +        g_autoptr(GDateTime) now = g_date_time_new_now_local();    \
> +        g_autofree char *nowstr = g_date_time_format(now, "%T.%f");\
> +        fprintf(stderr, "%s ROCKER: " fmt, nowstr, ## __VA_ARGS__);\
>      } while (0)
>  #else
>  static inline GCC_FMT_ATTR(1, 2) int DPRINTF(const char *fmt, ...)
> -- 
> 2.31.1
> 

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 :|



^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH 5/7] io: use GDateTime for formatting timestamp for websock headers
  2021-05-05 10:37 ` [PATCH 5/7] io: use GDateTime for formatting timestamp for websock headers Daniel P. Berrangé
@ 2021-06-11 16:29   ` Daniel P. Berrangé
  2021-06-14 15:41     ` Dr. David Alan Gilbert
  0 siblings, 1 reply; 21+ messages in thread
From: Daniel P. Berrangé @ 2021-06-11 16:29 UTC (permalink / raw)
  To: qemu-devel
  Cc: Kevin Wolf, Jiri Pirko, qemu-block, Juan Quintela,
	Markus Armbruster, Jason Wang, Laurent Vivier, Max Reitz,
	virtio-fs, Gerd Hoffmann, Stefan Hajnoczi,
	Dr. David Alan Gilbert, Brad Smith

ping: anyone willing to review this

On Wed, May 05, 2021 at 11:37:00AM +0100, Daniel P. Berrangé wrote:
> The GDateTime APIs provided by GLib avoid portability pitfalls, such
> as some platforms where 'struct timeval.tv_sec' field is still 'long'
> instead of 'time_t'. When combined with automatic cleanup, GDateTime
> often results in simpler code too.
> 
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>  io/channel-websock.c | 10 ++--------
>  1 file changed, 2 insertions(+), 8 deletions(-)
> 
> diff --git a/io/channel-websock.c b/io/channel-websock.c
> index 03c1f7cb62..70889bb54d 100644
> --- a/io/channel-websock.c
> +++ b/io/channel-websock.c
> @@ -177,15 +177,9 @@ qio_channel_websock_handshake_send_res(QIOChannelWebsock *ioc,
>  
>  static gchar *qio_channel_websock_date_str(void)
>  {
> -    struct tm tm;
> -    time_t now = time(NULL);
> -    char datebuf[128];
> +    g_autoptr(GDateTime) now = g_date_time_new_now_utc();
>  
> -    gmtime_r(&now, &tm);
> -
> -    strftime(datebuf, sizeof(datebuf), "%a, %d %b %Y %H:%M:%S GMT", &tm);
> -
> -    return g_strdup(datebuf);
> +    return g_date_time_format(now, "%a, %d %b %Y %H:%M:%S GMT");
>  }
>  
>  static void qio_channel_websock_handshake_send_res_err(QIOChannelWebsock *ioc,
> -- 
> 2.31.1
> 

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 :|



^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH 5/7] io: use GDateTime for formatting timestamp for websock headers
  2021-06-11 16:29   ` Daniel P. Berrangé
@ 2021-06-14 15:41     ` Dr. David Alan Gilbert
  0 siblings, 0 replies; 21+ messages in thread
From: Dr. David Alan Gilbert @ 2021-06-14 15:41 UTC (permalink / raw)
  To: Daniel P. Berrangé
  Cc: Kevin Wolf, Jiri Pirko, qemu-block, Juan Quintela,
	Markus Armbruster, Jason Wang, qemu-devel, Laurent Vivier,
	virtio-fs, Gerd Hoffmann, Stefan Hajnoczi, Max Reitz, Brad Smith

* Daniel P. Berrangé (berrange@redhat.com) wrote:
> ping: anyone willing to review this
> 

Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>

> On Wed, May 05, 2021 at 11:37:00AM +0100, Daniel P. Berrangé wrote:
> > The GDateTime APIs provided by GLib avoid portability pitfalls, such
> > as some platforms where 'struct timeval.tv_sec' field is still 'long'
> > instead of 'time_t'. When combined with automatic cleanup, GDateTime
> > often results in simpler code too.
> > 
> > Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> > ---
> >  io/channel-websock.c | 10 ++--------
> >  1 file changed, 2 insertions(+), 8 deletions(-)
> > 
> > diff --git a/io/channel-websock.c b/io/channel-websock.c
> > index 03c1f7cb62..70889bb54d 100644
> > --- a/io/channel-websock.c
> > +++ b/io/channel-websock.c
> > @@ -177,15 +177,9 @@ qio_channel_websock_handshake_send_res(QIOChannelWebsock *ioc,
> >  
> >  static gchar *qio_channel_websock_date_str(void)
> >  {
> > -    struct tm tm;
> > -    time_t now = time(NULL);
> > -    char datebuf[128];
> > +    g_autoptr(GDateTime) now = g_date_time_new_now_utc();
> >  
> > -    gmtime_r(&now, &tm);
> > -
> > -    strftime(datebuf, sizeof(datebuf), "%a, %d %b %Y %H:%M:%S GMT", &tm);
> > -
> > -    return g_strdup(datebuf);
> > +    return g_date_time_format(now, "%a, %d %b %Y %H:%M:%S GMT");
> >  }
> >  
> >  static void qio_channel_websock_handshake_send_res_err(QIOChannelWebsock *ioc,
> > -- 
> > 2.31.1
> > 
> 
> 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 :|
-- 
Dr. David Alan Gilbert / dgilbert@redhat.com / Manchester, UK



^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH 3/7] net/rocker: use GDateTime for formatting timestamp in debug messages
  2021-06-11 16:28   ` Daniel P. Berrangé
@ 2021-06-14 16:43     ` Juan Quintela
  0 siblings, 0 replies; 21+ messages in thread
From: Juan Quintela @ 2021-06-14 16:43 UTC (permalink / raw)
  To: Daniel P. Berrangé
  Cc: Kevin Wolf, Jiri Pirko, qemu-block, Markus Armbruster,
	Jason Wang, qemu-devel, Laurent Vivier, virtio-fs, Gerd Hoffmann,
	Stefan Hajnoczi, Max Reitz, Dr. David Alan Gilbert, Brad Smith

Daniel P. Berrangé <berrange@redhat.com> wrote:
> ping: anyone willing to give a review of this one
>
> On Wed, May 05, 2021 at 11:36:58AM +0100, Daniel P. Berrangé wrote:
>> The GDateTime APIs provided by GLib avoid portability pitfalls, such
>> as some platforms where 'struct timeval.tv_sec' field is still 'long'
>> instead of 'time_t'. When combined with automatic cleanup, GDateTime
>> often results in simpler code too.
>> 
>> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
>> ---
>>  hw/net/rocker/rocker.h | 11 +++--------
>>  1 file changed, 3 insertions(+), 8 deletions(-)
>> 
>> diff --git a/hw/net/rocker/rocker.h b/hw/net/rocker/rocker.h
>> index 941c932265..412fa44d01 100644
>> --- a/hw/net/rocker/rocker.h
>> +++ b/hw/net/rocker/rocker.h
>> @@ -25,14 +25,9 @@
>>  #if defined(DEBUG_ROCKER)
>>  #  define DPRINTF(fmt, ...) \
>>      do {                                                           \
>> -        struct timeval tv;                                         \
>> -        char timestr[64];                                          \
>> -        time_t now;                                                \
>> -        gettimeofday(&tv, NULL);                                   \
>> -        now = tv.tv_sec;                                           \
>> -        strftime(timestr, sizeof(timestr), "%T", localtime(&now)); \
>> -        fprintf(stderr, "%s.%06ld ", timestr, tv.tv_usec);         \
>> -        fprintf(stderr, "ROCKER: " fmt, ## __VA_ARGS__);           \
>> +        g_autoptr(GDateTime) now = g_date_time_new_now_local();    \
>> +        g_autofree char *nowstr = g_date_time_format(now, "%T.%f");\
>> +        fprintf(stderr, "%s ROCKER: " fmt, nowstr, ## __VA_ARGS__);\
>>      } while (0)
>>  #else
>>  static inline GCC_FMT_ATTR(1, 2) int DPRINTF(const char *fmt, ...)

Reviewed-by: Juan Quintela <quintela@redhat.com>



^ permalink raw reply	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2021-06-14 16:44 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-05 10:36 [PATCH 0/7] replace all use of strftime() with g_date_time_format() Daniel P. Berrangé
2021-05-05 10:36 ` [PATCH 1/7] migration: use GDateTime for formatting timestamp in snapshot names Daniel P. Berrangé
2021-05-05 14:42   ` Dr. David Alan Gilbert
2021-05-06  3:14   ` Brad Smith
2021-05-05 10:36 ` [PATCH 2/7] block: use GDateTime for formatting timestamp when dumping snapshot info Daniel P. Berrangé
2021-05-07 17:05   ` Max Reitz
2021-05-05 10:36 ` [PATCH 3/7] net/rocker: use GDateTime for formatting timestamp in debug messages Daniel P. Berrangé
2021-06-11 16:28   ` Daniel P. Berrangé
2021-06-14 16:43     ` Juan Quintela
2021-05-05 10:36 ` [PATCH 4/7] usb/dev-mtp: use GDateTime for formatting timestamp for objects Daniel P. Berrangé
2021-05-05 13:08   ` Gerd Hoffmann
2021-05-05 10:37 ` [PATCH 5/7] io: use GDateTime for formatting timestamp for websock headers Daniel P. Berrangé
2021-06-11 16:29   ` Daniel P. Berrangé
2021-06-14 15:41     ` Dr. David Alan Gilbert
2021-05-05 10:37 ` [PATCH 6/7] linux-user: use GDateTime for formatting timestamp for core file Daniel P. Berrangé
2021-05-05 11:18   ` Laurent Vivier
2021-05-15 19:51   ` Laurent Vivier
2021-05-05 10:37 ` [PATCH 7/7] virtiofsd: use GDateTime for formatting timestamp for debug messages Daniel P. Berrangé
2021-05-05 14:51   ` Dr. David Alan Gilbert
2021-05-06 16:00     ` Dr. David Alan Gilbert
2021-05-06 17:45       ` Dr. David Alan Gilbert

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 NNTP newsgroup(s).