From 7f783fac8f6c54217e2f4eb42fcf2f8b0236b167 Mon Sep 17 00:00:00 2001 From: Mathew Sutcliffe Date: Tue, 21 Oct 2014 01:47:11 +0100 Subject: [PATCH] refs #338 CLogHandler: cleaned up the logic for selectively enabling console output, with the help of the partial ordering feature of CLogPattern::isProperSubsetOf. --- src/blackmisc/loghandler.cpp | 15 ++------------- src/blackmisc/loghandler.h | 31 +++++++++++++++++++++++++++---- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/blackmisc/loghandler.cpp b/src/blackmisc/loghandler.cpp index a1a4c6f0b..23c09fdb6 100644 --- a/src/blackmisc/loghandler.cpp +++ b/src/blackmisc/loghandler.cpp @@ -54,7 +54,7 @@ namespace BlackMisc auto it = std::find_if(m_patternHandlers.begin(), m_patternHandlers.end(), finder); if (it == m_patternHandlers.end()) { - auto *handler = new CLogPatternHandler(this, m_enableFallThrough); + auto *handler = new CLogPatternHandler(this); topologicallySortedInsert(m_patternHandlers, PatternPair(pattern, handler), comparator); return handler; } @@ -77,22 +77,11 @@ namespace BlackMisc return m_handlers; } - void CLogHandler::enableConsoleOutput(bool enable) - { - Q_ASSERT(thread() == QThread::currentThread()); - - m_enableFallThrough = enable; - for (const auto &pair : m_patternHandlers) - { - pair.second->enableConsoleOutput(enable); - } - } - bool CLogHandler::isFallThroughEnabled(const QList &handlers) const { for (const auto *handler : handlers) { - if (handler->m_enableFallThrough != m_enableFallThrough) + if (! handler->m_inheritFallThrough) { return handler->m_enableFallThrough; } diff --git a/src/blackmisc/loghandler.h b/src/blackmisc/loghandler.h index 9dea7dc7a..5e586fcfc 100644 --- a/src/blackmisc/loghandler.h +++ b/src/blackmisc/loghandler.h @@ -61,7 +61,11 @@ namespace BlackMisc void logRemoteMessage(const BlackMisc::CStatusMessage &message); //! Enable or disable the default Qt handler. - void enableConsoleOutput(bool enable); + void enableConsoleOutput(bool enable) + { + Q_ASSERT(thread() == QThread::currentThread()); + m_enableFallThrough = enable; + } private: void logMessage(const BlackMisc::CStatusMessage &message); @@ -90,7 +94,25 @@ namespace BlackMisc * CLogPatternHandler or the base CLogHandler, if this handler's pattern is a subset of the * other handler's pattern. Which is to say, more specific patterns can override less specific patterns. */ - void enableConsoleOutput(bool enable) { Q_ASSERT(thread() == QThread::currentThread()); m_enableFallThrough = enable; } + void enableConsoleOutput(bool enable) + { + Q_ASSERT(thread() == QThread::currentThread()); + m_inheritFallThrough = false; + m_enableFallThrough = enable; + } + + /*! + * The policy of whether to enable or disable the default Qt handler for messages which match + * the relevant pattern will be inherited from the base CLogHandler, or from another CLogPatternHandler + * which matches a superset of the messages which this one matches. + * + * This is the default, but can be used to reverse the effect of calling enableConsoleOutput. + */ + void inheritConsoleOutput() + { + Q_ASSERT(thread() == QThread::currentThread()); + m_inheritFallThrough = true; + } signals: /*! @@ -108,8 +130,9 @@ namespace BlackMisc private: friend class CLogHandler; - CLogPatternHandler(QObject *parent, bool enableFallThrough) : QObject(parent), m_enableFallThrough(enableFallThrough) {} - bool m_enableFallThrough; + CLogPatternHandler(QObject *parent) : QObject(parent) {} + bool m_inheritFallThrough = true; + bool m_enableFallThrough = true; bool canBeDeleted() {