render / rpms / libvirt

Forked from rpms/libvirt 7 months ago
Clone
43fe83
From 7b932543265a2e0abd6b2a5428fe1ad40ea035ec Mon Sep 17 00:00:00 2001
43fe83
Message-Id: <7b932543265a2e0abd6b2a5428fe1ad40ea035ec.1381871412.git.jdenemar@redhat.com>
43fe83
From: "Daniel P. Berrange" <berrange@redhat.com>
43fe83
Date: Mon, 14 Oct 2013 16:45:20 +0100
43fe83
Subject: [PATCH] Fix flaw in detecting log format
43fe83
43fe83
For
43fe83
43fe83
  https://bugzilla.redhat.com/show_bug.cgi?id=927072
43fe83
43fe83
The log message regex has been
43fe83
43fe83
[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 :
43fe83
43fe83
The precedence of '|' is high though, so this is equivalent to matching
43fe83
43fe83
   [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
43fe83
43fe83
Or
43fe83
43fe83
   info
43fe83
43fe83
Or
43fe83
43fe83
   warning
43fe83
43fe83
Or
43fe83
43fe83
   error :
43fe83
43fe83
Which is clearly not what it should have done. This caused the code to
43fe83
skip over things which are not log messages. The solution is to simply
43fe83
add brackets.
43fe83
43fe83
A test case is also added to validate correctness.
43fe83
43fe83
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
43fe83
(cherry picked from commit 5787f0b95ed5a58be020836bda4b27fa3538086c)
43fe83
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
43fe83
---
43fe83
 .gitignore         |  1 +
43fe83
 src/util/virlog.c  |  5 +++-
43fe83
 tests/Makefile.am  |  5 ++++
43fe83
 tests/virlogtest.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
43fe83
 4 files changed, 77 insertions(+), 1 deletion(-)
43fe83
 create mode 100644 tests/virlogtest.c
43fe83
43fe83
diff --git a/src/util/virlog.c b/src/util/virlog.c
43fe83
index 047a131..daf964b 100644
43fe83
--- a/src/util/virlog.c
43fe83
+++ b/src/util/virlog.c
43fe83
@@ -83,7 +83,7 @@ static regex_t *virLogRegex = NULL;
43fe83
 #define VIR_LOG_DATE_REGEX "[0-9]{4}-[0-9]{2}-[0-9]{2}"
43fe83
 #define VIR_LOG_TIME_REGEX "[0-9]{2}:[0-9]{2}:[0-9]{2}\\.[0-9]{3}\\+[0-9]{4}"
43fe83
 #define VIR_LOG_PID_REGEX "[0-9]+"
43fe83
-#define VIR_LOG_LEVEL_REGEX "debug|info|warning|error"
43fe83
+#define VIR_LOG_LEVEL_REGEX "(debug|info|warning|error)"
43fe83
 
43fe83
 #define VIR_LOG_REGEX \
43fe83
     VIR_LOG_DATE_REGEX " " VIR_LOG_TIME_REGEX ": " \
43fe83
@@ -1623,6 +1623,9 @@ virLogSetFromEnv(void)
43fe83
 {
43fe83
     char *debugEnv;
43fe83
 
43fe83
+    if (virLogInitialize() < 0)
43fe83
+        return;
43fe83
+
43fe83
     debugEnv = getenv("LIBVIRT_DEBUG");
43fe83
     if (debugEnv && *debugEnv)
43fe83
         virLogParseDefaultPriority(debugEnv);
43fe83
diff --git a/tests/Makefile.am b/tests/Makefile.am
43fe83
index 4787752..b794005 100644
43fe83
--- a/tests/Makefile.am
43fe83
+++ b/tests/Makefile.am
43fe83
@@ -122,6 +122,7 @@ test_programs = virshtest sockettest \
43fe83
 	viridentitytest \
43fe83
 	virkeycodetest \
43fe83
 	virlockspacetest \
43fe83
+	virlogtest \
43fe83
 	virstringtest \
43fe83
         virportallocatortest \
43fe83
 	sysinfotest \
43fe83
@@ -643,6 +644,10 @@ virlockspacetest_SOURCES = \
43fe83
 	virlockspacetest.c testutils.h testutils.c
43fe83
 virlockspacetest_LDADD = $(LDADDS)
43fe83
 
43fe83
+virlogtest_SOURCES = \
43fe83
+	virlogtest.c testutils.h testutils.c
43fe83
+virlogtest_LDADD = $(LDADDS)
43fe83
+
43fe83
 virportallocatortest_SOURCES = \
43fe83
 	virportallocatortest.c testutils.h testutils.c
43fe83
 virportallocatortest_LDADD = $(LDADDS)
43fe83
diff --git a/tests/virlogtest.c b/tests/virlogtest.c
43fe83
new file mode 100644
43fe83
index 0000000..ab4c2e8
43fe83
--- /dev/null
43fe83
+++ b/tests/virlogtest.c
43fe83
@@ -0,0 +1,67 @@
43fe83
+/*
43fe83
+ * Copyright (C) 2013 Red Hat, Inc.
43fe83
+ *
43fe83
+ * This library is free software; you can redistribute it and/or
43fe83
+ * modify it under the terms of the GNU Lesser General Public
43fe83
+ * License as published by the Free Software Foundation; either
43fe83
+ * version 2.1 of the License, or (at your option) any later version.
43fe83
+ *
43fe83
+ * This library is distributed in the hope that it will be useful,
43fe83
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
43fe83
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
43fe83
+ * Lesser General Public License for more details.
43fe83
+ *
43fe83
+ * You should have received a copy of the GNU Lesser General Public
43fe83
+ * License along with this library.  If not, see
43fe83
+ * <http://www.gnu.org/licenses/>.
43fe83
+ *
43fe83
+ */
43fe83
+
43fe83
+#include <config.h>
43fe83
+
43fe83
+#include "testutils.h"
43fe83
+
43fe83
+#include "virlog.h"
43fe83
+
43fe83
+struct testLogMatchData {
43fe83
+    const char *str;
43fe83
+    bool res;
43fe83
+};
43fe83
+
43fe83
+static int
43fe83
+testLogMatch(const void *opaque)
43fe83
+{
43fe83
+    const struct testLogMatchData *data = opaque;
43fe83
+
43fe83
+    bool got = virLogProbablyLogMessage(data->str);
43fe83
+    if (got != data->res) {
43fe83
+        fprintf(stderr, "Expected '%d' but got '%d' for '%s'\n",
43fe83
+                data->res, got, data->str);
43fe83
+        return -1;
43fe83
+    }
43fe83
+    return 0;
43fe83
+}
43fe83
+
43fe83
+
43fe83
+static int
43fe83
+mymain(void)
43fe83
+{
43fe83
+    int ret = 0;
43fe83
+
43fe83
+#define TEST_LOG_MATCH(str, res)                                        \
43fe83
+    do {                                                                \
43fe83
+        struct testLogMatchData data = {                                \
43fe83
+            str, res                                                    \
43fe83
+        };                                                              \
43fe83
+        if (virtTestRun("testLogMatch " # str, 1, testLogMatch, &data) < 0) \
43fe83
+            ret = -1;                                                   \
43fe83
+    } while (0)
43fe83
+
43fe83
+    TEST_LOG_MATCH("2013-10-11 15:43:43.866+0000: 28302: info : libvirt version: 1.1.3", true);
43fe83
+
43fe83
+    TEST_LOG_MATCH("libvirt:  error : cannot execute binary /usr/libexec/libvirt_lxc: No such file or directory", false);
43fe83
+
43fe83
+    return ret;
43fe83
+}
43fe83
+
43fe83
+VIRT_TEST_MAIN(mymain)
43fe83
-- 
43fe83
1.8.3.2
43fe83