diff options
author | Kai Koehne <kai.koehne@digia.com> | 2014-07-11 12:36:30 +0200 |
---|---|---|
committer | Kai Koehne <kai.koehne@digia.com> | 2014-07-23 15:38:10 +0200 |
commit | ea34893b8ff4374fb631da19101ea5ea998eb9dc (patch) | |
tree | 40f5ed901a24d6f80639ad47b4b67794c6ab5cb8 /src/corelib/io/qloggingcategory.cpp | |
parent | c4a2d6ca1ea3e1411391978f089c0b360c833f1b (diff) |
Support setting a default severity level for QLoggingCategory
Allow to alter the default configuration for categories by passing a
message type: All message types with lower severity are disabled in this
category.
This is useful for libraries, which shouldn't mess with the category
registry itself: Setting rules, a category filter ... might cause
conflicts and ordering problems, so this API should be reserved to the
specific application.
For the Qt categories, we have code in the default category filter that
disables the 'debug' category. However, this is hardcoded, and there's no
way so far for other libraries to get the same behavior. With this patch
one can get the same behavior:
Q_LOGGING_CATEGORY(DRIVER_USB_EVENTS, "driver.usb.events", QtWarningMsg);
[ChangeLog][QtCore][Logging] Added QtMsgType argument to QLoggingCategory
constructor and Q_LOGGING_CATEGORY macro that controls the default
category configuration.
Change-Id: Ib2902f755f9f7285d79888ec30e8f3cef95ae628
Reviewed-by: Alex Blasche <alexander.blasche@digia.com>
Diffstat (limited to 'src/corelib/io/qloggingcategory.cpp')
-rw-r--r-- | src/corelib/io/qloggingcategory.cpp | 64 |
1 files changed, 54 insertions, 10 deletions
diff --git a/src/corelib/io/qloggingcategory.cpp b/src/corelib/io/qloggingcategory.cpp index 0f2c7de053..63241f6698 100644 --- a/src/corelib/io/qloggingcategory.cpp +++ b/src/corelib/io/qloggingcategory.cpp @@ -101,13 +101,21 @@ static void setBoolLane(QBasicAtomicInt *atomic, bool enable, int shift) \section1 Default category configuration - In the default configuration \l isWarningEnabled() , \l isDebugEnabled() and - \l isCriticalEnabled() will return \c true. + Both the QLoggingCategory constructor and the Q_LOGGING_CATEGORY() macro + accept an optional QtMsgType argument, which disables all message types with + a lower severity. That is, a category declared with + + \snippet qloggingcategory/main.cpp 5 + + will log messages of type \c QtWarningMsg, \c QtCriticalMsg, \c QtFatalMsg, but will + ignore messages of type \c QtDebugMsg. + + If no argument is passed, all messages will be logged. \section1 Configuring Categories - Categories can be centrally configured by either setting logging rules, - or by installing a custom filter. + The default configuration of categories can be overridden either by setting logging + rules, or by installing a custom filter. \section2 Logging Rules @@ -183,7 +191,7 @@ static void setBoolLane(QBasicAtomicInt *atomic, bool enable, int shift) /*! Constructs a QLoggingCategory object with the provided \a category name. - The object becomes the local identifier for the category. + All message types for this category are enabled by default. If \a category is \c{0}, the category name is changed to \c "default". */ @@ -191,6 +199,26 @@ QLoggingCategory::QLoggingCategory(const char *category) : d(0), name(0) { + init(category, QtDebugMsg); +} + +/*! + Constructs a QLoggingCategory object with the provided \a category name, + and enables all messages with types more severe or equal than \a enableForLevel. + + If \a category is \c{0}, the category name is changed to \c "default". + + \since 5.4 +*/ +QLoggingCategory::QLoggingCategory(const char *category, QtMsgType enableForLevel) + : d(0), + name(0) +{ + init(category, enableForLevel); +} + +void QLoggingCategory::init(const char *category, QtMsgType severityLevel) +{ enabled.store(0x01010101); // enabledDebug = enabledWarning = enabledCritical = true; const bool isDefaultCategory @@ -198,14 +226,13 @@ QLoggingCategory::QLoggingCategory(const char *category) // normalize "default" category name, so that we can just do // pointer comparison in QLoggingRegistry::updateCategory - if (isDefaultCategory) { + if (isDefaultCategory) name = qtDefaultCategoryName; - } else { + else name = category; - } if (QLoggingRegistry *reg = QLoggingRegistry::instance()) - reg->registerCategory(this); + reg->registerCategory(this, severityLevel); } /*! @@ -522,7 +549,7 @@ void QLoggingCategory::setFilterRules(const QString &rules) \since 5.2 Defines a logging category \a name, and makes it configurable under the - \a string identifier. + \a string identifier. By default, all message types are enabled. Only one translation unit in a library or executable can define a category with a specific name. @@ -530,4 +557,21 @@ void QLoggingCategory::setFilterRules(const QString &rules) This macro must be used outside of a class or method. */ +/*! + \macro Q_LOGGING_CATEGORY(name, string, msgType) + \sa Q_DECLARE_LOGGING_CATEGORY() + \relates QLoggingCategory + \since 5.4 + + Defines a logging category \a name, and makes it configurable under the + \a string identifier. By default, messages of QtMsgType \a msgType + and more severe are enabled, types with a lower severity are disabled. + + Only one translation unit in a library or executable can define a category + with a specific name. + + This macro must be used outside of a class or method. It is only defined + if variadic macros are supported. +*/ + QT_END_NAMESPACE |