[RFC 3/4] util/message: Use LogOutput

Richard Henderson posted 4 patches 3 weeks, 5 days ago
[RFC 3/4] util/message: Use LogOutput
Posted by Richard Henderson 3 weeks, 5 days ago
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