[PATCH] Fix flaw in detecting log format

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

 



From: "Daniel P. Berrange" <berrange@xxxxxxxxxx>

The log message regex has been

[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{3}\+[0-9]{4}: [0-9]+: debug|info|warning|error :

The precedence of '|' is high though, so this is equivalent to matching

   [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{3}\+[0-9]{4}: [0-9]+: debug

Or

   info

Or

   warning

Or

   error :

Which is clearly not what it should have done. This caused the code to
skip over things which are not log messages. The solution is to simply
add brackets.

A test case is also added to validate correctness.

Signed-off-by: Daniel P. Berrange <berrange@xxxxxxxxxx>
---
 .gitignore         |  1 +
 src/util/virlog.c  |  5 +++-
 tests/Makefile.am  |  5 ++++
 tests/virlogtest.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 77 insertions(+), 1 deletion(-)
 create mode 100644 tests/virlogtest.c

diff --git a/.gitignore b/.gitignore
index b5824dc..db5abcd 100644
--- a/.gitignore
+++ b/.gitignore
@@ -210,6 +210,7 @@
 /tests/virkeycodetest
 /tests/virkeyfiletest
 /tests/virlockspacetest
+/tests/virlogtest
 /tests/virnet*test
 /tests/virportallocatortest
 /tests/virshtest
diff --git a/src/util/virlog.c b/src/util/virlog.c
index 7ee5117..cc5032f 100644
--- a/src/util/virlog.c
+++ b/src/util/virlog.c
@@ -83,7 +83,7 @@ static regex_t *virLogRegex = NULL;
 #define VIR_LOG_DATE_REGEX "[0-9]{4}-[0-9]{2}-[0-9]{2}"
 #define VIR_LOG_TIME_REGEX "[0-9]{2}:[0-9]{2}:[0-9]{2}\\.[0-9]{3}\\+[0-9]{4}"
 #define VIR_LOG_PID_REGEX "[0-9]+"
-#define VIR_LOG_LEVEL_REGEX "debug|info|warning|error"
+#define VIR_LOG_LEVEL_REGEX "(debug|info|warning|error)"
 
 #define VIR_LOG_REGEX \
     VIR_LOG_DATE_REGEX " " VIR_LOG_TIME_REGEX ": " \
@@ -1623,6 +1623,9 @@ virLogSetFromEnv(void)
 {
     char *debugEnv;
 
+    if (virLogInitialize() < 0)
+        return;
+
     debugEnv = getenv("LIBVIRT_DEBUG");
     if (debugEnv && *debugEnv)
         virLogParseDefaultPriority(debugEnv);
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 3eda522..250cd8c 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -126,6 +126,7 @@ test_programs = virshtest sockettest \
 	viridentitytest \
 	virkeycodetest \
 	virlockspacetest \
+	virlogtest \
 	virstringtest \
         virportallocatortest \
 	sysinfotest \
@@ -716,6 +717,10 @@ virlockspacetest_SOURCES = \
 	virlockspacetest.c testutils.h testutils.c
 virlockspacetest_LDADD = $(LDADDS)
 
+virlogtest_SOURCES = \
+	virlogtest.c testutils.h testutils.c
+virlogtest_LDADD = $(LDADDS)
+
 virportallocatortest_SOURCES = \
 	virportallocatortest.c testutils.h testutils.c
 virportallocatortest_LDADD = $(LDADDS)
diff --git a/tests/virlogtest.c b/tests/virlogtest.c
new file mode 100644
index 0000000..dfe0f75
--- /dev/null
+++ b/tests/virlogtest.c
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2013 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <config.h>
+
+#include "testutils.h"
+
+#include "virlog.h"
+
+struct testLogMatchData {
+    const char *str;
+    bool res;
+};
+
+static int
+testLogMatch(const void *opaque)
+{
+    const struct testLogMatchData *data = opaque;
+
+    bool got = virLogProbablyLogMessage(data->str);
+    if (got != data->res) {
+        fprintf(stderr, "Expected '%d' but got '%d' for '%s'\n",
+                data->res, got, data->str);
+        return -1;
+    }
+    return 0;
+}
+
+
+static int
+mymain(void)
+{
+    int ret = 0;
+
+#define TEST_LOG_MATCH(str, res)                                        \
+    do {                                                                \
+        struct testLogMatchData data = {                                \
+            str, res                                                    \
+        };                                                              \
+        if (virtTestRun("testLogMatch " # str, testLogMatch, &data) < 0) \
+            ret = -1;                                                   \
+    } while (0)
+
+    TEST_LOG_MATCH("2013-10-11 15:43:43.866+0000: 28302: info : libvirt version: 1.1.3", true);
+
+    TEST_LOG_MATCH("libvirt:  error : cannot execute binary /usr/libexec/libvirt_lxc: No such file or directory", false);
+
+    return ret;
+}
+
+VIRT_TEST_MAIN(mymain)
-- 
1.8.3.1

--
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]