From: "Daniel P. Berrangé" <berrange@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Markus Armbruster" <armbru@redhat.com>,
"Dr. David Alan Gilbert" <dave@treblig.org>,
"Marc-André Lureau" <marcandre.lureau@redhat.com>,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Daniel P. Berrangé" <berrange@redhat.com>,
"Peter Maydell" <peter.maydell@linaro.org>,
"Richard Henderson" <richard.henderson@linaro.org>
Subject: [PATCH 6/6] util: add '-msg format=FORMATSTR' argument
Date: Thu, 3 Dec 2026 17:25:43 +0000 [thread overview]
Message-ID: <20261203172543.676434-7-berrange@redhat.com> (raw)
In-Reply-To: <20261203172543.676434-1-berrange@redhat.com>
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
next prev parent reply other threads:[~2026-12-03 17:27 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-03 17:25 [PATCH 0/6] util: configurable / flexible error/log message output format Daniel P. Berrangé
2026-12-03 17:25 ` [PATCH 1/6] util: pull Location struct and APIs into location.{c,h} Daniel P. Berrangé
2026-12-03 20:51 ` Eric Blake
2026-12-03 17:25 ` [PATCH 2/6] util: add helper to query if a location is set Daniel P. Berrangé
2026-12-03 20:51 ` Eric Blake
2026-12-03 17:25 ` [PATCH 3/6] util: introduce a configurable message format template Daniel P. Berrangé
2026-12-03 21:44 ` Eric Blake
2026-12-04 9:22 ` Daniel P. Berrangé
2026-12-04 0:32 ` Dr. David Alan Gilbert
2026-12-03 17:25 ` [PATCH 4/6] util: convert log code to support QMessageFormat Daniel P. Berrangé
2026-12-03 21:52 ` Eric Blake
2026-12-03 17:25 ` [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 ` Daniel P. Berrangé [this message]
2026-12-03 22:02 ` [PATCH 6/6] util: add '-msg format=FORMATSTR' argument Eric Blake
2026-12-04 9:23 ` Daniel P. Berrangé
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261203172543.676434-7-berrange@redhat.com \
--to=berrange@redhat.com \
--cc=armbru@redhat.com \
--cc=dave@treblig.org \
--cc=marcandre.lureau@redhat.com \
--cc=pbonzini@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=richard.henderson@linaro.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.