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

[libvirt] [PATCH] Unify *ReportError logic



Currently, most src/* files have their own ReportError
function. Some support printf style arguments, others
only allow reporting a single string message. The code
for all of them does virtually the same thing, possibly
passing a different constant off to another function.

The attached patch adds a function to virterror.c which
encapsulates the common ReportError logic. I used this
to replace qemudReportError with a macro, which also 
allows passing off filename and line number info if
we wanted to do something with it later.

I did just the one function conversion to see what
people think: if I'm missing something, or ideas for
anything else to add. Seems to work as expected in
my testing.

Thanks,
Cole
diff --git a/src/internal.h b/src/internal.h
index a3d48fa..07fc151 100644
--- a/src/internal.h
+++ b/src/internal.h
@@ -313,6 +313,10 @@ void __virRaiseError(virConnectPtr conn,
                      int int1, int int2, const char *msg, ...)
   ATTRIBUTE_FORMAT(printf, 12, 13);
 const char *__virErrorMsg(virErrorNumber error, const char *info);
+void __virReportErrorHelper(virConnectPtr conn, virDomainPtr dom, virNetworkPtr net,
+                            int domcode, int errcode, const char *filename,
+                            long long filenr, const char *fmt, ...)
+  ATTRIBUTE_FORMAT(printf, 8, 9);
 
 /************************************************************************
  *									*
diff --git a/src/qemu_conf.c b/src/qemu_conf.c
index 314c14a..2513f74 100644
--- a/src/qemu_conf.c
+++ b/src/qemu_conf.c
@@ -59,27 +59,6 @@ VIR_ENUM_IMPL(virDomainDiskQEMUBus, VIR_DOMAIN_DISK_BUS_LAST,
 
 #define qemudLog(level, msg...) fprintf(stderr, msg)
 
-void qemudReportError(virConnectPtr conn,
-                      virDomainPtr dom,
-                      virNetworkPtr net,
-                      int code, const char *fmt, ...) {
-    va_list args;
-    char errorMessage[1024];
-    const char *virerr;
-
-    if (fmt) {
-        va_start(args, fmt);
-        vsnprintf(errorMessage, sizeof(errorMessage)-1, fmt, args);
-        va_end(args);
-    } else {
-        errorMessage[0] = '\0';
-    }
-
-    virerr = __virErrorMsg(code, (errorMessage[0] ? errorMessage : NULL));
-    __virRaiseError(conn, dom, net, VIR_FROM_QEMU, code, VIR_ERR_ERROR,
-                    virerr, errorMessage, NULL, -1, -1, virerr, errorMessage);
-}
-
 int qemudLoadDriverConfig(struct qemud_driver *driver,
                           const char *filename) {
     virConfPtr conf;
diff --git a/src/qemu_conf.h b/src/qemu_conf.h
index 0099dff..a3c9b74 100644
--- a/src/qemu_conf.h
+++ b/src/qemu_conf.h
@@ -69,11 +69,9 @@ struct qemud_driver {
 };
 
 
-void qemudReportError(virConnectPtr conn,
-                      virDomainPtr dom,
-                      virNetworkPtr net,
-                      int code, const char *fmt, ...)
-    ATTRIBUTE_FORMAT(printf,5,6);
+#define qemudReportError(conn, dom, net, code, fmt...)                        \
+        __virReportErrorHelper(conn, dom, net, VIR_FROM_QEMU, code,           \
+                               __FILE__, __LINE__, fmt)
 
 
 int qemudLoadDriverConfig(struct qemud_driver *driver,
diff --git a/src/virterror.c b/src/virterror.c
index 4aa7f42..bae6f55 100644
--- a/src/virterror.c
+++ b/src/virterror.c
@@ -722,3 +722,42 @@ __virErrorMsg(virErrorNumber error, const char *info)
     }
     return (errmsg);
 }
+
+/**
+ * __virReportErrorHelper
+ *
+ * @conn: the connection to the hypervisor if available
+ * @dom: the domain if available
+ * @net: the network if available
+ * @domcode: the virErrorDomain indicating where it's coming from
+ * @errcode: the virErrorNumber code for the error
+ * @filename: Source file error is dispatched from
+ * @linenr: Line number error is dispatched from
+ * @msg:  the message to display/transmit
+ * @...:  extra parameters for the message display
+ *
+ * Helper function to do most of the grunt work for individual driver ReportError
+ */
+void __virReportErrorHelper(virConnectPtr conn, virDomainPtr dom, virNetworkPtr net,
+                            int domcode, int errcode,
+                            const char *filename ATTRIBUTE_UNUSED,
+                            long long linenr ATTRIBUTE_UNUSED,
+                            const char *fmt, ...)
+{
+    va_list args;
+    char errorMessage[1024];
+    const char *virerr;
+
+    if (fmt) {
+        va_start(args, fmt);
+        vsnprintf(errorMessage, sizeof(errorMessage)-1, fmt, args);
+        va_end(args);
+    } else {
+        errorMessage[0] = '\0';
+    }
+
+    virerr = __virErrorMsg(errcode, (errorMessage[0] ? errorMessage : NULL));
+    __virRaiseError(conn, dom, net, domcode, errcode, VIR_ERR_ERROR,
+                    virerr, errorMessage, NULL, -1, -1, virerr, errorMessage);
+
+}

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