File libvirt-Add-method-for-checking-if-a-string-is-probably-a-log-message.patch of Package libvirt

From babe2b00ee877cfa37b0eab84bf6cfb9ec982916 Mon Sep 17 00:00:00 2001
Message-Id: <babe2b00ee877cfa37b0eab84bf6cfb9ec982916.1373271643.git.jdenemar@redhat.com>
From: "Daniel P. Berrange" <berrange@redhat.com>
Date: Mon, 4 Mar 2013 20:46:32 +0000
Subject: [PATCH] Add method for checking if a string is (probably) a log
 message

https://bugzilla.redhat.com/show_bug.cgi?id=954248

When reading log output from QEMU/LXC we need to skip over any
libvirt log messages. Currently the QEMU driver checks for a
fixed string, but this is better done with a regex. Add a method
virLogProbablyLogMessage to do a regex check

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
(cherry picked from commit f3d312f6c8eb640b3973e99dd96b96c9b5981235)

Conflicts:
	src/libvirt_private.syms - virlog.h is called logging.h in RHEL
---
 src/libvirt_private.syms |  1 +
 src/util/logging.c       | 34 ++++++++++++++++++++++++++++++++++
 src/util/logging.h       |  3 +++
 3 files changed, 38 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 4669293..754a347 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -811,6 +811,7 @@ virLogParseDefaultPriority;
 virLogParseFilters;
 virLogParseOutputs;
 virLogPriorityFromSyslog;
+virLogProbablyLogMessage;
 virLogReset;
 virLogSetBufferSize;
 virLogSetDefaultPriority;
diff --git a/src/util/logging.c b/src/util/logging.c
index ae631a9..eab3fea 100644
--- a/src/util/logging.c
+++ b/src/util/logging.c
@@ -32,6 +32,7 @@
 #include <unistd.h>
 #include <signal.h>
 #include <execinfo.h>
+#include <regex.h>
 #if HAVE_SYSLOG_H
 # include <syslog.h>
 #endif
@@ -56,6 +57,17 @@ static char *virLogBuffer = NULL;
 static int virLogLen = 0;
 static int virLogStart = 0;
 static int virLogEnd = 0;
+static regex_t *virLogRegex = NULL;
+
+
+#define VIR_LOG_DATE_REGEX "[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]"
+#define VIR_LOG_TIME_REGEX "[0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9][0-9]+[0-9][0-9][0-9][0-9]"
+#define VIR_LOG_PID_REGEX "[0-9]+"
+#define VIR_LOG_LEVEL_REGEX "debug|info|warning|error"
+
+#define VIR_LOG_REGEX \
+    VIR_LOG_DATE_REGEX " " VIR_LOG_TIME_REGEX ": " \
+    VIR_LOG_PID_REGEX ": " VIR_LOG_LEVEL_REGEX " : "
 
 /*
  * Filters are used to refine the rules on what to keep or drop
@@ -172,6 +184,12 @@ static int virLogOnceInit(void)
     virLogStart = 0;
     virLogEnd = 0;
     virLogDefaultPriority = VIR_LOG_DEFAULT;
+
+    if (VIR_ALLOC(virLogRegex) >= 0) {
+        if (regcomp(virLogRegex, VIR_LOG_REGEX, REG_EXTENDED) != 0)
+            VIR_FREE(virLogRegex);
+    }
+
     virLogUnlock();
     if (pbm)
         VIR_WARN("%s", pbm);
@@ -1255,3 +1273,19 @@ void virLogSetFromEnv(void) {
     if (debugEnv && *debugEnv)
         virLogParseOutputs(debugEnv);
 }
+
+
+/*
+ * Returns a true value if the first line in @str is
+ * probably a log message generated by the libvirt
+ * logging layer
+ */
+bool virLogProbablyLogMessage(const char *str)
+{
+    bool ret = false;
+    if (!virLogRegex)
+        return false;
+    if (regexec(virLogRegex, str, 0, NULL, 0) == 0)
+        ret = true;
+    return ret;
+}
diff --git a/src/util/logging.h b/src/util/logging.h
index bc1355b..eacb350 100644
--- a/src/util/logging.h
+++ b/src/util/logging.h
@@ -149,4 +149,7 @@ extern void virLogVMessage(const char *category, int priority,
                            va_list vargs) ATTRIBUTE_FMT_PRINTF(6, 0);
 extern int virLogSetBufferSize(int size);
 extern void virLogEmergencyDumpAll(int signum);
+
+bool virLogProbablyLogMessage(const char *str);
+
 #endif
-- 
1.8.2.1

openSUSE Build Service is sponsored by