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 3/6] util: introduce a configurable message format template
Date: Thu, 3 Dec 2026 17:25:40 +0000 [thread overview]
Message-ID: <20261203172543.676434-4-berrange@redhat.com> (raw)
In-Reply-To: <20261203172543.676434-1-berrange@redhat.com>
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
next prev parent reply other threads:[~2026-12-03 17:26 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-03 17:25 [PATCH 0/6] util: configurable / flexible error/log message output format Daniel P. Berrangé
2026-12-03 17:25 ` [PATCH 1/6] util: pull Location struct and APIs into location.{c,h} Daniel P. Berrangé
2026-12-03 20:51 ` Eric Blake
2026-12-03 17:25 ` [PATCH 2/6] util: add helper to query if a location is set Daniel P. Berrangé
2026-12-03 20:51 ` Eric Blake
2026-12-03 17:25 ` Daniel P. Berrangé [this message]
2026-12-03 21:44 ` [PATCH 3/6] util: introduce a configurable message format template 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é
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-4-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.