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 4/6] util: convert log code to support QMessageFormat
Date: Thu, 3 Dec 2026 17:25:41 +0000 [thread overview]
Message-ID: <20261203172543.676434-5-berrange@redhat.com> (raw)
In-Reply-To: <20261203172543.676434-1-berrange@redhat.com>
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
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 ` Daniel P. Berrangé [this message]
2026-12-03 21:52 ` [PATCH 4/6] util: convert log code to support QMessageFormat 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-5-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.