]> xenbits.xensource.com Git - libvirt.git/commitdiff
Improve error reporting in virsh
authorJohn Levon <john.levon@sun.com>
Mon, 9 Feb 2009 14:24:06 +0000 (14:24 +0000)
committerJohn Levon <john.levon@sun.com>
Mon, 9 Feb 2009 14:24:06 +0000 (14:24 +0000)
ChangeLog
src/virsh.c

index 917b1fd6f53481ea983ded4031610f8a1a8779ad..413d21629ab93eb95c1929348fa225ff27ef44a1 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Mon Feb  9 14:19:02 GMT 2009 John Levon <john.levon@sun.com>
+
+       * src/virsh.c: rather than verbosely printing every error, save
+       the last error and report that only if the entire command fails.
+
 Mon Feb  9 14:07:51 GMT 2009 John Levon <john.levon@sun.com>
 
        * include/libvirt/virterror.h:
index ad2dae06e6920960009927c943be543b970b3fb3..57bd3969eb53dd3effa14462ff03ba2d3340a7e4 100644 (file)
@@ -92,22 +92,6 @@ typedef enum {
     VSH_ERR_ERROR
 } vshErrorLevel;
 
-/*
- * The error handler for virsh
- */
-static void
-virshErrorHandler(void *unused, virErrorPtr error)
-{
-    if ((unused != NULL) || (error == NULL))
-        return;
-
-    /* Suppress the VIR_ERR_NO_XEN error which fails as non-root */
-    if ((error->code == VIR_ERR_NO_XEN) || (error->code == VIR_ERR_OK))
-        return;
-
-    virDefaultErrorFunc(error);
-}
-
 /*
  * virsh command line grammar:
  *
@@ -321,6 +305,46 @@ static int namesorter(const void *a, const void *b) {
   return strcasecmp(*sa, *sb);
 }
 
+static virErrorPtr last_error;
+
+/*
+ * Quieten libvirt until we're done with the command.
+ */
+static void
+virshErrorHandler(void *unused ATTRIBUTE_UNUSED, virErrorPtr error)
+{
+    virFreeError(last_error);
+    last_error = virSaveLastError();
+    if (getenv("VIRSH_DEBUG") != NULL)
+        virDefaultErrorFunc(error);
+}
+
+/*
+ * Report an error when a command finishes.  This is better than before
+ * (when correct operation would report errors), but it has some
+ * problems: we lose the smarter formatting of virDefaultErrorFunc(),
+ * and it can become harder to debug problems, if errors get reported
+ * twice during one command.  This case shouldn't really happen anyway,
+ * and it's IMHO a bug that libvirt does that sometimes.
+ */
+static void
+virshReportError(vshControl *ctl)
+{
+    if (last_error == NULL)
+        return;
+
+    if (last_error->code == VIR_ERR_OK) {
+        vshError(ctl, FALSE, "%s", _("unknown error"));
+        goto out;
+    }
+
+    vshError(ctl, FALSE, "%s", last_error->message);
+
+out:
+    virFreeError(last_error);
+    last_error = NULL;
+}
+
 
 /* ---------------
  * Commands
@@ -6103,6 +6127,9 @@ vshCommandRun(vshControl *ctl, const vshCmd *cmd)
         if (ctl->timing)
             GETTIMEOFDAY(&after);
 
+        if (ret == FALSE)
+            virshReportError(ctl);
+
         if (STREQ(cmd->def->name, "quit"))        /* hack ... */
             return ret;