[PATCH 21/38] virlog: Split filter parsing and filter defining to separate operations

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

 



Everything has been prepared to successfully split parsing and defining logic
to separate operations.
---
 src/libvirt_private.syms |   1 +
 src/util/virlog.c        | 100 +++++++++++++++++++++++------------------------
 src/util/virlog.h        |   8 ++--
 tests/virlogtest.c       |   7 ++--
 4 files changed, 58 insertions(+), 58 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index e594eaa..319ef18 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1743,6 +1743,7 @@ virLockSpaceReleaseResourcesForOwner;
 # util/virlog.h
 virLogDefineFilters;
 virLogDefineOutputs;
+virLogFilterListFree;
 virLogFilterNew;
 virLogGetDefaultPriority;
 virLogGetFilters;
diff --git a/src/util/virlog.c b/src/util/virlog.c
index 369d7dd..0116f56 100644
--- a/src/util/virlog.c
+++ b/src/util/virlog.c
@@ -257,57 +257,36 @@ virLogResetFilters(void)
  * The filter defines a rules that will apply only to messages matching
  * the pattern (currently if @match is a substring of the message category)
  *
- * Returns -1 in case of failure or the filter number if successful
+ * Returns a reference to a newly created filter that needs to be defined using
+ * virLogDefineFiltersm, or NULL in case of an error.
  */
-int
+virLogFilterPtr
 virLogFilterNew(const char *match,
                 virLogPriority priority,
                 unsigned int flags)
 {
-    size_t i;
-    int ret = -1;
+    virLogFilterPtr ret = NULL;
     char *mdup = NULL;
-    virLogFilterPtr filter = NULL;
 
-    virCheckFlags(VIR_LOG_STACK_TRACE, -1);
-
-    if (virLogInitialize() < 0)
-        return -1;
+    virCheckFlags(VIR_LOG_STACK_TRACE, NULL);
 
     if ((match == NULL) || (priority < VIR_LOG_DEBUG) ||
         (priority > VIR_LOG_ERROR))
-        return -1;
-
-    virLogLock();
-    for (i = 0; i < virLogNbFilters; i++) {
-        if (STREQ(virLogFilters[i]->match, match)) {
-            virLogFilters[i]->priority = priority;
-            ret = i;
-            goto cleanup;
-        }
-    }
+        return NULL;
 
     if (VIR_STRDUP_QUIET(mdup, match) < 0)
-        goto cleanup;
+        return NULL;
 
-    if (VIR_ALLOC_QUIET(filter) < 0) {
+    if (VIR_ALLOC_QUIET(ret) < 0) {
         VIR_FREE(mdup);
-        goto cleanup;
+        return NULL;
     }
 
-    filter->match = mdup;
-    filter->priority = priority;
-    filter->flags = flags;
-
-    if (VIR_APPEND_ELEMENT_QUIET(virLogFilters, virLogNbFilters, filter) < 0)
-        goto cleanup;
+    ret->match = mdup;
+    ret->priority = priority;
+    ret->flags = flags;
 
-    virLogFiltersSerial++;
- cleanup:
-    virLogUnlock();
-    if (ret < 0)
-        virReportOOMError();
-    return virLogNbFilters;
+    return ret;
 }
 
 /**
@@ -1217,10 +1196,10 @@ virLogParseOutputs(const char *src, virLogOutputPtr **outputs)
 }
 
 
-static int
+static virLogFilterPtr
 virLogParseFilter(const char *filter)
 {
-    int ret = -1;
+    virLogFilterPtr ret = NULL;
     size_t count = 0;
     virLogPriority prio;
     char **tokens = NULL;
@@ -1228,12 +1207,12 @@ virLogParseFilter(const char *filter)
     char *ref = NULL;
 
     if (!filter)
-        return -1;
+        return NULL;
 
     VIR_DEBUG("filter=%s", filter);
 
     if (!(tokens = virStringSplitCount(filter, ":", 0, &count)))
-        return -1;
+        return NULL;
 
     if (count != 2)
         goto cleanup;
@@ -1251,12 +1230,11 @@ virLogParseFilter(const char *filter)
     if (!*ref)
         goto cleanup;
 
-    if (virLogFilterNew(ref, prio, flags) < 0)
+    if (!(ret = virLogFilterNew(ref, prio, flags)))
         goto cleanup;
 
-    ret = 0;
  cleanup:
-    if (ret < 0)
+    if (!ret)
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Failed to parse and define log filter %s"), filter);
     virStringFreeList(tokens);
@@ -1282,19 +1260,21 @@ virLogParseFilter(const char *filter)
  * Returns the number of filter parsed or -1 in case of error.
  */
 int
-virLogParseFilters(const char *filters)
+virLogParseFilters(const char *src, virLogFilterPtr **filters)
 {
     int ret = -1;
-    int count = 0;
+    size_t count = 0;
     size_t i;
     char **strings = NULL;
+    virLogFilterPtr filter = NULL;
+    virLogFilterPtr *list = NULL;
 
-    if (!filters)
+    if (!src)
         return -1;
 
-    VIR_DEBUG("filters=%s", filters);
+    VIR_DEBUG("filters=%s", src);
 
-    if (!(strings = virStringSplit(filters, " ", 0)))
+    if (!(strings = virStringSplit(src, " ", 0)))
         goto cleanup;
 
     for (i = 0; strings[i]; i++) {
@@ -1302,14 +1282,22 @@ virLogParseFilters(const char *filters)
         if (STREQ(strings[i], ""))
             continue;
 
-        if (virLogParseFilter(strings[i]) < 0)
+        if (!(filter = virLogParseFilter(strings[i])))
             goto cleanup;
 
-        count++;
+        if (VIR_APPEND_ELEMENT(list, count, filter)) {
+            virLogFilterFree(filter);
+            goto cleanup;
+        }
+
+        virLogFilterFree(filter);
     }
 
     ret = count;
+    *filters = list;
  cleanup:
+    if (ret < 0)
+        virLogFilterListFree(list, count);
     virStringFreeList(strings);
     return ret;
 }
@@ -1516,12 +1504,22 @@ int
 virLogSetFilters(const char *filters)
 {
     int ret = -1;
+    int count = 0;
+    virLogFilterPtr *list = NULL;
 
     if (virLogInitialize() < 0)
         return -1;
 
-    ret = virLogParseFilters(filters);
+    if ((count = virLogParseFilters(filters, &list)) < 0)
+        goto cleanup;
 
+    if (virLogDefineFilters(list, count) < 0)
+        goto cleanup;
+
+    ret = count;
+ cleanup:
+    if (ret < 0)
+        virLogFilterListFree(list, count);
     return ret;
 }
 
@@ -1599,9 +1597,9 @@ virLogDefineFilters(virLogFilterPtr *filters, size_t nfilters)
         return -1;
 
     virLogLock();
-    virLogResetOutputs();
+    virLogResetFilters();
     virLogFilters = filters;
-    virLogNbOutputs = nfilters;
+    virLogNbFilters = nfilters;
     virLogFiltersSerial++;
     virLogUnlock();
 
diff --git a/src/util/virlog.h b/src/util/virlog.h
index 9b9f643..0102489 100644
--- a/src/util/virlog.h
+++ b/src/util/virlog.h
@@ -185,9 +185,9 @@ extern int virLogSetDefaultPriority(virLogPriority priority);
 extern void virLogSetFromEnv(void);
 extern int virLogSetFilters(const char *filters);
 extern int virLogSetOutputs(const char *outputs);
-extern int virLogFilterNew(const char *match,
-                           virLogPriority priority,
-                           unsigned int flags);
+extern virLogFilterPtr virLogFilterNew(const char *match,
+                                       virLogPriority priority,
+                                       unsigned int flags);
 extern virLogOutputPtr virLogOutputNew(virLogOutputFunc f,
                                        virLogCloseFunc c,
                                        void *data,
@@ -207,7 +207,7 @@ extern void virLogLock(void);
 extern void virLogUnlock(void);
 extern int virLogReset(void);
 extern int virLogParseDefaultPriority(const char *priority);
-extern int virLogParseFilters(const char *filters);
+extern int virLogParseFilters(const char *src, virLogFilterPtr **filters);
 extern int virLogParseOutputs(const char *src, virLogOutputPtr **outputs);
 extern int virLogPriorityFromSyslog(int priority);
 extern void virLogMessage(virLogSourcePtr source,
diff --git a/tests/virlogtest.c b/tests/virlogtest.c
index 02613f5..047013f 100644
--- a/tests/virlogtest.c
+++ b/tests/virlogtest.c
@@ -79,10 +79,11 @@ static int
 testLogParseFilters(const void *opaque)
 {
     int ret = -1;
-    int nfilters;
+    int nfilters = -1;
+    virLogFilterPtr *list = NULL;
     const struct testLogData *data = opaque;
 
-    nfilters = virLogParseFilters(data->str);
+    nfilters = virLogParseFilters(data->str, &list);
     if (nfilters < 0) {
         if (!data->pass) {
             VIR_TEST_DEBUG("Got expected error: %s\n",
@@ -102,7 +103,7 @@ testLogParseFilters(const void *opaque)
 
     ret = 0;
  cleanup:
-    virLogReset();
+    virLogFilterListFree(list, nfilters);
     return ret;
 }
 
-- 
2.4.3

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]