qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Gerd Hoffmann <kraxel@redhat.com>
To: qemu-devel@nongnu.org
Cc: Gerd Hoffmann <kraxel@redhat.com>
Subject: [Qemu-devel] [PATCH 2/4] add qemu_error() + friends
Date: Wed, 26 Aug 2009 15:19:17 +0200	[thread overview]
Message-ID: <1251292759-32247-3-git-send-email-kraxel@redhat.com> (raw)
In-Reply-To: <1251292759-32247-1-git-send-email-kraxel@redhat.com>

This patch adds some functions for error reporting to address the
problem that error messages should be routed to different destinations
depending on the context of the caller, i.e. monitor command errors
should go to the monitor, command line errors to stderr.

qemu_error() is a printf-like function to report errors.

qemu_errors_to_file() and qemu_errors_to_mon() switch the destination
for the error message to the specified file or monitor.  When setting a
new destination the old one will be kept.  One can switch back using
qemu_errors_to_previous().  i.e. it works like a stack.

main() calls qemu_errors_to_file(stderr), so errors go to stderr by
default.  monitor callbacks are wrapped into qemu_errors_to_mon() +
qemu_errors_to_previous(), so any errors triggered by monitor commands
will go to the monitor.

Each thread has its own error message destination.  qemu-kvm probably
should add a qemu_errors_to_file(stderr) call to the i/o-thread
initialization code.

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
---
 monitor.c |   71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 sysemu.h  |    5 ++++
 vl.c      |    1 +
 3 files changed, 76 insertions(+), 1 deletions(-)

diff --git a/monitor.c b/monitor.c
index 967171b..6f7c940 100644
--- a/monitor.c
+++ b/monitor.c
@@ -2782,6 +2782,7 @@ static void monitor_handle_command(Monitor *mon, const char *cmdline)
         goto fail;
     }
 
+    qemu_errors_to_mon(mon);
     switch(nb_args) {
     case 0:
         handler_0 = cmd->handler;
@@ -2833,8 +2834,10 @@ static void monitor_handle_command(Monitor *mon, const char *cmdline)
         break;
     default:
         monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
-        goto fail;
+        break;
     }
+    qemu_errors_to_previous();
+
  fail:
     for(i = 0; i < MAX_ARGS; i++)
         qemu_free(str_allocated[i]);
@@ -3202,3 +3205,69 @@ void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
     if (err && completion_cb)
         completion_cb(opaque, err);
 }
+
+typedef struct QemuErrorSink QemuErrorSink;
+struct QemuErrorSink {
+    enum {
+        ERR_SINK_FILE,
+        ERR_SINK_MONITOR,
+    } dest;
+    union {
+        FILE    *fp;
+        Monitor *mon;
+    };
+    QemuErrorSink *previous;
+};
+
+static __thread QemuErrorSink *qemu_error_sink;
+
+void qemu_errors_to_file(FILE *fp)
+{
+    QemuErrorSink *sink;
+
+    sink = qemu_mallocz(sizeof(*sink));
+    sink->dest = ERR_SINK_FILE;
+    sink->fp = fp;
+    sink->previous = qemu_error_sink;
+    qemu_error_sink = sink;
+}
+
+void qemu_errors_to_mon(Monitor *mon)
+{
+    QemuErrorSink *sink;
+
+    sink = qemu_mallocz(sizeof(*sink));
+    sink->dest = ERR_SINK_MONITOR;
+    sink->mon = mon;
+    sink->previous = qemu_error_sink;
+    qemu_error_sink = sink;
+}
+
+void qemu_errors_to_previous(void)
+{
+    QemuErrorSink *sink;
+
+    assert(qemu_error_sink != NULL);
+    sink = qemu_error_sink;
+    qemu_error_sink = sink->previous;
+    qemu_free(sink);
+}
+
+void qemu_error(const char *fmt, ...)
+{
+    va_list args;
+
+    assert(qemu_error_sink != NULL);
+    switch (qemu_error_sink->dest) {
+    case ERR_SINK_FILE:
+        va_start(args, fmt);
+        vfprintf(qemu_error_sink->fp, fmt, args);
+        va_end(args);
+        break;
+    case ERR_SINK_MONITOR:
+        va_start(args, fmt);
+        monitor_vprintf(qemu_error_sink->mon, fmt, args);
+        va_end(args);
+        break;
+    }
+}
diff --git a/sysemu.h b/sysemu.h
index 1df0872..eaf7f58 100644
--- a/sysemu.h
+++ b/sysemu.h
@@ -65,6 +65,11 @@ int qemu_savevm_state_complete(QEMUFile *f);
 int qemu_savevm_state(QEMUFile *f);
 int qemu_loadvm_state(QEMUFile *f);
 
+void qemu_errors_to_file(FILE *fp);
+void qemu_errors_to_mon(Monitor *mon);
+void qemu_errors_to_previous(void);
+void qemu_error(const char *fmt, ...) __attribute__ ((format(printf, 1, 2)));
+
 #ifdef _WIN32
 /* Polling handling */
 
diff --git a/vl.c b/vl.c
index c164f45..7bbd273 100644
--- a/vl.c
+++ b/vl.c
@@ -4801,6 +4801,7 @@ int main(int argc, char **argv, char **envp)
     CPUState *env;
     int show_vnc_port = 0;
 
+    qemu_errors_to_file(stderr);
     qemu_cache_utils_init(envp);
 
     LIST_INIT (&vm_change_state_head);
-- 
1.6.2.5

  parent reply	other threads:[~2009-08-26 13:19 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-08-26 13:19 [Qemu-devel] [PATCH 0/4] qdev: init() callback retval + error reporting Gerd Hoffmann
2009-08-26 13:19 ` [Qemu-devel] [PATCH 1/4] qdev: add return value to init() callbacks Gerd Hoffmann
2009-08-26 13:19 ` Gerd Hoffmann [this message]
2009-08-26 13:19 ` [Qemu-devel] [PATCH 3/4] virtio-pci error logging Gerd Hoffmann
2009-08-26 13:19 ` [Qemu-devel] [PATCH 4/4] qdev " Gerd Hoffmann
2009-08-28  0:46 ` [Qemu-devel] [PATCH 0/4] qdev: init() callback retval + error reporting Anthony Liguori
  -- strict thread matches above, loose matches on Subject: below --
2009-08-14  8:36 [Qemu-devel] [PATCH 1/4] qdev: add return value to init() callbacks Gerd Hoffmann
2009-08-14  8:36 ` [Qemu-devel] [PATCH 2/4] add qemu_error() + friends Gerd Hoffmann

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=1251292759-32247-3-git-send-email-kraxel@redhat.com \
    --to=kraxel@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).