From: Richard Henderson <richard.henderson@linaro.org>
To: qemu-devel@nongnu.org
Cc: berrange@redhat.com
Subject: [RFC 3/4] util/message: Use LogOutput
Date: Tue, 2 Sep 2025 12:30:09 +0200 [thread overview]
Message-ID: <20250902103010.309094-4-richard.henderson@linaro.org> (raw)
In-Reply-To: <20250902103010.309094-1-richard.henderson@linaro.org>
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
include/qemu/message.h | 12 +++++-------
util/error-report.c | 8 ++++----
util/log.c | 16 ++--------------
util/message.c | 40 ++++++++++++++++++----------------------
4 files changed, 29 insertions(+), 47 deletions(-)
diff --git a/include/qemu/message.h b/include/qemu/message.h
index 68b08f0ea7..ac285a0021 100644
--- a/include/qemu/message.h
+++ b/include/qemu/message.h
@@ -3,6 +3,8 @@
#ifndef QEMU_MESSAGE_H
#define QEMU_MESSAGE_H
+#include "qemu/log-output.h"
+
enum QMessageFormatFlags {
QMESSAGE_FORMAT_TIMESTAMP = (1 << 0),
QMESSAGE_FORMAT_WORKLOAD_NAME = (1 << 1),
@@ -31,13 +33,9 @@ void qmessage_set_workload_name(const char *name);
/**
* qmessage_context:
*
- * Format a message prefix with the information
- * previously selected by a call to
- * qmessage_set_format.
- *
- * Returns: a formatted message prefix, or empty string;
- * to be freed by the caller.
+ * Format a message prefix with the information previously selected
+ * by a call to qmessage_set_format.
*/
-char *qmessage_context(void);
+void qmessage_context(const LogOutput *l, void *opaque);
#endif /* QEMU_MESSAGE_H */
diff --git a/util/error-report.c b/util/error-report.c
index fa34019dad..6ef556af5f 100644
--- a/util/error-report.c
+++ b/util/error-report.c
@@ -173,10 +173,10 @@ G_GNUC_PRINTF(2, 0)
static void vreport(report_type type, const char *fmt, va_list ap)
{
if (!monitor_cur()) {
- g_autofree gchar *context = qmessage_context();
- if (context) {
- error_printf("%s", context);
- }
+ void *opaque;
+ const LogOutput *l = error_log_output(&opaque);
+
+ qmessage_context(l, opaque);
}
print_loc();
diff --git a/util/log.c b/util/log.c
index b129634708..8bbd8e5dda 100644
--- a/util/log.c
+++ b/util/log.c
@@ -153,23 +153,11 @@ static __thread bool incomplete;
void qemu_log(const char *fmt, ...)
{
- FILE *f;
- /*
- * Prepare the context *outside* the logging
- * lock so any timestamp better reflects when
- * the message was emitted if we are delayed
- * acquiring the mutex
- */
- g_autofree const char *context =
- incomplete ? NULL : qmessage_context();
-
- f = qemu_log_trylock();
+ FILE *f = qemu_log_trylock();
if (f) {
va_list ap;
- if (context != NULL) {
- fwrite(context, 1, strlen(context), f);
- }
+ qmessage_context(&log_output_stdio, f);
va_start(ap, fmt);
vfprintf(f, fmt, ap);
diff --git a/util/message.c b/util/message.c
index 8deba3940c..0c63d128fc 100644
--- a/util/message.c
+++ b/util/message.c
@@ -20,7 +20,7 @@ void qmessage_set_workload_name(const char *name)
}
-char *qmessage_context(void)
+void qmessage_context(const LogOutput *l, void *opaque)
{
g_autofree char *timestr = NULL;
const char *wknamestr = NULL;
@@ -43,26 +43,22 @@ char *qmessage_context(void)
int thid = qemu_get_thread_id();
const char *thname = qemu_thread_get_name();
- return g_strdup_printf("%s%s%s%s%s%s(%d:%s): ",
- timestr ? timestr : "",
- timestr ? " " : "",
- wknamestr ? "[" : "",
- wknamestr ? wknamestr : "",
- wknamestr ? "] " : "",
- pgnamestr ? pgnamestr : "",
- thid, thname);
- } else {
- if (!timestr && !wknamestr && !pgnamestr) {
- return NULL;
- }
-
- return g_strdup_printf("%s%s%s%s%s%s%s",
- timestr ? timestr : "",
- timestr ? " " : "",
- wknamestr ? "[" : "",
- wknamestr ? wknamestr : "",
- wknamestr ? "] " : "",
- pgnamestr ? pgnamestr : "",
- pgnamestr ? ": " : "");
+ l->print(opaque, "%s%s%s%s%s%s(%d:%s): ",
+ timestr ? timestr : "",
+ timestr ? " " : "",
+ wknamestr ? "[" : "",
+ wknamestr ? wknamestr : "",
+ wknamestr ? "] " : "",
+ pgnamestr ? pgnamestr : "",
+ thid, thname);
+ } else if (timestr || wknamestr || pgnamestr) {
+ l->print(opaque, "%s%s%s%s%s%s%s",
+ timestr ? timestr : "",
+ timestr ? " " : "",
+ wknamestr ? "[" : "",
+ wknamestr ? wknamestr : "",
+ wknamestr ? "] " : "",
+ pgnamestr ? pgnamestr : "",
+ pgnamestr ? ": " : "");
}
}
--
2.43.0
next prev parent reply other threads:[~2025-09-02 10:31 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-02 10:30 [RFC 0/4] util: qmessage_context followup Richard Henderson
2025-09-02 10:30 ` [RFC 1/4] util: Introduce LogOutput Richard Henderson
2025-09-02 10:30 ` [RFC 2/4] util: Drop QMESSAGE_CONTEXT_SKIP_MONITOR Richard Henderson
2025-09-02 10:30 ` Richard Henderson [this message]
2025-09-02 10:30 ` [RFC 4/4] util/error-report: Use LogOutput in vreport Richard Henderson
2025-09-02 16:47 ` [RFC 0/4] util: qmessage_context followup Daniel P. Berrangé
2025-09-10 18:05 ` 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=20250902103010.309094-4-richard.henderson@linaro.org \
--to=richard.henderson@linaro.org \
--cc=berrange@redhat.com \
--cc=qemu-devel@nongnu.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).