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

Daniel P. Berrangé posted 4 patches 7 years ago
There is a newer version of this series
[libvirt] [PATCH 3/4] log: support logging using shell wildcard syntax
Posted by Daniel P. Berrangé 7 years ago
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@redhat.com>
---
 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@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 3/4] log: support logging using shell wildcard syntax
Posted by Daniel P. Berrangé 7 years ago
On Fri, Apr 20, 2018 at 05:56:38PM +0100, Daniel P. Berrangé wrote:
> 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@redhat.com>
> ---
>  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)) {

After doing some perf tests I determined that while fnmatch is x14 slower
than strstr, this is not worth the code complexity - always using fnmatch
is invisible in real world perf tests.


Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|

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