[libvirt] [PATCH] Add showError function to display error data generated by libvirt.

David Allan dallan at redhat.com
Fri Jan 30 22:46:26 UTC 2009


---
 examples/hellolibvirt/hellolibvirt.c |   46 ++++++++++++++++++++++++++++++++++
 1 files changed, 46 insertions(+), 0 deletions(-)

diff --git a/examples/hellolibvirt/hellolibvirt.c b/examples/hellolibvirt/hellolibvirt.c
index cc1af0f..234637e 100644
--- a/examples/hellolibvirt/hellolibvirt.c
+++ b/examples/hellolibvirt/hellolibvirt.c
@@ -6,6 +6,43 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <libvirt/libvirt.h>
+#include <libvirt/virterror.h>
+
+static void
+showError(virConnectPtr conn)
+{
+    int ret;
+    virErrorPtr err;
+
+    err = malloc(sizeof(*err));
+    if (NULL == err) {
+        printf("Could not allocate memory for error data\n");
+        goto out;
+    }
+
+    ret = virConnCopyLastError(conn, err);
+
+    switch (ret) {
+    case 0:
+        printf("No error found\n");
+        break;
+
+    case -1:
+        printf("Parameter error when attempting to get last error\n");
+        break;
+
+    default:
+        printf("libvirt reported: \"%s\"\n", err->message);
+        break;
+    }
+
+    virResetError(err);
+    free(err);
+
+out:
+    return;
+}
+
 
 static int
 showHypervisorInfo(virConnectPtr conn)
@@ -22,12 +59,14 @@ showHypervisorInfo(virConnectPtr conn)
     if (NULL == hvType) {
         ret = 1;
         printf("Failed to get hypervisor type\n");
+        showError(conn);
         goto out;
     }
 
     if (0 != virConnectGetVersion(conn, &hvVer)) {
         ret = 1;
         printf("Failed to get hypervisor version\n");
+        showError(conn);
         goto out;
     }
 
@@ -57,6 +96,7 @@ showDomains(virConnectPtr conn)
     if (-1 == numActiveDomains) {
         ret = 1;
         printf("Failed to get number of active domains\n");
+        showError(conn);
         goto out;
     }
 
@@ -64,6 +104,7 @@ showDomains(virConnectPtr conn)
     if (-1 == numInactiveDomains) {
         ret = 1;
         printf("Failed to get number of inactive domains\n");
+        showError(conn);
         goto out;
     }
 
@@ -85,6 +126,7 @@ showDomains(virConnectPtr conn)
     if (-1 == numNames) {
         ret = 1;
         printf("Could not get list of defined domains from hypervisor\n");
+        showError(conn);
         goto out;
     }
 
@@ -124,6 +166,7 @@ main(int argc, char *argv[])
     if (NULL == conn) {
         ret = 1;
         printf("No connection to hypervisor\n");
+        showError(conn);
         goto out;
     }
 
@@ -131,6 +174,7 @@ main(int argc, char *argv[])
     if (NULL == uri) {
         ret = 1;
         printf("Failed to get URI for hypervisor connection\n");
+        showError(conn);
         goto disconnect;
     }
 
@@ -150,6 +194,8 @@ main(int argc, char *argv[])
 disconnect:
     if (0 != virConnectClose(conn)) {
         printf("Failed to disconnect from hypervisor\n");
+        showError(conn);
+        ret = 1;
     } else {
         printf("Disconnected from hypervisor\n");
     }
-- 
1.6.0.6




More information about the libvir-list mailing list