[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH] Update hellolibvirt to demo virGetLastErrorMessage()



From: "Daniel P. Berrange" <berrange redhat com>

Update the hellolibvirt example program to demonstrate use of
the virGetLastErrorMessage() API for quick error reporting

Signed-off-by: Daniel P. Berrange <berrange redhat com>
---
 examples/hellolibvirt/hellolibvirt.c | 64 ++++++++----------------------------
 1 file changed, 14 insertions(+), 50 deletions(-)

diff --git a/examples/hellolibvirt/hellolibvirt.c b/examples/hellolibvirt/hellolibvirt.c
index 26dd67f..4ae60fc 100644
--- a/examples/hellolibvirt/hellolibvirt.c
+++ b/examples/hellolibvirt/hellolibvirt.c
@@ -9,42 +9,6 @@
 #include <libvirt/libvirt.h>
 #include <libvirt/virterror.h>
 
-static void
-showError(virConnectPtr conn)
-{
-    int ret;
-    virErrorPtr err;
-
-    err = malloc(sizeof(*err));
-    if (!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)
 {
@@ -59,15 +23,15 @@ showHypervisorInfo(virConnectPtr conn)
     hvType = virConnectGetType(conn);
     if (!hvType) {
         ret = 1;
-        printf("Failed to get hypervisor type\n");
-        showError(conn);
+        printf("Failed to get hypervisor type: %s\n",
+               virGetLastErrorMessage());
         goto out;
     }
 
     if (0 != virConnectGetVersion(conn, &hvVer)) {
         ret = 1;
-        printf("Failed to get hypervisor version\n");
-        showError(conn);
+        printf("Failed to get hypervisor version: %s\n",
+               virGetLastErrorMessage());
         goto out;
     }
 
@@ -102,16 +66,16 @@ showDomains(virConnectPtr conn)
     numActiveDomains = virConnectNumOfDomains(conn);
     if (numActiveDomains == -1) {
         ret = 1;
-        printf("Failed to get number of active domains\n");
-        showError(conn);
+        printf("Failed to get number of active domains: %s\n",
+               virGetLastErrorMessage());
         goto out;
     }
 
     numInactiveDomains = virConnectNumOfDefinedDomains(conn);
     if (numInactiveDomains == -1) {
         ret = 1;
-        printf("Failed to get number of inactive domains\n");
-        showError(conn);
+        printf("Failed to get number of inactive domains: %s\n",
+               virGetLastErrorMessage());
         goto out;
     }
 
@@ -157,16 +121,16 @@ main(int argc, char *argv[])
 
     if (!conn) {
         ret = 1;
-        printf("No connection to hypervisor\n");
-        showError(conn);
+        printf("No connection to hypervisor: %s\n",
+               virGetLastErrorMessage());
         goto out;
     }
 
     uri = virConnectGetURI(conn);
     if (!uri) {
         ret = 1;
-        printf("Failed to get URI for hypervisor connection\n");
-        showError(conn);
+        printf("Failed to get URI for hypervisor connection: %s\n",
+               virGetLastErrorMessage());
         goto disconnect;
     }
 
@@ -185,8 +149,8 @@ main(int argc, char *argv[])
 
 disconnect:
     if (0 != virConnectClose(conn)) {
-        printf("Failed to disconnect from hypervisor\n");
-        showError(conn);
+        printf("Failed to disconnect from hypervisor: %s\n",
+               virGetLastErrorMessage());
         ret = 1;
     } else {
         printf("Disconnected from hypervisor\n");
-- 
1.8.2.1


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]