From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 46758606A32D for ; Thu, 3 Dec 2026 17:26:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=fy1c4TIz Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xZAZX-0003CY-Pa; Thu, 03 Dec 2026 12:26:20 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xZAZS-00034b-Bl for qemu-devel@nongnu.org; Thu, 03 Dec 2026 12:26:16 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xZAZO-0007iZ-7s for qemu-devel@nongnu.org; Thu, 03 Dec 2026 12:26:13 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796318770; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=O3kerfhK8VtYdyiOyX8dUnJUIcN0r7Mbqaq852aOlcs=; b=fy1c4TIzNcaF1KuZ+t6h+RKgakQ48WLK7v/+X6/98/Ni0y566mSfk8fx3108hNmJ8nlzxf mbrY0Dz5wuOdS0aJgsIuRtztRNAvGfQHF3eF3q+vUdv2rBJcEP/AJo9MVmEPOl+zXvSlhR sJySN9G4n+Gk1DONyjPjnMU+qFh6op4= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-345-9gV0Gw38OWyIzne5VsNYXQ-1; Thu, 3 Dec 2026 17:26:00 +0000 X-MC-Unique: 9gV0Gw38OWyIzne5VsNYXQ-1 X-Mimecast-MFC-AGG-ID: 9gV0Gw38OWyIzne5VsNYXQ_1796318759 Received: from mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.95]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id DB2D11877DB7; Thu, 3 Dec 2026 17:25:58 +0000 (UTC) Received: from berrange.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id EFC81B8F; Thu, 3 Dec 2026 17:25:56 +0000 (UTC) From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Cc: Markus Armbruster , "Dr. David Alan Gilbert" , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Peter Maydell , Richard Henderson Subject: [PATCH 5/6] util: convert error report code to use QMessageFormat Date: Thu, 3 Dec 2026 17:25:42 +0000 Message-ID: <20261203172543.676434-6-berrange@redhat.com> In-Reply-To: <20261203172543.676434-1-berrange@redhat.com> References: <20261203172543.676434-1-berrange@redhat.com> X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.6 on 10.30.177.95 Received-SPF: pass client-ip=170.10.129.124; envelope-from=berrange@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.24, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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é --- 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 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