[PATCH 3/4] log: support logging using shell wildcard syntax

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

 



Rather than specialcasing handling of the '*' character, use fnmatch()
to get normal shell wildcard syntax, as described in 'man glob(7)'.

Signed-off-by: Daniel P. Berrangé <berrange@xxxxxxxxxx>
---
 src/util/virlog.c | 17 +++++++++++++++--
 src/util/virlog.h |  1 +
 2 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/src/util/virlog.c b/src/util/virlog.c
index 5262d613f6..1db10fcc71 100644
--- a/src/util/virlog.c
+++ b/src/util/virlog.c
@@ -40,6 +40,7 @@
 #if HAVE_SYS_UN_H
 # include <sys/un.h>
 #endif
+#include <fnmatch.h>
 
 #include "virerror.h"
 #include "virlog.h"
@@ -508,7 +509,9 @@ virLogSourceUpdate(virLogSourcePtr source)
         size_t i;
 
         for (i = 0; i < virLogNbFilters; i++) {
-            if (strstr(source->name, virLogFilters[i]->match)) {
+            if ((virLogFilters[i]->flags & VIR_LOG_GLOB) ?
+                (fnmatch(virLogFilters[i]->match, source->name, 0) == 0) :
+                (strstr(source->name, virLogFilters[i]->match) != NULL)) {
                 priority = virLogFilters[i]->priority;
                 flags = virLogFilters[i]->flags;
                 break;
@@ -1409,7 +1412,7 @@ virLogFilterNew(const char *match,
     virLogFilterPtr ret = NULL;
     char *mdup = NULL;
 
-    virCheckFlags(VIR_LOG_STACK_TRACE, NULL);
+    virCheckFlags(VIR_LOG_STACK_TRACE | VIR_LOG_GLOB, NULL);
 
     if (priority < VIR_LOG_DEBUG || priority > VIR_LOG_ERROR) {
         virReportError(VIR_ERR_INVALID_ARG, _("Invalid log priority %d"),
@@ -1718,6 +1721,16 @@ virLogParseFilter(const char *src)
         goto cleanup;
     }
 
+    /* Only turn on fnmatch usage if we see special glob
+     * characters, so we use more efficient strstr()
+     * by default
+     */
+    if (strchr(match, '*') ||
+        strchr(match, '?') ||
+        strchr(match, '[')) {
+        flags |= VIR_LOG_GLOB;
+    }
+
     if (!(ret = virLogFilterNew(match, prio, flags)))
         goto cleanup;
 
diff --git a/src/util/virlog.h b/src/util/virlog.h
index 35dba16cfe..95c405bac0 100644
--- a/src/util/virlog.h
+++ b/src/util/virlog.h
@@ -174,6 +174,7 @@ typedef void (*virLogCloseFunc) (void *data);
 
 typedef enum {
     VIR_LOG_STACK_TRACE = (1 << 0),
+    VIR_LOG_GLOB = (1 << 1),
 } virLogFilterFlags;
 
 int virLogGetNbFilters(void);
-- 
2.14.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]

  Powered by Linux