From b3871dc8049819ae3e095555f451457567eb4ee3 Mon Sep 17 00:00:00 2001 From: Kai Koehne Date: Fri, 21 Feb 2014 11:39:12 +0100 Subject: Make parsing of categories in logging rules consistent. The documentation says that the left side of a logging rule has the syntax [.] with optional wildcard '*' as the first or the last character (or at both positions. However, so far we didn't allow qt.*.debug But what we did allow is implicit dropping of trailing '.', e.g. qt.* matched also 'qt' Fix these by splitting up the '.type' in advance, and then do string matching only on the 'real' category names. Change-Id: Iab50ad0fc673464e870f5ab8dfb3245d829b3107 Reviewed-by: Thiago Macieira --- src/corelib/io/qloggingregistry.cpp | 90 ++++++++++++++++++------------------- 1 file changed, 44 insertions(+), 46 deletions(-) (limited to 'src/corelib/io/qloggingregistry.cpp') diff --git a/src/corelib/io/qloggingregistry.cpp b/src/corelib/io/qloggingregistry.cpp index 2619743ff4..20eb2e13f5 100644 --- a/src/corelib/io/qloggingregistry.cpp +++ b/src/corelib/io/qloggingregistry.cpp @@ -65,11 +65,11 @@ QLoggingRule::QLoggingRule() : Constructs a logging rule. */ QLoggingRule::QLoggingRule(const QString &pattern, bool enabled) : - pattern(pattern), + messageType(-1), flags(Invalid), enabled(enabled) { - parse(); + parse(pattern); } /*! @@ -77,48 +77,33 @@ QLoggingRule::QLoggingRule(const QString &pattern, bool enabled) : Return value 1 means filter passed, 0 means filter doesn't influence this category, -1 means category doesn't pass this filter. */ -int QLoggingRule::pass(const QString &categoryName, QtMsgType msgType) const +int QLoggingRule::pass(const QString &cat, QtMsgType msgType) const { - QString fullCategory = categoryName; - switch (msgType) { - case QtDebugMsg: - fullCategory += QLatin1String(".debug"); - break; - case QtWarningMsg: - fullCategory += QLatin1String(".warning"); - break; - case QtCriticalMsg: - fullCategory += QLatin1String(".critical"); - break; - default: - break; - } + // check message type + if (messageType > -1 && messageType != msgType) + return 0; if (flags == FullText) { - // can be - // qtproject.org.debug = true - // or - // qtproject.org = true - if (pattern == categoryName - || pattern == fullCategory) + // full match + if (category == cat) return (enabled ? 1 : -1); + else + return 0; } - int idx = 0; - if (flags == MidFilter) { - // e.g. *.qtproject* - idx = fullCategory.indexOf(pattern); - if (idx >= 0) - return (enabled ? 1 : -1); - } else { - idx = fullCategory.indexOf(pattern); - if (flags == LeftFilter) { - // e.g. org.qtproject.* + const int idx = cat.indexOf(category); + if (idx >= 0) { + if (flags == MidFilter) { + // matches somewhere + if (idx >= 0) + return (enabled ? 1 : -1); + } else if (flags == LeftFilter) { + // matches left if (idx == 0) return (enabled ? 1 : -1); } else if (flags == RightFilter) { - // e.g. *.qtproject - if (idx == (fullCategory.count() - pattern.count())) + // matches right + if (idx == (cat.count() - category.count())) return (enabled ? 1 : -1); } } @@ -127,28 +112,41 @@ int QLoggingRule::pass(const QString &categoryName, QtMsgType msgType) const /*! \internal - Parses the category and checks which kind of wildcard the filter can contain. + Parses \a pattern. Allowed is f.ex.: - org.qtproject.logging FullText - org.qtproject.* LeftFilter - *.qtproject RightFilter - *.qtproject* MidFilter + qt.core.io.debug FullText, QtDebugMsg + qt.core.* LeftFilter, all types + *.io.warning RightFilter, QtWarningMsg + *.core.* MidFilter */ -void QLoggingRule::parse() +void QLoggingRule::parse(const QString &pattern) { - int index = pattern.indexOf(QLatin1Char('*')); + category = pattern; + // strip trailing ".messagetype" + if (pattern.endsWith(QLatin1String(".debug"))) { + category.chop(strlen(".debug")); + messageType = QtDebugMsg; + } else if (pattern.endsWith(QLatin1String(".warning"))) { + category.chop(strlen(".warning")); + messageType = QtWarningMsg; + } else if (pattern.endsWith(QLatin1String(".critical"))) { + category.chop(strlen(".critical")); + messageType = QtCriticalMsg; + } + + int index = category.indexOf(QLatin1Char('*')); if (index < 0) { flags = FullText; } else { flags = Invalid; if (index == 0) { flags |= RightFilter; - pattern = pattern.remove(0, 1); - index = pattern.indexOf(QLatin1Char('*')); + category.remove(0, 1); + index = category.indexOf(QLatin1Char('*')); } - if (index == (pattern.length() - 1)) { + if (index == (category.length() - 1)) { flags |= LeftFilter; - pattern = pattern.remove(pattern.length() - 1, 1); + category.chop(1); } } } -- cgit v1.2.3