* [PATCH 0/6] util: configurable / flexible error/log message output format
@ 2026-12-03 17:25 Daniel P. Berrangé
2026-12-03 17:25 ` [PATCH 1/6] util: pull Location struct and APIs into location.{c,h} Daniel P. Berrangé
` (5 more replies)
0 siblings, 6 replies; 16+ messages in thread
From: Daniel P. Berrangé @ 2026-12-03 17:25 UTC (permalink / raw)
To: qemu-devel
Cc: Markus Armbruster, Dr. David Alan Gilbert, Marc-André Lureau,
Paolo Bonzini, Daniel P. Berrangé, Peter Maydell,
Richard Henderson
At the start of the year I had a large series to add more info
to the error / log message output, so we can capture things
like thread names, IDs, and more, with more universal formatting
across both areas of code:
https://lists.nongnu.org/archive/html/qemu-devel/2026-02/msg03190.html
While much of the early supporting patches merged, Peter was not
convinced by some of the proposed changes, especially where it
affected the default output format:
https://lists.nongnu.org/archive/html/qemu-devel/2026-02/msg04960.html
I have now re-visited the work and decided on a substantially
different implementation approach.
Aside from Peter's issue, another issue was that the output
format was still substantially hardcoded. While you could turn
off/on individual data items with many new '-msg' flags, there
was no ability to control the order in which they were printed
nor any surrounding separators between fields.
In this new implementation, instead of adding lots of args to
'-msg', there is now a single "format string", which is a mix
of literal text and variable substitions, with some simple
conditions too. This gives the user far better control over
the output format.
Furthermore, the default output format has not been changed at
all from the current state.
So overall this this is a followup to my previous work, I am
calling it version 1, since it is a from scratch implementation.
Daniel P. Berrangé (6):
util: pull Location struct and APIs into location.{c,h}
util: add helper to query if a location is set
util: introduce a configurable message format template
util: convert log code to support QMessageFormat
util: convert error report code to use QMessageFormat
util: add '-msg format=FORMATSTR' argument
MAINTAINERS | 10 +
chardev/char.c | 3 +-
include/monitor/monitor.h | 2 +
include/qemu/error-report.h | 27 +-
include/qemu/location.h | 25 ++
include/qemu/log.h | 3 +
include/qemu/message-format.h | 97 ++++++
monitor/monitor.c | 15 +-
qemu-options.hx | 72 ++++-
stubs/meson.build | 1 +
stubs/monitor-core.c | 11 +
stubs/monitor-puts.c | 18 ++
system/vl.c | 35 +-
tests/unit/meson.build | 1 +
tests/unit/test-util-message-format.c | 226 +++++++++++++
util/error-report.c | 232 ++++---------
util/location.c | 137 ++++++++
util/log.c | 32 +-
util/meson.build | 2 +
util/message-format.c | 449 ++++++++++++++++++++++++++
20 files changed, 1202 insertions(+), 196 deletions(-)
create mode 100644 include/qemu/location.h
create mode 100644 include/qemu/message-format.h
create mode 100644 stubs/monitor-puts.c
create mode 100644 tests/unit/test-util-message-format.c
create mode 100644 util/location.c
create mode 100644 util/message-format.c
--
2.56.1
^ permalink raw reply [flat|nested] 16+ messages in thread
* [PATCH 1/6] util: pull Location struct and APIs into location.{c,h}
2026-12-03 17:25 [PATCH 0/6] util: configurable / flexible error/log message output format Daniel P. Berrangé
@ 2026-12-03 17:25 ` 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é
` (4 subsequent siblings)
5 siblings, 1 reply; 16+ messages in thread
From: Daniel P. Berrangé @ 2026-12-03 17:25 UTC (permalink / raw)
To: qemu-devel
Cc: Markus Armbruster, Dr. David Alan Gilbert, Marc-André Lureau,
Paolo Bonzini, Daniel P. Berrangé, Peter Maydell,
Richard Henderson
The Location struct and APIs are currently alongside the
error report code, since the primary use of Location was
as error reporting.
We need to reference the location APIs from the new
message output APIs, but the latter is in turn going to
be referenced from the error report APIs which sets up
a circular loop. Splitting off the location APIs keeps
the code loop-free.
The new files are put under the existing MAINTAINERS
file entry for error reporting.
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
MAINTAINERS | 2 +
include/qemu/error-report.h | 17 +----
include/qemu/location.h | 24 +++++++
util/error-report.c | 118 ++------------------------------
util/location.c | 131 ++++++++++++++++++++++++++++++++++++
util/meson.build | 1 +
6 files changed, 163 insertions(+), 130 deletions(-)
create mode 100644 include/qemu/location.h
create mode 100644 util/location.c
diff --git a/MAINTAINERS b/MAINTAINERS
index ad9dfc43898..a565919d3de 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -3484,9 +3484,11 @@ M: Markus Armbruster <armbru@redhat.com>
S: Supported
F: include/qapi/error.h
F: include/qemu/error-report.h
+F: include/qemu/location.h
F: qapi/error.json
F: util/error.c
F: util/error-report.c
+F: util/location.c
F: scripts/coccinelle/err-bad-newline.cocci
F: scripts/coccinelle/error-use-after-free.cocci
F: scripts/coccinelle/error_propagate_null.cocci
diff --git a/include/qemu/error-report.h b/include/qemu/error-report.h
index dc423d36071..482bbcc8fd3 100644
--- a/include/qemu/error-report.h
+++ b/include/qemu/error-report.h
@@ -13,22 +13,7 @@
#ifndef QEMU_ERROR_REPORT_H
#define QEMU_ERROR_REPORT_H
-typedef struct Location {
- /* all members are private to qemu-error.c */
- enum { LOC_NONE, LOC_CMDLINE, LOC_FILE } kind;
- int num;
- const void *ptr;
- struct Location *prev;
-} Location;
-
-Location *loc_push_restore(Location *loc);
-Location *loc_push_none(Location *loc);
-Location *loc_pop(Location *loc);
-Location *loc_save(Location *loc);
-void loc_restore(Location *loc);
-void loc_set_none(void);
-void loc_set_cmdline(char **argv, int idx, int cnt);
-void loc_set_file(const char *fname, int lno);
+#include "qemu/location.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);
diff --git a/include/qemu/location.h b/include/qemu/location.h
new file mode 100644
index 00000000000..c900ef9b979
--- /dev/null
+++ b/include/qemu/location.h
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#ifndef QEMU_LOCATION_H
+#define QEMU_LOCATION_H
+
+typedef struct Location {
+ /* all members are private to qemu-error.c */
+ enum { LOC_NONE, LOC_CMDLINE, LOC_FILE } kind;
+ int num;
+ const void *ptr;
+ struct Location *prev;
+} Location;
+
+Location *loc_push_restore(Location *loc);
+Location *loc_push_none(Location *loc);
+Location *loc_pop(Location *loc);
+Location *loc_save(Location *loc);
+void loc_restore(Location *loc);
+void loc_set_none(void);
+void loc_set_cmdline(char **argv, int idx, int cnt);
+void loc_set_file(const char *fname, int lno);
+char *loc_as_string(void);
+
+#endif /* QEMU_LOCATION_H */
diff --git a/util/error-report.c b/util/error-report.c
index 8932beebfb9..ebcd672d091 100644
--- a/util/error-report.c
+++ b/util/error-report.c
@@ -80,132 +80,22 @@ int error_printf(const char *fmt, ...)
return ret;
}
-static Location std_loc = {
- .kind = LOC_NONE
-};
-static Location *cur_loc = &std_loc;
-
-/*
- * Push location saved in LOC onto the location stack, return it.
- * The top of that stack is the current location.
- * Needs a matching loc_pop().
- */
-Location *loc_push_restore(Location *loc)
-{
- assert(!loc->prev);
- loc->prev = cur_loc;
- cur_loc = loc;
- return loc;
-}
-
-/*
- * Initialize *LOC to "nowhere", push it onto the location stack.
- * The top of that stack is the current location.
- * Needs a matching loc_pop().
- * Return LOC.
- */
-Location *loc_push_none(Location *loc)
-{
- loc->kind = LOC_NONE;
- loc->prev = NULL;
- return loc_push_restore(loc);
-}
-
-/*
- * Pop the location stack.
- * LOC must be the current location, i.e. the top of the stack.
- */
-Location *loc_pop(Location *loc)
-{
- assert(cur_loc == loc && loc->prev);
- cur_loc = loc->prev;
- loc->prev = NULL;
- return loc;
-}
-
-/*
- * Save the current location in LOC, return LOC.
- */
-Location *loc_save(Location *loc)
-{
- *loc = *cur_loc;
- loc->prev = NULL;
- return loc;
-}
-
-/*
- * Change the current location to the one saved in LOC.
- */
-void loc_restore(Location *loc)
-{
- Location *prev = cur_loc->prev;
- assert(!loc->prev);
- *cur_loc = *loc;
- cur_loc->prev = prev;
-}
-
-/*
- * Change the current location to "nowhere in particular".
- */
-void loc_set_none(void)
-{
- cur_loc->kind = LOC_NONE;
-}
-
-/*
- * Change the current location to argument ARGV[IDX..IDX+CNT-1].
- */
-void loc_set_cmdline(char **argv, int idx, int cnt)
-{
- cur_loc->kind = LOC_CMDLINE;
- cur_loc->num = cnt;
- cur_loc->ptr = argv + idx;
-}
-
-/*
- * Change the current location to file FNAME, line LNO.
- */
-void loc_set_file(const char *fname, int lno)
-{
- assert (fname || cur_loc->kind == LOC_FILE);
- cur_loc->kind = LOC_FILE;
- cur_loc->num = lno;
- if (fname) {
- cur_loc->ptr = fname;
- }
-}
-
/*
* 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 = "";
- int i;
- const char *const *argp;
if (!hmp && g_get_prgname()) {
fprintf(stderr, "%s:", g_get_prgname());
sep = " ";
}
- switch (cur_loc->kind) {
- case LOC_CMDLINE:
- argp = cur_loc->ptr;
- for (i = 0; i < cur_loc->num; i++) {
- error_printf_hmp(hmp, "%s%s", sep, argp[i]);
- sep = " ";
- }
- error_printf_hmp(hmp, ": ");
- break;
- case LOC_FILE:
- error_printf_hmp(hmp, "%s:", (const char *)cur_loc->ptr);
- if (cur_loc->num) {
- error_printf_hmp(hmp, "%d:", cur_loc->num);
- }
- error_printf_hmp(hmp, " ");
- break;
- default:
+ if (loc && *loc) {
+ error_printf_hmp(hmp, "%s%s: ", sep, loc);
+ } else {
error_printf_hmp(hmp, "%s", sep);
}
}
diff --git a/util/location.c b/util/location.c
new file mode 100644
index 00000000000..9693787039e
--- /dev/null
+++ b/util/location.c
@@ -0,0 +1,131 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#include "qemu/osdep.h"
+#include "qemu/location.h"
+
+
+static Location std_loc = {
+ .kind = LOC_NONE
+};
+static Location *cur_loc = &std_loc;
+
+/*
+ * Push location saved in LOC onto the location stack, return it.
+ * The top of that stack is the current location.
+ * Needs a matching loc_pop().
+ */
+Location *loc_push_restore(Location *loc)
+{
+ assert(!loc->prev);
+ loc->prev = cur_loc;
+ cur_loc = loc;
+ return loc;
+}
+
+/*
+ * Initialize *LOC to "nowhere", push it onto the location stack.
+ * The top of that stack is the current location.
+ * Needs a matching loc_pop().
+ * Return LOC.
+ */
+Location *loc_push_none(Location *loc)
+{
+ loc->kind = LOC_NONE;
+ loc->prev = NULL;
+ return loc_push_restore(loc);
+}
+
+/*
+ * Pop the location stack.
+ * LOC must be the current location, i.e. the top of the stack.
+ */
+Location *loc_pop(Location *loc)
+{
+ assert(cur_loc == loc && loc->prev);
+ cur_loc = loc->prev;
+ loc->prev = NULL;
+ return loc;
+}
+
+/*
+ * Save the current location in LOC, return LOC.
+ */
+Location *loc_save(Location *loc)
+{
+ *loc = *cur_loc;
+ loc->prev = NULL;
+ return loc;
+}
+
+/*
+ * Change the current location to the one saved in LOC.
+ */
+void loc_restore(Location *loc)
+{
+ Location *prev = cur_loc->prev;
+ assert(!loc->prev);
+ *cur_loc = *loc;
+ cur_loc->prev = prev;
+}
+
+/*
+ * Change the current location to "nowhere in particular".
+ */
+void loc_set_none(void)
+{
+ cur_loc->kind = LOC_NONE;
+}
+
+/*
+ * Change the current location to argument ARGV[IDX..IDX+CNT-1].
+ */
+void loc_set_cmdline(char **argv, int idx, int cnt)
+{
+ cur_loc->kind = LOC_CMDLINE;
+ cur_loc->num = cnt;
+ cur_loc->ptr = argv + idx;
+}
+
+/*
+ * Change the current location to file FNAME, line LNO.
+ */
+void loc_set_file(const char *fname, int lno)
+{
+ assert(fname || cur_loc->kind == LOC_FILE);
+ cur_loc->kind = LOC_FILE;
+ cur_loc->num = lno;
+ if (fname) {
+ cur_loc->ptr = fname;
+ }
+}
+
+/*
+ * Return the current location formatted as a string
+ */
+char *loc_as_string(void)
+{
+ GString *str = g_string_new("");
+ const char *sep = "";
+ int i;
+ const char *const *argp;
+
+ switch (cur_loc->kind) {
+ case LOC_CMDLINE:
+ argp = cur_loc->ptr;
+ for (i = 0; i < cur_loc->num; i++) {
+ g_string_append_printf(str, "%s%s", sep, argp[i]);
+ sep = " ";
+ }
+ break;
+ case LOC_FILE:
+ g_string_append_printf(str, "%s", (const char *)cur_loc->ptr);
+ if (cur_loc->num) {
+ g_string_append_printf(str, ":%d", cur_loc->num);
+ }
+ break;
+ default:
+ break;
+ }
+
+ return g_string_free(str, FALSE);
+}
diff --git a/util/meson.build b/util/meson.build
index fa174c07a57..0982481177b 100644
--- a/util/meson.build
+++ b/util/meson.build
@@ -39,6 +39,7 @@ util_ss.add(files('host-utils.c'))
util_ss.add(files('bitmap.c', 'bitops.c'))
util_ss.add(files('fifo8.c'))
util_ss.add(files('cacheflush.c'))
+util_ss.add(files('location.c'))
util_ss.add(files('error.c', 'error-report.c'))
util_ss.add(files('qemu-print.c'))
util_ss.add(files('id.c'))
--
2.56.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH 2/6] util: add helper to query if a location is set
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 17:25 ` 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é
` (3 subsequent siblings)
5 siblings, 1 reply; 16+ messages in thread
From: Daniel P. Berrangé @ 2026-12-03 17:25 UTC (permalink / raw)
To: qemu-devel
Cc: Markus Armbruster, Dr. David Alan Gilbert, Marc-André Lureau,
Paolo Bonzini, Daniel P. Berrangé, Peter Maydell,
Richard Henderson
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
include/qemu/location.h | 1 +
util/location.c | 6 ++++++
2 files changed, 7 insertions(+)
diff --git a/include/qemu/location.h b/include/qemu/location.h
index c900ef9b979..3f5391145a4 100644
--- a/include/qemu/location.h
+++ b/include/qemu/location.h
@@ -20,5 +20,6 @@ void loc_set_none(void);
void loc_set_cmdline(char **argv, int idx, int cnt);
void loc_set_file(const char *fname, int lno);
char *loc_as_string(void);
+bool loc_is_none(void);
#endif /* QEMU_LOCATION_H */
diff --git a/util/location.c b/util/location.c
index 9693787039e..d2bfbb31800 100644
--- a/util/location.c
+++ b/util/location.c
@@ -129,3 +129,9 @@ char *loc_as_string(void)
return g_string_free(str, FALSE);
}
+
+
+bool loc_is_none(void)
+{
+ return cur_loc->kind == LOC_NONE;
+}
--
2.56.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH 3/6] util: introduce a configurable message format template
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 17:25 ` [PATCH 2/6] util: add helper to query if a location is set Daniel P. Berrangé
@ 2026-12-03 17:25 ` Daniel P. Berrangé
2026-12-03 21:44 ` Eric Blake
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é
` (2 subsequent siblings)
5 siblings, 2 replies; 16+ messages in thread
From: Daniel P. Berrangé @ 2026-12-03 17:25 UTC (permalink / raw)
To: qemu-devel
Cc: Markus Armbruster, Dr. David Alan Gilbert, Marc-André Lureau,
Paolo Bonzini, Daniel P. Berrangé, Peter Maydell,
Richard Henderson
Currently error messages, generic log messages and chardev logs
can all have a timestamp prepended, by using the '-msg' argument's
'timestamp' flag.
There are various extra bits of information that are interesting
to emit in various scenarios. Rather than adding many more flags
to '-msg' and hardcoding the order they are emitted in, this
introcuces a configuraable message format template.
A format string template is a concatenation of the following
components
* literal string:
text
In the literal text, a '\' must be used to escape any '{',
'}' or '\' characters.
* a variable substition:
{variablename}
where variable name is one of:
- 'timestamp': the ISO 8601 formatted current time
- 'progname': the current program name
- 'thread': the current thread name
- 'tid': the thread ID
- 'pid': the process ID
- 'location': the current input location
* an conditional expression
{?{variablename}formatstring}
{!{variablename}formatstring}
which evaluates whether the variable is non-empty or empty
respectively, and processes the nested format string.
As example:
"Demo {pid}{?{progname} (in program '}{progname}{?{progname}')}"
Given a 'pid' value of '123' will print the following
if progname is empty:
"Demo 123"
or, if the program name is the strnig 'qemu', will print:
"Demo text (in program 'qemu')"
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
MAINTAINERS | 7 +
include/qemu/message-format.h | 97 ++++++
tests/unit/meson.build | 1 +
tests/unit/test-util-message-format.c | 226 +++++++++++++
util/meson.build | 1 +
util/message-format.c | 449 ++++++++++++++++++++++++++
6 files changed, 781 insertions(+)
create mode 100644 include/qemu/message-format.h
create mode 100644 tests/unit/test-util-message-format.c
create mode 100644 util/message-format.c
diff --git a/MAINTAINERS b/MAINTAINERS
index a565919d3de..85949e91aff 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -3496,6 +3496,13 @@ F: scripts/coccinelle/remove_local_err.cocci
F: scripts/coccinelle/use-error_fatal.cocci
F: scripts/coccinelle/errp-guard.cocci
+Message format
+M: Daniel Berrangé <berrange@redhat.com>
+S: Supported
+F: include/qemu/message-format.h
+F: util/message-format.c
+F: tests/unit/test-util-message-format.c
+
GDB stub
M: Alex Bennée <alex.bennee@linaro.org>
R: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
diff --git a/include/qemu/message-format.h b/include/qemu/message-format.h
new file mode 100644
index 00000000000..4e38277d6cb
--- /dev/null
+++ b/include/qemu/message-format.h
@@ -0,0 +1,97 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#ifndef QEMU_MESSAGE_FORMAT_H
+#define QEMU_MESSAGE_FORMAT_H
+
+#include "qapi/error.h"
+
+
+typedef struct QMessageFormat QMessageFormat;
+
+typedef enum QMessageLevel {
+ QMESSAGE_LEVEL_DEBUG,
+ QMESSAGE_LEVEL_INFO,
+ QMESSAGE_LEVEL_WARN,
+ QMESSAGE_LEVEL_ERROR,
+
+ QMESSAGE_LEVEL__MAX
+} QMessageLevel;
+
+typedef void (*QMessageWriteFunc)(void *opaque, const char *buf, size_t len);
+
+/**
+ * qmessage_format_new_from_string:
+ * @formatstr: the message formatting specification
+ * @errp: NULL initialized pointer to an error object
+ *
+ * Create a new message format object by parsing the
+ * specification in @formatstr, which can contain a
+ * mixture of literal text and placeholders. A
+ * placeholder can either be a plain subsitution:
+ *
+ * {variablename}
+ *
+ * Or can have an prefix and suffix added, if-and-only-if
+ * the expanded variable has a value set.
+ *
+ * {?prefix{variablename}suffix}
+ *
+ * In either the literal text, or the prefix and suffix
+ * of a conditional placeholder, '\' can be used to
+ * escape the special meaning of '{', '}' and '\'.
+ *
+ * As example:
+ *
+ * "Demo {pid}{? (in program '{progname}')}"
+ *
+ * Given a 'pid' value of '123' will print
+ *
+ * "Demo 123"
+ *
+ * or, if the program name is available, will print:
+ *
+ * "Demo text (in program 'qemu')"
+ *
+ * Currently defined placeholders are:
+ *
+ * - 'timestamp': the ISO 8601 formatted current time
+ * - 'progname': the current program name
+ * - 'thread': the current thread name
+ * - 'tid': the thread ID
+ * - 'pid': the process ID
+ * - 'location': the current input location
+ * - 'guestname': the current guest name
+ * - 'level': the message level (debug, info, warning, error)
+ * - 'levelalt': as 'level' but no string printed for 'error'
+ * or 'debug'
+ *
+ * Returns: a new message format, or NULL on error
+ */
+QMessageFormat *qmessage_format_new_from_string(const char *formatstr,
+ Error **errp);
+
+/**
+ * qmessage_format_free:
+ * @format: the message format to free
+ *
+ * Free the message format
+ */
+void qmessage_format_free(QMessageFormat *format);
+
+/**
+ * qmessage_format_emit:
+ * @format: the message format to apply
+ * @level: the message level
+ * @writefn: the callback to write data with
+ * @opaque: data to provide to @writefn
+ *
+ * Emit a format string identified by @format to @writefn
+ */
+void qmessage_format_emit(QMessageFormat *format,
+ QMessageLevel level,
+ QMessageWriteFunc writefn,
+ void *opaque);
+
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(QMessageFormat, qmessage_format_free);
+
+#endif /* QEMU_MESSAGE_H */
diff --git a/tests/unit/meson.build b/tests/unit/meson.build
index 37b561d4a71..3951dfac844 100644
--- a/tests/unit/meson.build
+++ b/tests/unit/meson.build
@@ -50,6 +50,7 @@ tests = {
'test-interval-tree': [],
'test-fifo': [],
'test-envlist': [],
+ 'test-util-message-format': [],
}
if have_system or have_tools
diff --git a/tests/unit/test-util-message-format.c b/tests/unit/test-util-message-format.c
new file mode 100644
index 00000000000..403230ae56a
--- /dev/null
+++ b/tests/unit/test-util-message-format.c
@@ -0,0 +1,226 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#include "qemu/osdep.h"
+#include "qemu/message-format.h"
+#include "qemu/location.h"
+#include "qemu/error-report.h"
+
+
+/*
+ * functions we "mock" override to give predictable
+ * output for testing
+ */
+GDateTime *g_date_time_new_now_utc(void)
+{
+ return g_date_time_new_from_unix_utc(872835240);
+}
+
+
+static bool emptyprgname;
+const char *g_get_prgname(void)
+{
+ return emptyprgname ? NULL : "skynet";
+}
+
+
+int qemu_get_thread_id(void)
+{
+ return 1000;
+}
+
+
+int getpid(void)
+{
+ return 9003;
+}
+
+
+static void str_write(void *opaque, const char *buf, size_t len)
+{
+ GString *str = (GString *)opaque;
+ g_string_append_len(str, buf, len);
+}
+
+
+static void test_util_message_format_null(void)
+{
+ g_autoptr(GString) str = g_string_new(NULL);
+ g_autoptr(QMessageFormat) format =
+ qmessage_format_new_from_string(NULL, &error_abort);
+
+ qmessage_format_emit(format, QMESSAGE_LEVEL_DEBUG, str_write, str);
+
+ g_assert_cmpstr(str->str, ==, "");
+}
+
+
+static void test_util_message_format_empty(void)
+{
+ g_autoptr(GString) str = g_string_new(NULL);
+ g_autoptr(QMessageFormat) format =
+ qmessage_format_new_from_string("", &error_abort);
+
+ qmessage_format_emit(format, QMESSAGE_LEVEL_DEBUG, str_write, str);
+
+ g_assert_cmpstr(str->str, ==, "");
+}
+
+
+static void test_util_message_format_literal(void)
+{
+ g_autoptr(GString) str = g_string_new(NULL);
+ g_autoptr(QMessageFormat) format =
+ qmessage_format_new_from_string("Test", &error_abort);
+
+ qmessage_format_emit(format, QMESSAGE_LEVEL_DEBUG, str_write, str);
+
+ g_assert_cmpstr(str->str, ==, "Test");
+}
+
+
+static void test_util_message_format_variable(void)
+{
+ g_autoptr(GString) str = g_string_new(NULL);
+ g_autoptr(QMessageFormat) format =
+ qmessage_format_new_from_string("{progname}", &error_abort);
+
+ qmessage_format_emit(format, QMESSAGE_LEVEL_DEBUG, str_write, str);
+
+ g_assert_cmpstr(str->str, ==, "skynet");
+}
+
+
+static void test_util_message_format_expression(void)
+{
+ g_autoptr(GString) str = g_string_new(NULL);
+ g_autoptr(QMessageFormat) format =
+ qmessage_format_new_from_string(
+ "Test at {location}{?{location}: OK}{!{location}<unset> NOT OK}!",
+ &error_abort);
+
+ qmessage_format_emit(format, QMESSAGE_LEVEL_DEBUG, str_write, str);
+ loc_set_file("demo.c", 100);
+ qmessage_format_emit(format, QMESSAGE_LEVEL_DEBUG, str_write, str);
+ loc_set_none();
+
+ g_assert_cmpstr(str->str, ==,
+ "Test at <unset> NOT OK!Test at demo.c:100: OK!");
+}
+
+
+static void test_util_message_format_bad(void)
+{
+ const char *badformats[] = {
+ /* regular placeholder, no final closing '}' */
+ "{message",
+ /* conditional placeholder, missing inner opening '{' */
+ "{?message",
+ /* conditional placeholder, missing inner closing '}' */
+ "{?some{message",
+ /* conditional placeholder, missing inner opening '{' */
+ "{?some\\{message",
+ /* conditional placeholder, missing final closing '}' */
+ "{?some{message}\\}",
+ /* conditional placeholder, missing final closing '}' */
+ "{?some{message}some\\}",
+ };
+ size_t i;
+ for (i = 0; i < G_N_ELEMENTS(badformats); i++) {
+ Error *local_err = NULL;
+ QMessageFormat *format;
+ g_test_message("Check '%s'", badformats[i]);
+ format = qmessage_format_new_from_string(badformats[i], &local_err);
+ g_assert(local_err != NULL);
+ g_assert(format == NULL);
+ error_free(local_err);
+ }
+}
+
+
+static void test_util_message_format_escapes(void)
+{
+ g_autoptr(GString) str = g_string_new(NULL);
+ g_autoptr(QMessageFormat) format =
+ qmessage_format_new_from_string(
+ "Test: {progname}{?{progname}\\\\\\}\\\\}!", &error_abort);
+
+ qmessage_format_emit(format, QMESSAGE_LEVEL_DEBUG, str_write, str);
+
+ g_assert_cmpstr(str->str, ==, "Test: skynet\\}\\!");
+}
+
+
+static void test_util_message_format_nested(void)
+{
+ g_autoptr(GString) str = g_string_new(NULL);
+ g_autoptr(QMessageFormat) format =
+ qmessage_format_new_from_string(
+ "{timestamp}: {progname}{?{progname}:"
+ "{!{location} }}{location}{?{location}: }OK",
+ &error_abort);
+
+ emptyprgname = true;
+ qmessage_format_emit(format, QMESSAGE_LEVEL_DEBUG, str_write, str);
+ loc_set_file("demo.c", 100);
+ qmessage_format_emit(format, QMESSAGE_LEVEL_DEBUG, str_write, str);
+ loc_set_none();
+ emptyprgname = false;
+ qmessage_format_emit(format, QMESSAGE_LEVEL_DEBUG, str_write, str);
+ loc_set_file("demo.c", 100);
+ qmessage_format_emit(format, QMESSAGE_LEVEL_DEBUG, str_write, str);
+ loc_set_none();
+
+ g_assert_cmpstr(str->str, ==,
+ "1997-08-29T06:14:00Z: OK"
+ "1997-08-29T06:14:00Z: demo.c:100: OK"
+ "1997-08-29T06:14:00Z: skynet: OK"
+ "1997-08-29T06:14:00Z: skynet:demo.c:100: OK");
+}
+
+
+static void test_util_message_format_everything(void)
+{
+ g_autoptr(GString) str = g_string_new(NULL);
+ g_autoptr(QMessageFormat) format =
+ qmessage_format_new_from_string(
+ "{timestamp}: [{level}] {progname}:"
+ "{location}({pid}:[{thread}:{tid}]): {guestname}: OK",
+ &error_abort);
+
+ loc_set_file("demo.c", 100);
+ error_guest_name = "t1000";
+ qmessage_format_emit(format, QMESSAGE_LEVEL_DEBUG, str_write, str);
+ error_guest_name = NULL;
+ loc_set_none();
+
+ g_assert_cmpstr(str->str, ==,
+ "1997-08-29T06:14:00Z: [debug] "
+ "skynet:demo.c:100(9003:[main:1000]): t1000: OK");
+}
+
+
+int main(int argc, char **argv)
+{
+ g_test_init(&argc, &argv, NULL);
+
+ g_test_add_func("/util/message/format/null",
+ test_util_message_format_null);
+ g_test_add_func("/util/message/format/empty",
+ test_util_message_format_empty);
+ g_test_add_func("/util/message/format/literal",
+ test_util_message_format_literal);
+ g_test_add_func("/util/message/format/variable",
+ test_util_message_format_variable);
+ g_test_add_func("/util/message/format/expression",
+ test_util_message_format_expression);
+ g_test_add_func("/util/message/format/bad",
+ test_util_message_format_bad);
+ g_test_add_func("/util/message/format/escapes",
+ test_util_message_format_escapes);
+ g_test_add_func("/util/message/format/nested",
+ test_util_message_format_nested);
+ g_test_add_func("/util/message/format/everything",
+ test_util_message_format_everything);
+
+ return g_test_run();
+}
diff --git a/util/meson.build b/util/meson.build
index 0982481177b..9e5caca2666 100644
--- a/util/meson.build
+++ b/util/meson.build
@@ -40,6 +40,7 @@ util_ss.add(files('bitmap.c', 'bitops.c'))
util_ss.add(files('fifo8.c'))
util_ss.add(files('cacheflush.c'))
util_ss.add(files('location.c'))
+util_ss.add(files('message-format.c'))
util_ss.add(files('error.c', 'error-report.c'))
util_ss.add(files('qemu-print.c'))
util_ss.add(files('id.c'))
diff --git a/util/message-format.c b/util/message-format.c
new file mode 100644
index 00000000000..df081c5a8af
--- /dev/null
+++ b/util/message-format.c
@@ -0,0 +1,449 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#include "qemu/osdep.h"
+#include "qemu/message-format.h"
+#include "qemu/thread.h"
+#include "qemu/location.h"
+#include "qemu/error-report.h"
+
+
+typedef enum QMessageFormatType {
+ QMESSAGE_FORMAT_TYPE_LITERAL,
+ QMESSAGE_FORMAT_TYPE_VARIABLE,
+ QMESSAGE_FORMAT_TYPE_EXPRESSION,
+} QMessageFormatType;
+
+typedef enum QMessageFormatVariable {
+ QMESSAGE_FORMAT_VAR_UNKNOWN = -1,
+ QMESSAGE_FORMAT_VAR_TIMESTAMP,
+ QMESSAGE_FORMAT_VAR_PROGNAME,
+ QMESSAGE_FORMAT_VAR_THREAD,
+ QMESSAGE_FORMAT_VAR_TID,
+ QMESSAGE_FORMAT_VAR_PID,
+ QMESSAGE_FORMAT_VAR_LOCATION,
+ QMESSAGE_FORMAT_VAR_GUESTNAME,
+ QMESSAGE_FORMAT_VAR_LEVEL,
+ QMESSAGE_FORMAT_VAR_LEVELALT,
+} QMessageFormatVariable;
+
+typedef struct QMessageFormatField {
+ QMessageFormatType type;
+ union {
+ char *literal;
+ QMessageFormatVariable variable;
+ struct {
+ QMessageFormatVariable variable;
+ bool negation;
+ QMessageFormat *format;
+ } expression;
+ } data;
+} QMessageFormatField;
+
+struct QMessageFormat {
+ QMessageFormatField *fields;
+ size_t nfields;
+};
+
+
+static QMessageFormatVariable
+qmessage_format_variable_from_str(const char *formatstr,
+ const char *str,
+ Error **errp)
+{
+ if (g_str_equal(str, "timestamp")) {
+ return QMESSAGE_FORMAT_VAR_TIMESTAMP;
+ } else if (g_str_equal(str, "progname")) {
+ return QMESSAGE_FORMAT_VAR_PROGNAME;
+ } else if (g_str_equal(str, "thread")) {
+ return QMESSAGE_FORMAT_VAR_THREAD;
+ } else if (g_str_equal(str, "tid")) {
+ return QMESSAGE_FORMAT_VAR_TID;
+ } else if (g_str_equal(str, "pid")) {
+ return QMESSAGE_FORMAT_VAR_PID;
+ } else if (g_str_equal(str, "location")) {
+ return QMESSAGE_FORMAT_VAR_LOCATION;
+ } else if (g_str_equal(str, "guestname")) {
+ return QMESSAGE_FORMAT_VAR_GUESTNAME;
+ } else if (g_str_equal(str, "level")) {
+ return QMESSAGE_FORMAT_VAR_LEVEL;
+ } else if (g_str_equal(str, "levelalt")) {
+ return QMESSAGE_FORMAT_VAR_LEVELALT;
+ }
+
+ error_setg(errp, "Unknown message format field '%s' in %s",
+ str, formatstr);
+ return QMESSAGE_FORMAT_VAR_UNKNOWN;
+}
+
+
+static char *qmessage_format_unescape_text(const char *text,
+ size_t textlen)
+{
+ size_t i, n;
+ bool escape = false;
+ char *rawtext = g_new0(char, textlen + 1);
+ for (i = 0, n = 0; i < textlen; i++) {
+ if (escape || text[i] != '\\') {
+ rawtext[n++] = text[i];
+ escape = false;
+ } else {
+ escape = true;
+ }
+ }
+ rawtext[n] = '\0';
+ rawtext = g_renew(char, rawtext, n + 1);
+ return rawtext;
+}
+
+
+static void qmessage_format_add_literal(QMessageFormat *format,
+ const char *text,
+ size_t textlen)
+{
+ format->fields = g_renew(QMessageFormatField,
+ format->fields, format->nfields + 1);
+ format->fields[format->nfields].type = QMESSAGE_FORMAT_TYPE_LITERAL;
+ format->fields[format->nfields].data.literal =
+ qmessage_format_unescape_text(text, textlen);
+ format->nfields++;
+}
+
+
+static void qmessage_format_add_variable(QMessageFormat *format,
+ QMessageFormatVariable variable)
+{
+ format->fields = g_renew(QMessageFormatField,
+ format->fields, format->nfields + 1);
+ format->fields[format->nfields].type = QMESSAGE_FORMAT_TYPE_VARIABLE;
+ format->fields[format->nfields].data.variable = variable;
+ format->nfields++;
+}
+
+
+static void qmessage_format_add_expression(QMessageFormat *format,
+ QMessageFormatVariable variable,
+ bool negation,
+ QMessageFormat *subformat)
+{
+ format->fields = g_renew(QMessageFormatField,
+ format->fields, format->nfields + 1);
+ format->fields[format->nfields].type = QMESSAGE_FORMAT_TYPE_EXPRESSION;
+ format->fields[format->nfields].data.expression.variable = variable;
+ format->fields[format->nfields].data.expression.negation = negation;
+ format->fields[format->nfields].data.expression.format = subformat;
+ format->nfields++;
+}
+
+
+static const char *
+qmessage_format_end_of_literal(const char *formatstr,
+ const char *tmp,
+ char terminator,
+ Error **errp)
+{
+ bool escape = false;
+ while (1) {
+ if (escape) {
+ if (*tmp == '\0') {
+ error_setg(errp,
+ "Unterminated escape at and of format string '%s'",
+ formatstr);
+ return NULL;
+ } else if (*tmp != '\\' && *tmp != '{' && *tmp != '}') {
+ error_setg(errp,
+ "Unrecognised escape char '\\%c' in string '%s'",
+ *tmp, formatstr);
+ return NULL;
+ }
+ escape = false;
+ } else {
+ switch (*tmp) {
+ case '\\':
+ escape = true;
+ break;
+ case '{':
+ case '}':
+ case '\0':
+ return tmp;
+ }
+ }
+ tmp++;
+ }
+ g_assert_not_reached();
+}
+
+
+static QMessageFormat *
+qmessage_format_new_internal(const char *formatstr,
+ const char **tmpptr,
+ char terminator,
+ Error **errp)
+{
+ g_autoptr(QMessageFormat) format = g_new0(QMessageFormat, 1);
+ const char *tmp = *tmpptr;
+
+ /*
+ * We're parsing 'formatstr' which can be a concatenation
+ * of any of the following
+ *
+ * a. literal text:
+ *
+ * text
+ *
+ * b. variable subsitution
+ *
+ * {variable}
+ *
+ * c. expression with literal text
+ *
+ * {?{variable}formatstr}
+ * {!{variable}formatstr}
+ *
+ * Literal 'text' can use '\' to escape '{', '}' or '\'
+ */
+
+ while (tmp && *tmp) {
+ const char *prev = tmp;
+ const char *start;
+ const char *end;
+ bool expression = false;
+ bool negation = false;
+ g_autofree char *varname = NULL;
+ QMessageFormatVariable variable;
+
+ tmp = qmessage_format_end_of_literal(formatstr, tmp, '{', errp);
+ if (!tmp) {
+ return NULL;
+ }
+ if (tmp != prev) {
+ qmessage_format_add_literal(format, prev, tmp - prev);
+ }
+ if (*tmp == terminator) {
+ break;
+ }
+ if (*tmp != '{') {
+ error_setg(errp, "Unterminated format string '%s'",
+ formatstr);
+ return NULL;
+ }
+
+ tmp++; /* skip over '{' */
+
+ if (*tmp == '?' || *tmp == '!') {
+ expression = true;
+ negation = *tmp == '!';
+
+ tmp++; /* skip over '?' / '!' */
+ if (*tmp != '{') {
+ error_setg(
+ errp,
+ "Missing expression variable name in format string '%s'",
+ formatstr);
+ return NULL;
+ }
+ tmp++; /* skip over '{' */
+ }
+
+ start = tmp;
+ while (*tmp && *tmp != '}') {
+ if (!g_ascii_isalpha(*tmp)) {
+ error_setg(
+ errp,
+ "Non-alphanumeric variable name in format string '%s'",
+ formatstr);
+ return NULL;
+ }
+ tmp++;
+ }
+ end = strchr(tmp, '}');
+ if (!end) {
+ error_setg(errp,
+ "Unterminated variable name in format string '%s'",
+ formatstr);
+ return NULL;
+ }
+
+ varname = g_strndup(start, end - start);
+ tmp = end + 1; /* skip over variable name and '}' */
+ variable = qmessage_format_variable_from_str(formatstr, varname, errp);
+ if (variable == QMESSAGE_FORMAT_VAR_UNKNOWN) {
+ return NULL;
+ }
+
+ if (expression) {
+ QMessageFormat *subformat =
+ qmessage_format_new_internal(formatstr, &tmp, '}', errp);
+ if (!subformat) {
+ return NULL;
+ }
+
+ tmp++; /* skip over '}' */
+ qmessage_format_add_expression(format, variable, negation,
+ subformat);
+ } else {
+ qmessage_format_add_variable(format, variable);
+ }
+ }
+
+ *tmpptr = tmp;
+ return g_steal_pointer(&format);
+}
+
+
+QMessageFormat *qmessage_format_new_from_string(const char *formatstr,
+ Error **errp)
+{
+ const char *tmp = formatstr;
+ return qmessage_format_new_internal(formatstr, &tmp, '\0', errp);
+}
+
+
+void qmessage_format_free(QMessageFormat *format)
+{
+ size_t i;
+ if (!format) {
+ return;
+ }
+ for (i = 0; i < format->nfields; i++) {
+ switch (format->fields[i].type) {
+ case QMESSAGE_FORMAT_TYPE_LITERAL:
+ g_free(format->fields[i].data.literal);
+ break;
+ case QMESSAGE_FORMAT_TYPE_EXPRESSION:
+ qmessage_format_free(format->fields[i].data.expression.format);
+ break;
+ default:
+ break;
+ }
+ }
+ g_free(format->fields);
+ g_free(format);
+}
+
+
+void qmessage_format_emit(QMessageFormat *format,
+ QMessageLevel level,
+ QMessageWriteFunc writefn,
+ void *opaque)
+{
+ size_t i;
+
+#define WRITE(str) \
+ writefn(opaque, str, strlen(str))
+
+ for (i = 0; i < format->nfields; i++) {
+ QMessageFormatField *field = &(format->fields[i]);
+ bool empty = false;
+
+ switch (field->type) {
+ case QMESSAGE_FORMAT_TYPE_LITERAL:
+ writefn(opaque, field->data.literal,
+ strlen(field->data.literal));
+ break;
+
+ case QMESSAGE_FORMAT_TYPE_VARIABLE:
+ switch (field->data.variable) {
+ case QMESSAGE_FORMAT_VAR_TIMESTAMP:
+ {
+ g_autoptr(GDateTime) dt = g_date_time_new_now_utc();
+ g_autofree const char *timestr = g_date_time_format_iso8601(dt);
+ writefn(opaque, timestr, strlen(timestr));
+ } break;
+ case QMESSAGE_FORMAT_VAR_PROGNAME:
+ {
+ const char *pgnamestr = g_get_prgname();
+ if (pgnamestr) {
+ writefn(opaque, pgnamestr, strlen(pgnamestr));
+ }
+ } break;
+ case QMESSAGE_FORMAT_VAR_THREAD:
+ {
+ const char *thnamestr = qemu_thread_get_name();
+ if (thnamestr) {
+ writefn(opaque, thnamestr, strlen(thnamestr));
+ }
+ } break;
+ case QMESSAGE_FORMAT_VAR_TID:
+ {
+ char buf[21];
+ unsigned int tid = qemu_get_thread_id();
+ snprintf(buf, sizeof(buf), "%u", tid);
+ writefn(opaque, buf, strlen(buf));
+ } break;
+ case QMESSAGE_FORMAT_VAR_PID:
+ {
+ char buf[21];
+ unsigned int pid = (int)getpid();
+ snprintf(buf, sizeof(buf), "%u", pid);
+ writefn(opaque, buf, strlen(buf));
+ } break;
+ case QMESSAGE_FORMAT_VAR_LOCATION:
+ {
+ g_autofree char *loc = loc_as_string();
+ if (loc) {
+ writefn(opaque, loc, strlen(loc));
+ }
+ } break;
+ case QMESSAGE_FORMAT_VAR_GUESTNAME:
+ {
+ if (error_guest_name) {
+ writefn(opaque, error_guest_name, strlen(error_guest_name));
+ }
+ } break;
+ case QMESSAGE_FORMAT_VAR_LEVEL:
+ case QMESSAGE_FORMAT_VAR_LEVELALT:
+ {
+ const char *str;
+ static const char *levelstr[] = {
+ "debug",
+ "info",
+ "warning",
+ "error",
+ };
+ QEMU_BUILD_BUG_ON(G_N_ELEMENTS(levelstr) !=
+ QMESSAGE_LEVEL__MAX);
+ str = levelstr[level];
+ if ((field->data.variable == QMESSAGE_FORMAT_VAR_LEVEL) ||
+ (level != QMESSAGE_LEVEL_ERROR &&
+ level != QMESSAGE_LEVEL_DEBUG)) {
+ writefn(opaque, str, strlen(str));
+ }
+ break;
+ }
+ default:
+ g_assert_not_reached();
+ }
+ break;
+
+ case QMESSAGE_FORMAT_TYPE_EXPRESSION:
+ switch (field->data.expression.variable) {
+ case QMESSAGE_FORMAT_VAR_PROGNAME:
+ empty = (g_get_prgname() == NULL);
+ break;
+ case QMESSAGE_FORMAT_VAR_THREAD:
+ empty = (qemu_thread_get_name() == NULL);
+ break;
+ case QMESSAGE_FORMAT_VAR_LOCATION:
+ empty = loc_is_none();
+ break;
+ case QMESSAGE_FORMAT_VAR_GUESTNAME:
+ empty = (error_guest_name == NULL);
+ break;
+ case QMESSAGE_FORMAT_VAR_LEVELALT:
+ empty = (level == QMESSAGE_LEVEL_ERROR ||
+ level == QMESSAGE_LEVEL_DEBUG);
+ break;
+ default:
+ empty = false;
+ }
+ if (empty ==
+ field->data.expression.negation) {
+ qmessage_format_emit(field->data.expression.format,
+ level, writefn, opaque);
+ }
+ break;
+ default:
+ g_assert_not_reached();
+ }
+ }
+}
--
2.56.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH 4/6] util: convert log code to support QMessageFormat
2026-12-03 17:25 [PATCH 0/6] util: configurable / flexible error/log message output format Daniel P. Berrangé
` (2 preceding siblings ...)
2026-12-03 17:25 ` [PATCH 3/6] util: introduce a configurable message format template Daniel P. Berrangé
@ 2026-12-03 17:25 ` Daniel P. Berrangé
2026-12-03 21:52 ` Eric Blake
2026-12-03 17:25 ` [PATCH 5/6] util: convert error report code to use QMessageFormat Daniel P. Berrangé
2026-12-03 17:25 ` [PATCH 6/6] util: add '-msg format=FORMATSTR' argument Daniel P. Berrangé
5 siblings, 1 reply; 16+ messages in thread
From: Daniel P. Berrangé @ 2026-12-03 17:25 UTC (permalink / raw)
To: qemu-devel
Cc: Markus Armbruster, Dr. David Alan Gilbert, Marc-André Lureau,
Paolo Bonzini, Daniel P. Berrangé, Peter Maydell,
Richard Henderson
The log code is currently hardcoded to emit a timestamp prefix
based on the '-msg timestamp=on|off' flag. This extends the
API to allow a full message format string to be configured,
with the default being "{timestamp} " when '-msg timestamp=on'
or empty string otherwise.
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
include/qemu/log.h | 3 +++
util/log.c | 32 +++++++++++++++++++++++++++-----
2 files changed, 30 insertions(+), 5 deletions(-)
diff --git a/include/qemu/log.h b/include/qemu/log.h
index 92956e5d0f5..2686e1b9297 100644
--- a/include/qemu/log.h
+++ b/include/qemu/log.h
@@ -3,6 +3,7 @@
/* A small part of this API is split into its own header */
#include "qemu/log-for-trace.h"
+#include "qemu/message-format.h"
/*
* The new API:
@@ -137,4 +138,6 @@ int qemu_str_to_log_mask(const char *str);
*/
void qemu_print_log_usage(FILE *f);
+void qemu_log_set_format(QMessageFormat *format);
+
#endif
diff --git a/util/log.c b/util/log.c
index 7cffbc1bf89..e621f1d1d06 100644
--- a/util/log.c
+++ b/util/log.c
@@ -47,6 +47,8 @@ static __thread Notifier qemu_log_thread_cleanup_notifier;
unsigned qemu_loglevel;
static bool log_per_thread;
static GArray *debug_regions;
+QMessageFormat *log_format;
+
/* Returns true if qemu_log() will really write somewhere. */
bool qemu_log_enabled(void)
@@ -145,14 +147,19 @@ FILE *qemu_log_trylock(void)
return f;
}
+
+static void msg_writefn(void *opaque, const char *str, size_t len)
+{
+ FILE *fp = opaque;
+ ignore_value(fwrite(str, 1, len, fp));
+}
+
+
FILE *qemu_log_trylock_with_context(void)
{
FILE *f = qemu_log_trylock();
- if (f && log_depth == 1 && message_with_timestamp) {
- g_autofree const char *timestr = NULL;
- g_autoptr(GDateTime) dt = g_date_time_new_now_utc();
- timestr = g_date_time_format_iso8601(dt);
- fprintf(f, "%s ", timestr);
+ if (f && log_depth == 1) {
+ qmessage_format_emit(log_format, QMESSAGE_LEVEL_DEBUG, msg_writefn, f);
}
return f;
}
@@ -229,6 +236,16 @@ valid_filename_template(const char *filename, bool per_thread, Error **errp)
return filename ? vft_strdup : vft_stderr;
}
+
+void qemu_log_set_format(QMessageFormat *format)
+{
+ QEMU_LOCK_GUARD(&global_mutex);
+
+ qmessage_format_free(log_format);
+ log_format = format;
+}
+
+
/* enable or disable low levels log */
static bool qemu_set_log_internal(const char *filename, bool changed_name,
int log_flags, Error **errp)
@@ -241,6 +258,11 @@ static bool qemu_set_log_internal(const char *filename, bool changed_name,
QEMU_LOCK_GUARD(&global_mutex);
logfile = global_file;
+ if (!log_format) {
+ log_format = qmessage_format_new_from_string(
+ message_with_timestamp ? "{timestamp} " : NULL, errp);
+ }
+
/* The per-thread flag is immutable. */
if (log_per_thread) {
log_flags |= LOG_PER_THREAD;
--
2.56.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH 5/6] util: convert error report code to use QMessageFormat
2026-12-03 17:25 [PATCH 0/6] util: configurable / flexible error/log message output format Daniel P. Berrangé
` (3 preceding siblings ...)
2026-12-03 17:25 ` [PATCH 4/6] util: convert log code to support QMessageFormat Daniel P. Berrangé
@ 2026-12-03 17:25 ` Daniel P. Berrangé
2026-12-03 21:55 ` Eric Blake
2026-12-03 17:25 ` [PATCH 6/6] util: add '-msg format=FORMATSTR' argument Daniel P. Berrangé
5 siblings, 1 reply; 16+ messages in thread
From: Daniel P. Berrangé @ 2026-12-03 17:25 UTC (permalink / raw)
To: qemu-devel
Cc: Markus Armbruster, Dr. David Alan Gilbert, Marc-André Lureau,
Paolo Bonzini, Daniel P. Berrangé, Peter Maydell,
Richard Henderson
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
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH 6/6] util: add '-msg format=FORMATSTR' argument
2026-12-03 17:25 [PATCH 0/6] util: configurable / flexible error/log message output format Daniel P. Berrangé
` (4 preceding siblings ...)
2026-12-03 17:25 ` [PATCH 5/6] util: convert error report code to use QMessageFormat Daniel P. Berrangé
@ 2026-12-03 17:25 ` Daniel P. Berrangé
2026-12-03 22:02 ` Eric Blake
5 siblings, 1 reply; 16+ messages in thread
From: Daniel P. Berrangé @ 2026-12-03 17:25 UTC (permalink / raw)
To: qemu-devel
Cc: Markus Armbruster, Dr. David Alan Gilbert, Marc-André Lureau,
Paolo Bonzini, Daniel P. Berrangé, Peter Maydell,
Richard Henderson
Allow the prefix for error messages and log messages to be runtime
configurable by introducing the '-msg format=FORMATSTR' argument.
This accepts a QMessageFormat templated string, and if set, the
existing 'timestamp' and 'guest-name' options to -msg must not
be used.
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
qemu-options.hx | 72 ++++++++++++++++++++++++++++++++++++++++++++++---
system/vl.c | 35 ++++++++++++++++++++++--
2 files changed, 102 insertions(+), 5 deletions(-)
diff --git a/qemu-options.hx b/qemu-options.hx
index 5fe9d73eaa3..01e9909cb72 100644
--- a/qemu-options.hx
+++ b/qemu-options.hx
@@ -5730,22 +5730,88 @@ ERST
#endif
DEF("msg", HAS_ARG, QEMU_OPTION_msg,
- "-msg [timestamp=on|off][,guest-name=on|off]\n"
+ "-msg [timestamp=on|off][,guest-name=on|off][format=FORMATSTR]\n"
" control error message format\n"
" timestamp=on enables timestamps (default: off)\n"
" guest-name=on enables guest name prefix but only if\n"
- " -name guest option is set (default: off)\n",
+ " -name guest option is set (default: off)\n"
+ " format=FORMATSTR configure the message format string\n"
+ " for logs and errors\n",
QEMU_ARCH_ALL)
SRST
-``-msg [timestamp=on|off][,guest-name=on|off]``
+``-msg [timestamp=on|off][,guest-name=on|off][,format=FORMATSTR]``
Control error message format.
+ The simple historical options are:
+
``timestamp=on|off``
Prefix messages with a timestamp. Default is off.
``guest-name=on|off``
Prefix messages with guest name but only if -name guest option is set
otherwise the option is ignored. Default is off.
+
+ These are mutually exclusive with the new more flexible
+ format string option:
+
+ ``format=FORMATSTR``
+ Prefix log and error messages with a string constructed from FMTSTR.
+
+ A ``FORMATSTR`` string is a concatenation of any of the following:
+
+ ``literal-text``
+ Any literal text string, except '{', '}' and '\' must be
+ escaped with a leading '\'. A comma must also be escaped
+ from the option parsing by doubling it up.
+
+ ``{variable}``
+ One of the named data items listed below.
+
+ ``{?{variable}FORMATSTR}``
+ A conditional output testing whether a variable is
+ set, and outputting ``FORMATSTR`` if true.
+
+ ``{!{variable}FORMATSTR}``
+ A conditional output testing whether a variable is
+ unset, and outputting ``FORMATSTR`` if true.
+
+ Valid variable names are:
+
+ ``timestamp``
+ The ISO 8601 formatted current time.
+
+ ``progname``
+ The current program name.
+
+ ``thread``
+ The current thread name.
+
+ ``tid``
+ The thread ID.
+
+ ``pid``
+ The process ID.
+
+ ``location``
+ The current input location.
+
+ ``guestname``
+ The current guest mame.
+
+ ``level``
+ The message severity level.
+
+ ``levelalt``
+ The message severity level, but empty for error or debug.
+
+ The default built-in format is:
+
+ ``"{progname}{?{progname}: }{location}{?{location}: }{levelalt}{?{levelalt}: }"``
+
+ If ``timestamp=on`` and ``guest-name=on`` are given, then
+ the above default format is further prefixed with:
+
+ ``"{timestamp} {guestname} "``
ERST
DEF("dump-vmstate", HAS_ARG, QEMU_OPTION_dump_vmstate,
diff --git a/system/vl.c b/system/vl.c
index 7ae6a8298fc..f938b3a3fe0 100644
--- a/system/vl.c
+++ b/system/vl.c
@@ -383,6 +383,11 @@ static QemuOptsList qemu_msg_opts = {
.help = "Prepends guest name for error messages but only if "
"-name guest is set otherwise option is ignored\n",
},
+ {
+ .name = "format",
+ .type = QEMU_OPT_STRING,
+ .help = "The message prefix format string for errors and logs",
+ },
{ /* end of list */ }
},
};
@@ -819,10 +824,34 @@ static void realtime_init(void)
}
-static void configure_msg(QemuOpts *opts)
+static bool configure_msg(QemuOpts *opts)
{
+ Error *local_err = NULL;
+ const char *formatstr = qemu_opt_get(opts, "format");
message_with_timestamp = qemu_opt_get_bool(opts, "timestamp", false);
error_with_guestname = qemu_opt_get_bool(opts, "guest-name", false);
+
+ if (formatstr && (message_with_timestamp || error_with_guestname)) {
+ error_report("-msg 'format' is mutually exclusive with "
+ "'timestamp' and 'guest-name'");
+ return false;
+ }
+
+ if (formatstr) {
+ QMessageFormat *format =
+ qmessage_format_new_from_string(formatstr, &local_err);
+ if (!format) {
+ error_report_err(local_err);
+ return false;
+ }
+ qemu_log_set_format(format); /* takes ownership of format */
+
+ format =
+ qmessage_format_new_from_string(formatstr, &error_abort);
+ error_set_format(format);
+ }
+
+ return true;
}
@@ -3693,7 +3722,9 @@ void qemu_init(int argc, char **argv)
if (!opts) {
exit(1);
}
- configure_msg(opts);
+ if (!configure_msg(opts)) {
+ exit(1);
+ }
break;
case QEMU_OPTION_dump_vmstate:
if (vmstate_dump_file) {
--
2.56.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* Re: [PATCH 1/6] util: pull Location struct and APIs into location.{c,h}
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
0 siblings, 0 replies; 16+ messages in thread
From: Eric Blake @ 2026-12-03 20:51 UTC (permalink / raw)
To: Daniel P. Berrangé
Cc: qemu-devel, Markus Armbruster, Dr. David Alan Gilbert,
Marc-André Lureau, Paolo Bonzini, Peter Maydell,
Richard Henderson
On Thu, Dec 03, 2026 at 05:25:38PM +0000, Daniel P. Berrangé wrote:
> The Location struct and APIs are currently alongside the
> error report code, since the primary use of Location was
> as error reporting.
>
> We need to reference the location APIs from the new
> message output APIs, but the latter is in turn going to
> be referenced from the error report APIs which sets up
> a circular loop. Splitting off the location APIs keeps
> the code loop-free.
>
> The new files are put under the existing MAINTAINERS
> file entry for error reporting.
>
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
> +++ b/include/qemu/error-report.h
> @@ -13,22 +13,7 @@
> #ifndef QEMU_ERROR_REPORT_H
> #define QEMU_ERROR_REPORT_H
>
> -typedef struct Location {
> - /* all members are private to qemu-error.c */
The comment was correct in its old location,
> +++ b/include/qemu/location.h
> @@ -0,0 +1,24 @@
> +/* SPDX-License-Identifier: GPL-2.0-or-later */
> +
> +#ifndef QEMU_LOCATION_H
> +#define QEMU_LOCATION_H
> +
> +typedef struct Location {
> + /* all members are private to qemu-error.c */
...but needs a tweak in this relocation.
With that fixed,
Reviewed-by: Eric Blake <eblake@redhat.com>
--
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization: qemu.org | libguestfs.org
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH 2/6] util: add helper to query if a location is set
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
0 siblings, 0 replies; 16+ messages in thread
From: Eric Blake @ 2026-12-03 20:51 UTC (permalink / raw)
To: Daniel P. Berrangé
Cc: qemu-devel, Markus Armbruster, Dr. David Alan Gilbert,
Marc-André Lureau, Paolo Bonzini, Peter Maydell,
Richard Henderson
On Thu, Dec 03, 2026 at 05:25:39PM +0000, Daniel P. Berrangé wrote:
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
> include/qemu/location.h | 1 +
> util/location.c | 6 ++++++
> 2 files changed, 7 insertions(+)
Reviewed-by: Eric Blake <eblake@redhat.com>
--
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization: qemu.org | libguestfs.org
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH 3/6] util: introduce a configurable message format template
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
1 sibling, 1 reply; 16+ messages in thread
From: Eric Blake @ 2026-12-03 21:44 UTC (permalink / raw)
To: Daniel P. Berrangé
Cc: qemu-devel, Markus Armbruster, Dr. David Alan Gilbert,
Marc-André Lureau, Paolo Bonzini, Peter Maydell,
Richard Henderson
On Thu, Dec 03, 2026 at 05:25:40PM +0000, Daniel P. Berrangé wrote:
> Currently error messages, generic log messages and chardev logs
> can all have a timestamp prepended, by using the '-msg' argument's
> 'timestamp' flag.
>
> There are various extra bits of information that are interesting
> to emit in various scenarios. Rather than adding many more flags
> to '-msg' and hardcoding the order they are emitted in, this
> introcuces a configuraable message format template.
introduces, configurable
>
> A format string template is a concatenation of the following
> components
>
> * literal string:
>
> text
>
> In the literal text, a '\' must be used to escape any '{',
> '}' or '\' characters.
>
> * a variable substition:
>
> {variablename}
>
> where variable name is one of:
>
> - 'timestamp': the ISO 8601 formatted current time
> - 'progname': the current program name
> - 'thread': the current thread name
> - 'tid': the thread ID
> - 'pid': the process ID
> - 'location': the current input location
Missing mention of 'guestname', 'level', and 'levelalt' from the
implementation [1]. Maybe it is sufficient to have the commit message
point to the .h file and maybe just summarize things with a subset,
rather than trying to duplicate the list in two places then ending up
inconsistent.
>
> * an conditional expression
s/an /a /
>
> {?{variablename}formatstring}
> {!{variablename}formatstring}
>
> which evaluates whether the variable is non-empty or empty
> respectively, and processes the nested format string.
>
> As example:
>
> "Demo {pid}{?{progname} (in program '}{progname}{?{progname}')}"
>
> Given a 'pid' value of '123' will print the following
> if progname is empty:
>
> "Demo 123"
>
> or, if the program name is the strnig 'qemu', will print:
string
>
> "Demo text (in program 'qemu')"
s/text/123/ to match the earlier explanation.
>
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
> +++ b/include/qemu/message-format.h
> @@ -0,0 +1,97 @@
...
> + * In either the literal text, or the prefix and suffix
> + * of a conditional placeholder, '\' can be used to
> + * escape the special meaning of '{', '}' and '\'.
> + *
> + * As example:
> + *
> + * "Demo {pid}{? (in program '{progname}')}"
> + *
> + * Given a 'pid' value of '123' will print
> + *
> + * "Demo 123"
> + *
> + * or, if the program name is available, will print:
> + *
> + * "Demo text (in program 'qemu')"
s/text/123/
> + *
> + * Currently defined placeholders are:
> + *
> + * - 'timestamp': the ISO 8601 formatted current time
> + * - 'progname': the current program name
> + * - 'thread': the current thread name
> + * - 'tid': the thread ID
> + * - 'pid': the process ID
> + * - 'location': the current input location
> + * - 'guestname': the current guest name
> + * - 'level': the message level (debug, info, warning, error)
> + * - 'levelalt': as 'level' but no string printed for 'error'
> + * or 'debug'
[1] see comment above about commit message.
> + *
> + * Returns: a new message format, or NULL on error
> + */
> +QMessageFormat *qmessage_format_new_from_string(const char *formatstr,
> + Error **errp);
...
> +++ b/tests/unit/test-util-message-format.c
> @@ -0,0 +1,226 @@
> +/* SPDX-License-Identifier: GPL-2.0-or-later */
> +
> +#include "qemu/osdep.h"
> +#include "qemu/message-format.h"
> +#include "qemu/location.h"
> +#include "qemu/error-report.h"
> +
> +
> +/*
> + * functions we "mock" override to give predictable
> + * output for testing
> + */
> +GDateTime *g_date_time_new_now_utc(void)
> +{
> + return g_date_time_new_from_unix_utc(872835240);
Anything special about that date in 1997? :)
> +static void test_util_message_format_escapes(void)
> +{
> + g_autoptr(GString) str = g_string_new(NULL);
> + g_autoptr(QMessageFormat) format =
> + qmessage_format_new_from_string(
> + "Test: {progname}{?{progname}\\\\\\}\\\\}!", &error_abort);
Leaning toothpicks! But literal \ in error strings is rare enough that
I don't mind this choice of escape.
> +
> +static void test_util_message_format_everything(void)
> +{
> + g_autoptr(GString) str = g_string_new(NULL);
> + g_autoptr(QMessageFormat) format =
> + qmessage_format_new_from_string(
> + "{timestamp}: [{level}] {progname}:"
> + "{location}({pid}:[{thread}:{tid}]): {guestname}: OK",
> + &error_abort);
Misses {levelalt}. But I'm liking the flexibility.
test_util_message_format_bad() covers incomplete strings, like
"{timestamp" (no terminating "}"); but should it also cover the case of
an unrecognized variable, such as "{bogus}"?
> +++ b/util/message-format.c
> @@ -0,0 +1,449 @@
> +
> +static char *qmessage_format_unescape_text(const char *text,
> + size_t textlen)
> +{
> + size_t i, n;
> + bool escape = false;
> + char *rawtext = g_new0(char, textlen + 1);
> + for (i = 0, n = 0; i < textlen; i++) {
> + if (escape || text[i] != '\\') {
> + rawtext[n++] = text[i];
> + escape = false;
> + } else {
> + escape = true;
> + }
> + }
Technically, this means \ escapes any character, not just \,{,} as
documented. If we want to add other escape sequences later (unlikely?)
then being noisy on unknown escape sequences now would make later
additions less likely to change behavior if they are accidentally
used; but costs more validation effort. I'm inclined to leave it the
way you have it.
> +static const char *
> +qmessage_format_end_of_literal(const char *formatstr,
> + const char *tmp,
> + char terminator,
> + Error **errp)
> +{
> + bool escape = false;
> + while (1) {
> + if (escape) {
> + if (*tmp == '\0') {
> + error_setg(errp,
> + "Unterminated escape at and of format string '%s'",
s/and/end/
> + formatstr);
> + return NULL;
> + } else if (*tmp != '\\' && *tmp != '{' && *tmp != '}') {
> + error_setg(errp,
> + "Unrecognised escape char '\\%c' in string '%s'",
> + *tmp, formatstr);
> + return NULL;
> + }
> + escape = false;
While here, you are checking for known escapes.
> + } else {
> + switch (*tmp) {
> + case '\\':
> + escape = true;
> + break;
> + case '{':
> + case '}':
> + case '\0':
> + return tmp;
> + }
> + }
> + tmp++;
> + }
> + g_assert_not_reached();
> +}
> +
> +
> +static QMessageFormat *
> +qmessage_format_new_internal(const char *formatstr,
> + const char **tmpptr,
> + char terminator,
> + Error **errp)
> +{
> + g_autoptr(QMessageFormat) format = g_new0(QMessageFormat, 1);
> + const char *tmp = *tmpptr;
> +
> + /*
> + * We're parsing 'formatstr' which can be a concatenation
> + * of any of the following
> + *
> + * a. literal text:
> + *
> + * text
> + *
> + * b. variable subsitution
substitution
> + *
> + * {variable}
> + *
> + * c. expression with literal text
> + *
> + * {?{variable}formatstr}
> + * {!{variable}formatstr}
> + *
> + * Literal 'text' can use '\' to escape '{', '}' or '\'
> + */
> +
> +void qmessage_format_emit(QMessageFormat *format,
> + QMessageLevel level,
> + QMessageWriteFunc writefn,
> + void *opaque)
> +{
> + size_t i;
> +
> +#define WRITE(str) \
> + writefn(opaque, str, strlen(str))
Looks funny to define this...
> +
> + for (i = 0; i < format->nfields; i++) {
> + QMessageFormatField *field = &(format->fields[i]);
> + bool empty = false;
> +
> + switch (field->type) {
> + case QMESSAGE_FORMAT_TYPE_LITERAL:
> + writefn(opaque, field->data.literal,
> + strlen(field->data.literal));
...and then not use it as WRITE(field->data.literal);
--
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization: qemu.org | libguestfs.org
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH 4/6] util: convert log code to support QMessageFormat
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
0 siblings, 0 replies; 16+ messages in thread
From: Eric Blake @ 2026-12-03 21:52 UTC (permalink / raw)
To: Daniel P. Berrangé
Cc: qemu-devel, Markus Armbruster, Dr. David Alan Gilbert,
Marc-André Lureau, Paolo Bonzini, Peter Maydell,
Richard Henderson
On Thu, Dec 03, 2026 at 05:25:41PM +0000, Daniel P. Berrangé wrote:
> The log code is currently hardcoded to emit a timestamp prefix
> based on the '-msg timestamp=on|off' flag. This extends the
> API to allow a full message format string to be configured,
> with the default being "{timestamp} " when '-msg timestamp=on'
> or empty string otherwise.
>
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
> include/qemu/log.h | 3 +++
> util/log.c | 32 +++++++++++++++++++++++++++-----
> 2 files changed, 30 insertions(+), 5 deletions(-)
>
Reviewed-by: Eric Blake <eblake@redhat.com>
--
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization: qemu.org | libguestfs.org
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH 5/6] util: convert error report code to use QMessageFormat
2026-12-03 17:25 ` [PATCH 5/6] util: convert error report code to use QMessageFormat Daniel P. Berrangé
@ 2026-12-03 21:55 ` Eric Blake
0 siblings, 0 replies; 16+ messages in thread
From: Eric Blake @ 2026-12-03 21:55 UTC (permalink / raw)
To: Daniel P. Berrangé
Cc: qemu-devel, Markus Armbruster, Dr. David Alan Gilbert,
Marc-André Lureau, Paolo Bonzini, Peter Maydell,
Richard Henderson
On Thu, Dec 03, 2026 at 05:25:42PM +0000, Daniel P. Berrangé wrote:
> 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>
> ---
Reviewed-by: Eric Blake <eblake@redhat.com>
--
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization: qemu.org | libguestfs.org
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH 6/6] util: add '-msg format=FORMATSTR' argument
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é
0 siblings, 1 reply; 16+ messages in thread
From: Eric Blake @ 2026-12-03 22:02 UTC (permalink / raw)
To: Daniel P. Berrangé
Cc: qemu-devel, Markus Armbruster, Dr. David Alan Gilbert,
Marc-André Lureau, Paolo Bonzini, Peter Maydell,
Richard Henderson
On Thu, Dec 03, 2026 at 05:25:43PM +0000, Daniel P. Berrangé wrote:
> Allow the prefix for error messages and log messages to be runtime
> configurable by introducing the '-msg format=FORMATSTR' argument.
> This accepts a QMessageFormat templated string, and if set, the
> existing 'timestamp' and 'guest-name' options to -msg must not
> be used.
>
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
> qemu-options.hx | 72 ++++++++++++++++++++++++++++++++++++++++++++++---
> system/vl.c | 35 ++++++++++++++++++++++--
> 2 files changed, 102 insertions(+), 5 deletions(-)
>
> +++ b/qemu-options.hx
> +``-msg [timestamp=on|off][,guest-name=on|off][,format=FORMATSTR]``
> Control error message format.
>
> + The simple historical options are:
> +
> ``timestamp=on|off``
> Prefix messages with a timestamp. Default is off.
>
> ``guest-name=on|off``
> Prefix messages with guest name but only if -name guest option is set
> otherwise the option is ignored. Default is off.
> +
> + These are mutually exclusive with the new more flexible
> + format string option:
Mutually exclusive, yet...
> +
> + ``format=FORMATSTR``
> + Prefix log and error messages with a string constructed from FMTSTR.
> +
> + A ``FORMATSTR`` string is a concatenation of any of the following:
...
> + The default built-in format is:
> +
> + ``"{progname}{?{progname}: }{location}{?{location}: }{levelalt}{?{levelalt}: }"``
> +
> + If ``timestamp=on`` and ``guest-name=on`` are given, then
> + the above default format is further prefixed with:
> +
> + ``"{timestamp} {guestname} "``
...this could be implied to mean that they can still be used. Maybe
word it along the lines of "The default built-in format, when
FORMATSTR is not specified, is:".
> +++ b/system/vl.c
> +static bool configure_msg(QemuOpts *opts)
> {
> + Error *local_err = NULL;
> + const char *formatstr = qemu_opt_get(opts, "format");
> message_with_timestamp = qemu_opt_get_bool(opts, "timestamp", false);
> error_with_guestname = qemu_opt_get_bool(opts, "guest-name", false);
> +
> + if (formatstr && (message_with_timestamp || error_with_guestname)) {
> + error_report("-msg 'format' is mutually exclusive with "
> + "'timestamp' and 'guest-name'");
> + return false;
> + }
At least you are diagnosing it up front, so it doesn't cause confusing
output later.
> +
> + if (formatstr) {
Two conditionals in a row gated by formatstr; is it worth moving 'if
(message_with_timestamp || error_with_guestname)' inside the one 'if
(formatstr)' block?
--
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization: qemu.org | libguestfs.org
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH 3/6] util: introduce a configurable message format template
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 0:32 ` Dr. David Alan Gilbert
1 sibling, 0 replies; 16+ messages in thread
From: Dr. David Alan Gilbert @ 2026-12-04 0:32 UTC (permalink / raw)
To: Daniel P. Berrangé
Cc: qemu-devel, Markus Armbruster, Marc-André Lureau,
Paolo Bonzini, Peter Maydell, Richard Henderson
* Daniel P. Berrangé (berrange@redhat.com) wrote:
> + * Currently defined placeholders are:
> + *
> + * - 'timestamp': the ISO 8601 formatted current time
Should this state the format and whether it is localtime
or UTC (I hope!).
Dave
--
-----Open up your eyes, open up your mind, open up your code -------
/ Dr. David Alan Gilbert | Running GNU/Linux | Happy \
\ dave @ treblig.org | | In Hex /
\ _________________________|_____ http://www.treblig.org |_______/
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH 3/6] util: introduce a configurable message format template
2026-12-03 21:44 ` Eric Blake
@ 2026-12-04 9:22 ` Daniel P. Berrangé
0 siblings, 0 replies; 16+ messages in thread
From: Daniel P. Berrangé @ 2026-12-04 9:22 UTC (permalink / raw)
To: Eric Blake
Cc: qemu-devel, Markus Armbruster, Dr. David Alan Gilbert,
Marc-André Lureau, Paolo Bonzini, Peter Maydell,
Richard Henderson
On Thu, Dec 03, 2026 at 03:44:08PM -0600, Eric Blake wrote:
> On Thu, Dec 03, 2026 at 05:25:40PM +0000, Daniel P. Berrangé wrote:
> > Currently error messages, generic log messages and chardev logs
> > can all have a timestamp prepended, by using the '-msg' argument's
> > 'timestamp' flag.
> >
> > There are various extra bits of information that are interesting
> > to emit in various scenarios. Rather than adding many more flags
> > to '-msg' and hardcoding the order they are emitted in, this
> > introcuces a configuraable message format template.
>
> introduces, configurable
>
> >
> > A format string template is a concatenation of the following
> > components
> >
> > * literal string:
> >
> > text
> >
> > In the literal text, a '\' must be used to escape any '{',
> > '}' or '\' characters.
> >
> > * a variable substition:
> >
> > {variablename}
> >
> > where variable name is one of:
> >
> > - 'timestamp': the ISO 8601 formatted current time
> > - 'progname': the current program name
> > - 'thread': the current thread name
> > - 'tid': the thread ID
> > - 'pid': the process ID
> > - 'location': the current input location
>
> Missing mention of 'guestname', 'level', and 'levelalt' from the
> implementation [1]. Maybe it is sufficient to have the commit message
> point to the .h file and maybe just summarize things with a subset,
> rather than trying to duplicate the list in two places then ending up
> inconsistent.
It is triplicated actually, as I added it to qemu-options.hx too
in the final patch 6 :-)
Yes, it is overkill to have it in the commit message.
> > +/*
> > + * functions we "mock" override to give predictable
> > + * output for testing
> > + */
> > +GDateTime *g_date_time_new_now_utc(void)
> > +{
> > + return g_date_time_new_from_unix_utc(872835240);
>
> Anything special about that date in 1997? :)
Lets just say the magic numbers in this commit all share
a common theme :-)
> > +static void test_util_message_format_escapes(void)
> > +{
> > + g_autoptr(GString) str = g_string_new(NULL);
> > + g_autoptr(QMessageFormat) format =
> > + qmessage_format_new_from_string(
> > + "Test: {progname}{?{progname}\\\\\\}\\\\}!", &error_abort);
>
> Leaning toothpicks! But literal \ in error strings is rare enough that
> I don't mind this choice of escape.
And this only looks so awful because we're doing it in C
code.
In the shell, one would just use single quotes to avoid the
need to escape the escapes, so real usage would be
-msg format='Test: {progname}{?{progname}\\\}\\}!'
which would result in output
Test: qemu\}\
> > +static void test_util_message_format_everything(void)
> > +{
> > + g_autoptr(GString) str = g_string_new(NULL);
> > + g_autoptr(QMessageFormat) format =
> > + qmessage_format_new_from_string(
> > + "{timestamp}: [{level}] {progname}:"
> > + "{location}({pid}:[{thread}:{tid}]): {guestname}: OK",
> > + &error_abort);
>
> Misses {levelalt}. But I'm liking the flexibility.
Ah yes.
>
> test_util_message_format_bad() covers incomplete strings, like
> "{timestamp" (no terminating "}"); but should it also cover the case of
> an unrecognized variable, such as "{bogus}"?
Sure, I can add such example
> > +static char *qmessage_format_unescape_text(const char *text,
> > + size_t textlen)
> > +{
> > + size_t i, n;
> > + bool escape = false;
> > + char *rawtext = g_new0(char, textlen + 1);
> > + for (i = 0, n = 0; i < textlen; i++) {
> > + if (escape || text[i] != '\\') {
> > + rawtext[n++] = text[i];
> > + escape = false;
> > + } else {
> > + escape = true;
> > + }
> > + }
>
> Technically, this means \ escapes any character, not just \,{,} as
> documented. If we want to add other escape sequences later (unlikely?)
> then being noisy on unknown escape sequences now would make later
> additions less likely to change behavior if they are accidentally
> used; but costs more validation effort. I'm inclined to leave it the
> way you have it.
We don't need to validate in this method, because it should
only be reachable after a prior call to the next method below
which does fuller validation. I'll add a comment to explain this
less obvious dependency
>
> > +static const char *
> > +qmessage_format_end_of_literal(const char *formatstr,
> > + const char *tmp,
> > + char terminator,
> > + Error **errp)
> > +{
> > + bool escape = false;
> > + while (1) {
> > + if (escape) {
> > + if (*tmp == '\0') {
> > + error_setg(errp,
> > + "Unterminated escape at and of format string '%s'",
>
> s/and/end/
>
> > + formatstr);
> > + return NULL;
> > + } else if (*tmp != '\\' && *tmp != '{' && *tmp != '}') {
> > + error_setg(errp,
> > + "Unrecognised escape char '\\%c' in string '%s'",
> > + *tmp, formatstr);
> > + return NULL;
> > + }
> > + escape = false;
>
> While here, you are checking for known escapes.
Yep, this is what avoids the need for the other method to validate
>
> > + } else {
> > + switch (*tmp) {
> > + case '\\':
> > + escape = true;
> > + break;
> > + case '{':
> > + case '}':
> > + case '\0':
> > + return tmp;
> > + }
> > + }
> > + tmp++;
> > + }
> > + g_assert_not_reached();
> > +}
> > +
> > +void qmessage_format_emit(QMessageFormat *format,
> > + QMessageLevel level,
> > + QMessageWriteFunc writefn,
> > + void *opaque)
> > +{
> > + size_t i;
> > +
> > +#define WRITE(str) \
> > + writefn(opaque, str, strlen(str))
>
> Looks funny to define this...
>
> > +
> > + for (i = 0; i < format->nfields; i++) {
> > + QMessageFormatField *field = &(format->fields[i]);
> > + bool empty = false;
> > +
> > + switch (field->type) {
> > + case QMESSAGE_FORMAT_TYPE_LITERAL:
> > + writefn(opaque, field->data.literal,
> > + strlen(field->data.literal));
>
> ...and then not use it as WRITE(field->data.literal);
WRITE() is obsolete cruft. In an earlier draft the code was more
complicated, justifying the macro but it doesn't add benefit in
the final posting. I'll delete the unused macro.
With regards,
Daniel
--
|: https://berrange.com ~~ https://hachyderm.io/@berrange :|
|: https://libvirt.org ~~ https://entangle-photo.org :|
|: https://pixelfed.art/berrange ~~ https://fstop138.berrange.com :|
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH 6/6] util: add '-msg format=FORMATSTR' argument
2026-12-03 22:02 ` Eric Blake
@ 2026-12-04 9:23 ` Daniel P. Berrangé
0 siblings, 0 replies; 16+ messages in thread
From: Daniel P. Berrangé @ 2026-12-04 9:23 UTC (permalink / raw)
To: Eric Blake
Cc: qemu-devel, Markus Armbruster, Dr. David Alan Gilbert,
Marc-André Lureau, Paolo Bonzini, Peter Maydell,
Richard Henderson
On Thu, Dec 03, 2026 at 04:02:50PM -0600, Eric Blake wrote:
> On Thu, Dec 03, 2026 at 05:25:43PM +0000, Daniel P. Berrangé wrote:
> > Allow the prefix for error messages and log messages to be runtime
> > configurable by introducing the '-msg format=FORMATSTR' argument.
> > This accepts a QMessageFormat templated string, and if set, the
> > existing 'timestamp' and 'guest-name' options to -msg must not
> > be used.
> >
> > Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> > ---
> > qemu-options.hx | 72 ++++++++++++++++++++++++++++++++++++++++++++++---
> > system/vl.c | 35 ++++++++++++++++++++++--
> > 2 files changed, 102 insertions(+), 5 deletions(-)
> >
>
> > +++ b/qemu-options.hx
>
> > +``-msg [timestamp=on|off][,guest-name=on|off][,format=FORMATSTR]``
> > Control error message format.
> >
> > + The simple historical options are:
> > +
> > ``timestamp=on|off``
> > Prefix messages with a timestamp. Default is off.
> >
> > ``guest-name=on|off``
> > Prefix messages with guest name but only if -name guest option is set
> > otherwise the option is ignored. Default is off.
> > +
> > + These are mutually exclusive with the new more flexible
> > + format string option:
>
> Mutually exclusive, yet...
>
> > +
> > + ``format=FORMATSTR``
> > + Prefix log and error messages with a string constructed from FMTSTR.
> > +
> > + A ``FORMATSTR`` string is a concatenation of any of the following:
> ...
> > + The default built-in format is:
> > +
> > + ``"{progname}{?{progname}: }{location}{?{location}: }{levelalt}{?{levelalt}: }"``
> > +
> > + If ``timestamp=on`` and ``guest-name=on`` are given, then
> > + the above default format is further prefixed with:
> > +
> > + ``"{timestamp} {guestname} "``
>
> ...this could be implied to mean that they can still be used. Maybe
> word it along the lines of "The default built-in format, when
> FORMATSTR is not specified, is:".
Hmmm, yes, that is poorly worded currently, I'll fix it.
>
> > +++ b/system/vl.c
>
> > +static bool configure_msg(QemuOpts *opts)
> > {
> > + Error *local_err = NULL;
> > + const char *formatstr = qemu_opt_get(opts, "format");
> > message_with_timestamp = qemu_opt_get_bool(opts, "timestamp", false);
> > error_with_guestname = qemu_opt_get_bool(opts, "guest-name", false);
> > +
> > + if (formatstr && (message_with_timestamp || error_with_guestname)) {
> > + error_report("-msg 'format' is mutually exclusive with "
> > + "'timestamp' and 'guest-name'");
> > + return false;
> > + }
>
> At least you are diagnosing it up front, so it doesn't cause confusing
> output later.
>
> > +
> > + if (formatstr) {
>
> Two conditionals in a row gated by formatstr; is it worth moving 'if
> (message_with_timestamp || error_with_guestname)' inside the one 'if
> (formatstr)' block?
Yes.
With regards,
Daniel
--
|: https://berrange.com ~~ https://hachyderm.io/@berrange :|
|: https://libvirt.org ~~ https://entangle-photo.org :|
|: https://pixelfed.art/berrange ~~ https://fstop138.berrange.com :|
^ permalink raw reply [flat|nested] 16+ messages in thread
end of thread, other threads:[~2026-12-04 9:24 UTC | newest]
Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH 5/6] util: convert error report code to use QMessageFormat Daniel P. Berrangé
2026-12-03 21:55 ` 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é
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.