From 77a45f2ff03ebd0a7af46f9a13e627aadb4012a0 Mon Sep 17 00:00:00 2001 From: Erik Skultety Date: Wed, 5 Oct 2016 14:41:51 +0200 Subject: [PATCH] virlog: Introduce virLogParseFilter Same as for outputs, introduce a new method, that is basically the same as virLogParseAndDefineFilter with the difference that it does not define the filter. It rather returns a newly created object that needs to be inserted into a list and then defined separately. Signed-off-by: Erik Skultety --- src/libvirt_private.syms | 1 + src/util/virlog.c | 77 ++++++++++++++++++++++++++++++++++++++++ src/util/virlog.h | 1 + 3 files changed, 79 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index baeaef8d18..24ac57041a 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1890,6 +1890,7 @@ virLogOutputNew; virLogParseAndDefineFilters; virLogParseAndDefineOutputs; virLogParseDefaultPriority; +virLogParseFilter; virLogParseOutput; virLogPriorityFromSyslog; virLogProbablyLogMessage; diff --git a/src/util/virlog.c b/src/util/virlog.c index 9879c4fc6a..150deb3d4e 100644 --- a/src/util/virlog.c +++ b/src/util/virlog.c @@ -1962,3 +1962,80 @@ virLogParseOutput(const char *src) virStringFreeList(tokens); return ret; } + + +/** + * virLogParseFilter: + * @src: string defining a single filter + * + * The format of @src should be one of the following: + * x:name - filter affecting all modules which match 'name' + * x:+name + * + * '+' - hints the logger to also include a stack trace for every message + * 'name' - match string which either matches a name of a directory in + * libvirt's source tree which in turn affects all modules in + * that directory or it can matches a specific module within a + * directory, e.g. 'util.file' will only affect messages from + * module virfile.c inside src/util/ directory + * 'x' - minimal priority level which acts as a filter meaning that only + * messages with priority level greater than or equal to 'x' will be + * sent to output; supported values for 'x' are as follows: + * 1: DEBUG + * 2: INFO + * 3: WARNING + * 4: ERROR + * + * Parses @src string into a logging object type. + * + * Returns a newly created logging object from @src on success or NULL in case + * of an error. + */ +virLogFilterPtr +virLogParseFilter(const char *src) +{ + virLogFilterPtr ret = NULL; + size_t count = 0; + virLogPriority prio; + char **tokens = NULL; + unsigned int flags = 0; + char *match = NULL; + + VIR_DEBUG("filter=%s", src); + + /* split our format prio:match_str to tokens and parse them individually */ + if (!(tokens = virStringSplitCount(src, ":", 0, &count)) || count != 2) { + virReportError(VIR_ERR_INVALID_ARG, + _("Malformed format for filter '%s'"), src); + return NULL; + } + + if (virStrToLong_uip(tokens[0], NULL, 10, &prio) < 0 || + (prio < VIR_LOG_DEBUG) || (prio > VIR_LOG_ERROR)) { + virReportError(VIR_ERR_INVALID_ARG, + _("Invalid priority '%s' for output '%s'"), + tokens[0], src); + goto cleanup; + } + + match = tokens[1]; + if (match[0] == '+') { + flags |= VIR_LOG_STACK_TRACE; + match++; + } + + /* match string cannot comprise just from a single '+' */ + if (!*match) { + virReportError(VIR_ERR_INVALID_ARG, + _("Invalid match string '%s'"), tokens[1]); + + goto cleanup; + } + + if (!(ret = virLogFilterNew(match, prio, flags))) + goto cleanup; + + cleanup: + virStringFreeList(tokens); + return ret; +} diff --git a/src/util/virlog.h b/src/util/virlog.h index e1cf81e109..568b8fa44a 100644 --- a/src/util/virlog.h +++ b/src/util/virlog.h @@ -241,5 +241,6 @@ int virLogDefineOutputs(virLogOutputPtr *outputs, size_t noutputs) ATTRIBUTE_NONNULL(1); int virLogDefineFilters(virLogFilterPtr *filters, size_t nfilters); virLogOutputPtr virLogParseOutput(const char *src) ATTRIBUTE_NONNULL(1); +virLogFilterPtr virLogParseFilter(const char *src) ATTRIBUTE_NONNULL(1); #endif