[libvirt] [PATCH 1/7] util: consolidate duplicated error messages in virnetlink.c

Laine Stump laine at laine.org
Thu Mar 8 09:24:16 UTC 2012


There are special stub versions of all public functions in this file
that are compiled when either libnl isn't available or the platform
isn't linux. Each of these functions had two almost identical message,
differing only in the function name included in the message. Since log
messages already contain the function name, we can just define a const
char* with the common part of the string, and use that same string for
all the log messages.

Also, rather than doing #if defined ... #else ... #endif *inside the
error log macro invocation*, this patch does #if defined ... just
once, using it to decide which single string to define. This turns the
error log in each function from 6 lines, to 1 line.
---
 src/util/virnetlink.c |   47 ++++++++++++-----------------------------------
 1 files changed, 12 insertions(+), 35 deletions(-)

diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c
index 59f3e39..77dde9f 100644
--- a/src/util/virnetlink.c
+++ b/src/util/virnetlink.c
@@ -525,17 +525,18 @@ cleanup:
 
 #else
 
+# if defined(__linux) && !defined(HAVE_LIBNL)
+static const char *unsupported = _("libnl was not available at build time");
+# else
+static const char *unsupported = _("not supported on non-linux platforms");
+# endif
+
 int virNetlinkCommand(struct nl_msg *nl_msg ATTRIBUTE_UNUSED,
            unsigned char **respbuf ATTRIBUTE_UNUSED,
            unsigned int *respbuflen ATTRIBUTE_UNUSED,
            int nl_pid ATTRIBUTE_UNUSED)
 {
-    netlinkError(VIR_ERR_INTERNAL_ERROR, "%s",
-# if defined(__linux__) && !defined(HAVE_LIBNL)
-                 _("virNetlinkCommand is not supported since libnl was not available"));
-# else
-                 _("virNetlinkCommand is not supported on non-linux platforms"));
-# endif
+    netlinkError(VIR_ERR_INTERNAL_ERROR, "%s", unsupported);
     return -1;
 }
 
@@ -545,11 +546,7 @@ int virNetlinkCommand(struct nl_msg *nl_msg ATTRIBUTE_UNUSED,
  */
 int virNetlinkEventServiceStop(void)
 {
-# if defined(__linux__) && !defined(HAVE_LIBNL)
-    netlinkError(VIR_ERR_INTERNAL_ERROR,
-                "%s",
-                _("virNetlinkEventServiceStop is not supported since libnl was not available"));
-# endif
+    netlinkError(VIR_ERR_INTERNAL_ERROR, "%s", unsupported);
     return 0;
 }
 
@@ -559,11 +556,7 @@ int virNetlinkEventServiceStop(void)
  */
 int virNetlinkEventServiceStart(void)
 {
-# if defined(__linux__) && !defined(HAVE_LIBNL)
-    netlinkError(VIR_ERR_INTERNAL_ERROR,
-                "%s",
-                _("virNetlinkEventServiceStart is not supported since libnl was not available"));
-# endif
+    netlinkError(VIR_ERR_INTERNAL_ERROR, "%s", unsupported);
     return 0;
 }
 
@@ -573,11 +566,7 @@ int virNetlinkEventServiceStart(void)
  */
 bool virNetlinkEventServiceIsRunning(void)
 {
-# if defined(__linux__) && !defined(HAVE_LIBNL)
-    netlinkError(VIR_ERR_INTERNAL_ERROR,
-                "%s",
-                _("virNetlinkEventServiceIsRunning is not supported since libnl was not available"));
-# endif
+    netlinkError(VIR_ERR_INTERNAL_ERROR, "%s", unsupported);
     return 0;
 }
 
@@ -590,13 +579,7 @@ int virNetlinkEventAddClient(virNetlinkEventHandleCallback handleCB ATTRIBUTE_UN
                              void *opaque ATTRIBUTE_UNUSED,
                              const unsigned char *macaddr ATTRIBUTE_UNUSED)
 {
-    netlinkError(VIR_ERR_INTERNAL_ERROR,
-                "%s",
-# if defined(__linux__) && !defined(HAVE_LIBNL)
-                _("virNetlinkEventServiceAddClient is not supported since libnl was not available"));
-# else
-                _("virNetlinkEventServiceAddClient is not supported on non-linux platforms"));
-# endif
+    netlinkError(VIR_ERR_INTERNAL_ERROR, "%s", unsupported);
     return -1;
 }
 
@@ -606,13 +589,7 @@ int virNetlinkEventAddClient(virNetlinkEventHandleCallback handleCB ATTRIBUTE_UN
 int virNetlinkEventRemoveClient(int watch ATTRIBUTE_UNUSED,
                                 const unsigned char *macaddr ATTRIBUTE_UNUSED)
 {
-    netlinkError(VIR_ERR_INTERNAL_ERROR,
-                "%s",
-# if defined(__linux__) && !defined(HAVE_LIBNL)
-                _("virNetlinkEventRemoveClient is not supported since libnl was not available"));
-# else
-                _("virNetlinkEventRemoveClient is not supported on non-linux platforms"));
-# endif
+    netlinkError(VIR_ERR_INTERNAL_ERROR, "%s", unsupported);
     return -1;
 }
 
-- 
1.7.7.6




More information about the libvir-list mailing list