# HG changeset patch
# User Dan Smith <danms(a)us.ibm.com>
# Date 1227550535 28800
# Node ID af1c552b33cb473ed7ce52ddb6eb97b39b69b001
# Parent 598515fe705be5738226bb0fe789d7645769e4f3
Add vir_set_status() to misc_util.h
This function allows you to set the message of a CMPIStatus object to something
like what perror() would do. You give it an error message and (optionally)
a virConnectPtr object and it formats it as "YourError: VirtError". It
attempts to extract the last error from the connection pointer if supplied,
otherwise it takes the last error in the global library pointer.
I tried to make this function as foolproof as possible, providing no real
error paths. It attempts to get as much info as it can into the status
object, despite any failures along the way. Since this is usually to be used
for error handling scenarios, this seems like the only sane approach.
Signed-off-by: Dan Smith <danms(a)us.ibm.com>
diff -r 598515fe705b -r af1c552b33cb libxkutil/misc_util.c
--- a/libxkutil/misc_util.c Fri Nov 21 15:08:33 2008 -0800
+++ b/libxkutil/misc_util.c Mon Nov 24 10:15:35 2008 -0800
@@ -24,6 +24,7 @@
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
+#include <stdarg.h>
#include <unistd.h>
#include <libvirt/libvirt.h>
#include <libvirt/virterror.h>
@@ -607,6 +608,51 @@
return vref;
}
+int virt_set_status(const CMPIBroker *broker,
+ CMPIStatus *s,
+ CMPIrc rc,
+ virConnectPtr conn,
+ const char *fmt, ...)
+{
+ va_list ap;
+ char *formatted = NULL;
+ char *verrmsg = NULL;
+ int ret;
+ virErrorPtr virt_error;
+
+ if (conn == NULL)
+ virt_error = virGetLastError();
+ else
+ virt_error = virConnGetLastError(conn);
+
+ if (virt_error == NULL) {
+ if (asprintf(&verrmsg, "(none)") == -1)
+ verrmsg = NULL;
+ } else if (virt_error->message != NULL) {
+ verrmsg = strdup(virt_error->message);
+ } else {
+ if (asprintf(&verrmsg, "Error %i", virt_error->code) ==
-1)
+ verrmsg = NULL;
+ }
+
+ va_start(ap, fmt);
+ ret = vasprintf(&formatted, fmt, ap);
+ va_end(ap);
+
+ if (ret == -1) {
+ CU_DEBUG("Failed to format message for %s", fmt);
+ formatted = NULL;
+ }
+
+ ret = cu_statusf(broker, s, rc, "%s: %s", formatted, verrmsg);
+
+ free(formatted);
+ free(verrmsg);
+
+ return ret;
+}
+
+
/*
* Local Variables:
* mode: C
diff -r 598515fe705b -r af1c552b33cb libxkutil/misc_util.h
--- a/libxkutil/misc_util.h Fri Nov 21 15:08:33 2008 -0800
+++ b/libxkutil/misc_util.h Mon Nov 24 10:15:35 2008 -0800
@@ -130,6 +130,12 @@
const CMPIObjectPath *ref,
struct std_assoc_info *info);
+int virt_set_status(const CMPIBroker *broker,
+ CMPIStatus *s,
+ CMPIrc rc,
+ virConnectPtr conn,
+ const char *fmt, ...);
+
#define LIBVIRT_CIM_DEFAULT_MAKEREF() \
static CMPIInstance* make_ref(const CMPIObjectPath *source_ref, \
const CMPIInstance *target_inst, \