From f3d312f6c8eb640b3973e99dd96b96c9b5981235 Mon Sep 17 00:00:00 2001 From: "Daniel P. Berrange" Date: Mon, 4 Mar 2013 20:46:32 +0000 Subject: [PATCH] Add method for checking if a string is (probably) a log message 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 --- src/libvirt_private.syms | 1 + src/util/virlog.c | 34 ++++++++++++++++++++++++++++++++++ src/util/virlog.h | 3 +++ 3 files changed, 38 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index ed464798fd..599b71edd3 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1429,6 +1429,7 @@ virLogMessage; virLogParseDefaultPriority; virLogParseFilters; virLogParseOutputs; +virLogProbablyLogMessage; virLogReset; virLogSetBufferSize; virLogSetDefaultPriority; diff --git a/src/util/virlog.c b/src/util/virlog.c index 24ec9d3a7f..130bdff1c2 100644 --- a/src/util/virlog.c +++ b/src/util/virlog.c @@ -32,6 +32,7 @@ #include #include #include +#include #if HAVE_SYSLOG_H # include #endif @@ -75,6 +76,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 @@ -209,6 +221,12 @@ 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); @@ -1587,3 +1605,19 @@ 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/virlog.h b/src/util/virlog.h index aa81d6a578..6b83245ece 100644 --- a/src/util/virlog.h +++ b/src/util/virlog.h @@ -188,4 +188,7 @@ extern void virLogVMessage(virLogSource src, va_list vargs) ATTRIBUTE_FMT_PRINTF(7, 0); extern int virLogSetBufferSize(int size); extern void virLogEmergencyDumpAll(int signum); + +bool virLogProbablyLogMessage(const char *str); + #endif