Update hellolibvirt to demo virGetLastErrorMessage()

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>
This commit is contained in:
Daniel P. Berrange 2013-05-10 18:43:38 +01:00
parent 31989e66c6
commit 0902f696a9

View File

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