All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Daniel P. Berrangé" <berrange@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Markus Armbruster" <armbru@redhat.com>,
	"Dr. David Alan Gilbert" <dave@treblig.org>,
	"Marc-André Lureau" <marcandre.lureau@redhat.com>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Daniel P. Berrangé" <berrange@redhat.com>,
	"Peter Maydell" <peter.maydell@linaro.org>,
	"Richard Henderson" <richard.henderson@linaro.org>
Subject: [PATCH 5/6] util: convert error report code to use QMessageFormat
Date: Thu,  3 Dec 2026 17:25:42 +0000	[thread overview]
Message-ID: <20261203172543.676434-6-berrange@redhat.com> (raw)
In-Reply-To: <20261203172543.676434-1-berrange@redhat.com>

Currently the vreport() function (called from error_report and
friends), will optionally print the timestamp and the guest
name, followed by the program name (if set) and the location
(if set).

Convert to the QMessageFormat APIs to unlock the ability to make
the message format configurable at runtime.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 MAINTAINERS                 |   1 +
 chardev/char.c              |   3 +-
 include/monitor/monitor.h   |   2 +
 include/qemu/error-report.h |  10 ++-
 monitor/monitor.c           |  15 ++++-
 stubs/meson.build           |   1 +
 stubs/monitor-core.c        |  11 ++++
 stubs/monitor-puts.c        |  18 ++++++
 util/error-report.c         | 122 +++++++++++++++++++++---------------
 9 files changed, 123 insertions(+), 60 deletions(-)
 create mode 100644 stubs/monitor-puts.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 85949e91aff..0fae72f6da0 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -3627,6 +3627,7 @@ F: include/monitor/hmp-completion.h
 F: tests/qtest/test-hmp.c
 F: include/qemu/qemu-print.h
 F: util/qemu-print.c
+F: stubs/monitor*
 
 Network device backends
 M: Jason Wang <jasowangio@gmail.com>
diff --git a/chardev/char.c b/chardev/char.c
index 972550ba1f8..727d6152a6a 100644
--- a/chardev/char.c
+++ b/chardev/char.c
@@ -102,7 +102,8 @@ static void do_write_log_timestamps(Chardev *s, const uint8_t *buf, size_t len)
 
         if (s->log_line_start) {
             if (!timestr) {
-                timestr = real_time_iso8601();
+                g_autoptr(GDateTime) dt = g_date_time_new_now_utc();
+                timestr = g_date_time_format_iso8601(dt);
             }
             do_write_log(s, (const uint8_t *)timestr, strlen(timestr));
             do_write_log(s, (const uint8_t *)" ", 1);
diff --git a/include/monitor/monitor.h b/include/monitor/monitor.h
index 2a550aec023..cf56bea9c3e 100644
--- a/include/monitor/monitor.h
+++ b/include/monitor/monitor.h
@@ -38,9 +38,11 @@ int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp);
 int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp);
 
 int monitor_puts(Monitor *mon, const char *str);
+int monitor_puts_len(Monitor *mon, const char *str, size_t len);
 void monitor_flush(Monitor *mon);
 
 int monitor_puts_locked(Monitor *mon, const char *str);
+int monitor_puts_len_locked(Monitor *mon, const char *str, size_t len);
 void monitor_flush_locked(Monitor *mon);
 
 AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
diff --git a/include/qemu/error-report.h b/include/qemu/error-report.h
index 482bbcc8fd3..c69aca6a188 100644
--- a/include/qemu/error-report.h
+++ b/include/qemu/error-report.h
@@ -14,6 +14,7 @@
 #define QEMU_ERROR_REPORT_H
 
 #include "qemu/location.h"
+#include "qemu/message-format.h"
 
 int error_vprintf(const char *fmt, va_list ap) G_GNUC_PRINTF(1, 0);
 int error_printf(const char *fmt, ...) G_GNUC_PRINTF(1, 2);
@@ -31,6 +32,8 @@ bool error_report_once_cond(bool *printed, const char *fmt, ...)
 bool warn_report_once_cond(bool *printed, const char *fmt, ...)
     G_GNUC_PRINTF(2, 3);
 
+void error_set_format(QMessageFormat *format);
+
 void error_init(const char *argv0);
 
 /*
@@ -58,11 +61,6 @@ void error_init(const char *argv0);
 extern bool message_with_timestamp;
 extern bool error_with_guestname;
 extern const char *error_guest_name;
-
-/*
- * Return current datetime in ISO 8601 format.
- * Caller is responsible to g_free() the returned string.
- */
-char *real_time_iso8601(void);
+extern QMessageFormat *error_format;
 
 #endif
diff --git a/monitor/monitor.c b/monitor/monitor.c
index 2654b81890d..309fd8483ce 100644
--- a/monitor/monitor.c
+++ b/monitor/monitor.c
@@ -248,7 +248,7 @@ void monitor_flush(Monitor *mon)
 }
 
 /* flush at every end of line */
-int monitor_puts_locked(Monitor *mon, const char *str)
+int monitor_puts_len_locked(Monitor *mon, const char *str, size_t len)
 {
     int i;
     char c;
@@ -267,10 +267,21 @@ int monitor_puts_locked(Monitor *mon, const char *str)
     return i;
 }
 
+int monitor_puts_locked(Monitor *mon, const char *str)
+{
+    return monitor_puts_len_locked(mon, str, strlen(str));
+}
+
+int monitor_puts_len(Monitor *mon, const char *str, size_t len)
+{
+    QEMU_LOCK_GUARD(&mon->mon_lock);
+    return monitor_puts_len_locked(mon, str, len);
+}
+
 int monitor_puts(Monitor *mon, const char *str)
 {
     QEMU_LOCK_GUARD(&mon->mon_lock);
-    return monitor_puts_locked(mon, str);
+    return monitor_puts_len_locked(mon, str, strlen(str));
 }
 
 static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
diff --git a/stubs/meson.build b/stubs/meson.build
index 744fb759a2f..44bc3cae821 100644
--- a/stubs/meson.build
+++ b/stubs/meson.build
@@ -9,6 +9,7 @@ stub_ss.add(files('is-daemonized.c'))
 stub_ss.add(files('monitor-core.c'))
 stub_ss.add(files('monitor-cur.c'))
 stub_ss.add(files('monitor-hmp.c'))
+stub_ss.add(files('monitor-puts.c'))
 stub_ss.add(files('qapi-event-emit.c'))
 stub_ss.add(files('replay-mode.c'))
 stub_ss.add(files('trace-control.c'))
diff --git a/stubs/monitor-core.c b/stubs/monitor-core.c
index 8dfd46d2a8a..117cb8886c4 100644
--- a/stubs/monitor-core.c
+++ b/stubs/monitor-core.c
@@ -18,4 +18,15 @@ int monitor_hmp_vprintf(MonitorHMP *mon, const char *fmt, va_list ap)
     }
     return -1;
 }
+
+int monitor_hmp_printf(MonitorHMP *mon, const char *fmt, ...)
+{
+    va_list ap;
+    int ret;
+    va_start(ap, fmt);
+    ret = monitor_hmp_vprintf(mon, fmt, ap);
+    va_end(ap);
+    return ret;
+}
+
 #endif
diff --git a/stubs/monitor-puts.c b/stubs/monitor-puts.c
new file mode 100644
index 00000000000..2503fefb7d5
--- /dev/null
+++ b/stubs/monitor-puts.c
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#include "qemu/osdep.h"
+#include "monitor/hmp.h"
+
+#ifdef CONFIG_HMP
+
+int monitor_puts_len(Monitor *mon, const char *str, size_t len)
+{
+    if (g_test_initialized() && !g_test_subprocess() &&
+        getenv("QTEST_SILENT_ERRORS")) {
+        g_test_message("%.*s", (int)len, str);
+        return len;
+    }
+    return -1;
+}
+
+#endif
diff --git a/util/error-report.c b/util/error-report.c
index ebcd672d091..3e499c05a7f 100644
--- a/util/error-report.c
+++ b/util/error-report.c
@@ -13,6 +13,7 @@
 #include "qemu/osdep.h"
 #include "monitor/hmp.h"
 #include "qemu/error-report.h"
+#include "qemu/message-format.h"
 
 /*
  * @report_type is the type of message: error, warning or
@@ -22,12 +23,78 @@ typedef enum {
     REPORT_TYPE_ERROR,
     REPORT_TYPE_WARNING,
     REPORT_TYPE_INFO,
+
+    REPORT_TYPE__MAX
 } report_type;
 
 /* Prepend timestamp to messages */
 bool message_with_timestamp;
 bool error_with_guestname;
 const char *error_guest_name;
+QMessageFormat *error_format;
+QMessageFormat *hmp_format;
+
+static const QMessageLevel levelmap[] = {
+    QMESSAGE_LEVEL_ERROR,
+    QMESSAGE_LEVEL_WARN,
+    QMESSAGE_LEVEL_INFO,
+};
+QEMU_BUILD_BUG_ON(G_N_ELEMENTS(levelmap) != REPORT_TYPE__MAX);
+
+
+#define PROGNAME_FMT "{progname}{?{progname}: }"
+#define LOCATION_FMT "{location}{?{location}: }"
+#define LEVEL_FMT "{levelalt}{?{levelalt}: }"
+#define HMP_FMT LOCATION_FMT LEVEL_FMT
+#define STDIO_FMT PROGNAME_FMT HMP_FMT
+
+static void msg_writefn(void *opaque, const char *buf, size_t len)
+{
+    FILE *fp = opaque;
+    ignore_value(fwrite(buf, 1, len, fp));
+}
+
+
+static void error_message_format_emit_stdio(FILE *fp, report_type type)
+{
+
+    if (!error_format) {
+        const char *formatstr;
+        if (message_with_timestamp && error_with_guestname) {
+            formatstr = "{timestamp} {guestname} " STDIO_FMT;
+        } else if (message_with_timestamp) {
+            formatstr = "{timestamp} " STDIO_FMT;
+        } else if (error_with_guestname) {
+            formatstr = "{guestname} " STDIO_FMT;
+        } else {
+            formatstr = STDIO_FMT;
+        }
+        error_format = qmessage_format_new_from_string(formatstr, &error_abort);
+    }
+
+    qmessage_format_emit(error_format, levelmap[type], msg_writefn, fp);
+}
+
+static void hmp_writefn(void *opaque, const char *buf, size_t len)
+{
+    MonitorHMP *hmp = opaque;
+    monitor_puts_len(MONITOR(hmp), buf, len);
+}
+
+static void error_message_format_emit_hmp(MonitorHMP *hmp, report_type type)
+{
+    if (!hmp_format) {
+        hmp_format = qmessage_format_new_from_string(HMP_FMT, &error_abort);
+    }
+
+    qmessage_format_emit(hmp_format, levelmap[type], hmp_writefn, hmp);
+}
+
+void error_set_format(QMessageFormat *format)
+{
+    qmessage_format_free(error_format);
+    error_format = format;
+}
 
 /*
  * Print to the @hmp monitor, else to stderr.
@@ -80,31 +147,6 @@ int error_printf(const char *fmt, ...)
     return ret;
 }
 
-/*
- * Print current location to current HMP monitor if we have one, else
- * to stderr.
- */
-static void print_loc(MonitorHMP *hmp)
-{
-    g_autofree const char *loc = loc_as_string();
-    const char *sep = "";
-
-    if (!hmp && g_get_prgname()) {
-        fprintf(stderr, "%s:", g_get_prgname());
-        sep = " ";
-    }
-    if (loc && *loc) {
-        error_printf_hmp(hmp, "%s%s: ", sep, loc);
-    } else {
-        error_printf_hmp(hmp, "%s", sep);
-    }
-}
-
-char *real_time_iso8601(void)
-{
-    g_autoptr(GDateTime) dt = g_date_time_new_now_utc();
-    return g_date_time_format_iso8601(dt);
-}
 
 /*
  * Print a message to current HMP monitor if we have one, else to
@@ -118,34 +160,12 @@ G_GNUC_PRINTF(2, 0)
 static void vreport(report_type type, const char *fmt, va_list ap)
 {
     MonitorHMP *hmp = monitor_cur_hmp();
-    gchar *timestr;
 
-    if (!hmp) {
+    if (hmp) {
+        error_message_format_emit_hmp(hmp, type);
+    } else {
         qemu_flockfile(stderr);
-    }
-
-    if (message_with_timestamp && !hmp) {
-        timestr = real_time_iso8601();
-        fprintf(stderr, "%s ", timestr);
-        g_free(timestr);
-    }
-
-    /* Only prepend guest name if -msg guest-name and -name guest=... are set */
-    if (error_with_guestname && error_guest_name && !hmp) {
-        fprintf(stderr, "%s ", error_guest_name);
-    }
-
-    print_loc(hmp);
-
-    switch (type) {
-    case REPORT_TYPE_ERROR:
-        break;
-    case REPORT_TYPE_WARNING:
-        error_printf_hmp(hmp, "warning: ");
-        break;
-    case REPORT_TYPE_INFO:
-        error_printf_hmp(hmp, "info: ");
-        break;
+        error_message_format_emit_stdio(stderr, type);
     }
 
     error_vprintf_hmp(hmp, fmt, ap);
-- 
2.56.1



  parent reply	other threads:[~2026-12-03 17:26 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 17:25 [PATCH 0/6] util: configurable / flexible error/log message output format Daniel P. Berrangé
2026-12-03 17:25 ` [PATCH 1/6] util: pull Location struct and APIs into location.{c,h} Daniel P. Berrangé
2026-12-03 20:51   ` Eric Blake
2026-12-03 17:25 ` [PATCH 2/6] util: add helper to query if a location is set Daniel P. Berrangé
2026-12-03 20:51   ` Eric Blake
2026-12-03 17:25 ` [PATCH 3/6] util: introduce a configurable message format template Daniel P. Berrangé
2026-12-03 21:44   ` Eric Blake
2026-12-04  9:22     ` Daniel P. Berrangé
2026-12-04  0:32   ` Dr. David Alan Gilbert
2026-12-03 17:25 ` [PATCH 4/6] util: convert log code to support QMessageFormat Daniel P. Berrangé
2026-12-03 21:52   ` Eric Blake
2026-12-03 17:25 ` Daniel P. Berrangé [this message]
2026-12-03 21:55   ` [PATCH 5/6] util: convert error report code to use QMessageFormat Eric Blake
2026-12-03 17:25 ` [PATCH 6/6] util: add '-msg format=FORMATSTR' argument Daniel P. Berrangé
2026-12-03 22:02   ` Eric Blake
2026-12-04  9:23     ` Daniel P. Berrangé

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=20261203172543.676434-6-berrange@redhat.com \
    --to=berrange@redhat.com \
    --cc=armbru@redhat.com \
    --cc=dave@treblig.org \
    --cc=marcandre.lureau@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=richard.henderson@linaro.org \
    /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.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.