diff options
author | Tomasz Olszak <olszak.tomasz@gmail.com> | 2018-03-22 11:25:59 +0100 |
---|---|---|
committer | Tomasz Olszak <olszak.tomasz@gmail.com> | 2018-06-25 10:25:40 +0000 |
commit | f319fcf70917c392913cf6db6115d18623dd4fd5 (patch) | |
tree | 22ebe95ccd8076b1f7b17ba12e3c86b4347da128 | |
parent | 8597f74e520c18e0ac1c7a90e02a3b5ba33d4753 (diff) |
Add defaultLogLevel to LoggingCategory
Add possibility to define default logging category log level.
Just like in QLoggingCategory constructor.
[ChangeLog][QML Elements][LoggingCategory] Added defaultLogLevel property.
It is possible to define default log level that LoggingCategory
is enabled for.
Task-number: QTBUG-67094
Change-Id: I12557dfb7c228c40b325d0dccde4c525acae0300
Reviewed-by: Michael Brasser <michael.brasser@live.com>
-rw-r--r-- | src/imports/qtqml/plugins.qmltypes | 15 | ||||
-rw-r--r-- | src/qml/qml/qqmlengine.cpp | 4 | ||||
-rw-r--r-- | src/qml/qml/qqmlloggingcategory.cpp | 36 | ||||
-rw-r--r-- | src/qml/qml/qqmlloggingcategory_p.h | 13 | ||||
-rw-r--r-- | tests/auto/qml/qqmlconsole/data/categorized_logging.qml | 14 | ||||
-rw-r--r-- | tests/auto/qml/qqmlconsole/tst_qqmlconsole.cpp | 13 |
6 files changed, 86 insertions, 9 deletions
diff --git a/src/imports/qtqml/plugins.qmltypes b/src/imports/qtqml/plugins.qmltypes index 82333627a0..caa04b04bb 100644 --- a/src/imports/qtqml/plugins.qmltypes +++ b/src/imports/qtqml/plugins.qmltypes @@ -198,9 +198,20 @@ Module { Component { name: "QQmlLoggingCategory" prototype: "QObject" - exports: ["QtQml/LoggingCategory 2.8"] - exportMetaObjectRevisions: [0] + exports: ["QtQml/LoggingCategory 2.12", "QtQml/LoggingCategory 2.8"] + exportMetaObjectRevisions: [1, 0] + Enum { + name: "DefaultLogLevel" + values: { + "Debug": 0, + "Info": 4, + "Warning": 1, + "Critical": 2, + "Fatal": 3 + } + } Property { name: "name"; type: "string" } + Property { name: "defaultLogLevel"; revision: 1; type: "DefaultLogLevel" } } Component { name: "QQmlTimer" diff --git a/src/qml/qml/qqmlengine.cpp b/src/qml/qml/qqmlengine.cpp index 759d86c789..119120572c 100644 --- a/src/qml/qml/qqmlengine.cpp +++ b/src/qml/qml/qqmlengine.cpp @@ -228,7 +228,9 @@ void QQmlEnginePrivate::registerBaseTypes(const char *uri, int versionMajor, int qmlRegisterType<QQmlInstantiator>(uri, versionMajor, (versionMinor < 1 ? 1 : versionMinor), "Instantiator"); //Only available in >=2.1 qmlRegisterCustomType<QQmlConnections>(uri, versionMajor, versionMinor,"Connections", new QQmlConnectionsParser); qmlRegisterType<QQmlInstanceModel>(); - qmlRegisterType<QQmlLoggingCategory>(uri, versionMajor, (versionMinor < 8 ? 8 : versionMinor), "LoggingCategory"); //Only available in >=2.8 + + qmlRegisterType<QQmlLoggingCategory>(uri, versionMajor, 8, "LoggingCategory"); //Only available in >=2.8 + qmlRegisterType<QQmlLoggingCategory,1>(uri, versionMajor, 12, "LoggingCategory"); //Only available in >=2.12 } diff --git a/src/qml/qml/qqmlloggingcategory.cpp b/src/qml/qml/qqmlloggingcategory.cpp index 597fe458fa..b59a26e17e 100644 --- a/src/qml/qml/qqmlloggingcategory.cpp +++ b/src/qml/qml/qqmlloggingcategory.cpp @@ -59,6 +59,7 @@ LoggingCategory { id: category name: "com.qt.category" + defaultLogLevel: LoggingCategory.Warning } Component.onCompleted: { @@ -84,6 +85,17 @@ \sa QLoggingCategory::categoryName() */ +/*! + \qmlproperty enumeration QtQml::LoggingCategory::defaultLogLevel + \since 5.12 + + Holds the default log level of the logging category. By default it is + created with the LoggingCategory.Debug log level. + + \note This property needs to be set when declaring the LoggingCategory + and cannot be changed later. +*/ + QQmlLoggingCategory::QQmlLoggingCategory(QObject *parent) : QObject(parent) , m_initialized(false) @@ -99,6 +111,11 @@ QString QQmlLoggingCategory::name() const return QString::fromUtf8(m_name); } +QQmlLoggingCategory::DefaultLogLevel QQmlLoggingCategory::defaultLogLevel() const +{ + return m_defaultLogLevel; +} + QLoggingCategory *QQmlLoggingCategory::category() const { return m_category.data(); @@ -111,10 +128,25 @@ void QQmlLoggingCategory::classBegin() void QQmlLoggingCategory::componentComplete() { m_initialized = true; - if (m_name.isNull()) + if (m_name.isNull()) { qmlWarning(this) << QLatin1String("Declaring the name of the LoggingCategory is mandatory and cannot be changed later !"); + } else { + QScopedPointer<QLoggingCategory> category(new QLoggingCategory(m_name.constData(), QtMsgType(m_defaultLogLevel))); + m_category.swap(category); + } } +void QQmlLoggingCategory::setDefaultLogLevel(DefaultLogLevel defaultLogLevel) +{ + if (m_initialized) { + qmlWarning(this) << QLatin1String("The defaultLogLevel of a LoggingCategory cannot be changed after the Item is created"); + return; + } + + m_defaultLogLevel = defaultLogLevel; +} + + void QQmlLoggingCategory::setName(const QString &name) { if (m_initialized) { @@ -123,8 +155,6 @@ void QQmlLoggingCategory::setName(const QString &name) } m_name = name.toUtf8(); - QScopedPointer<QLoggingCategory> category(new QLoggingCategory(m_name.constData())); - m_category.swap(category); } #include "moc_qqmlloggingcategory_p.cpp" diff --git a/src/qml/qml/qqmlloggingcategory_p.h b/src/qml/qml/qqmlloggingcategory_p.h index 544db1fe33..ece06e04b4 100644 --- a/src/qml/qml/qqmlloggingcategory_p.h +++ b/src/qml/qml/qqmlloggingcategory_p.h @@ -65,11 +65,23 @@ class QQmlLoggingCategory : public QObject, public QQmlParserStatus Q_INTERFACES(QQmlParserStatus) Q_PROPERTY(QString name READ name WRITE setName) + Q_PROPERTY(DefaultLogLevel defaultLogLevel READ defaultLogLevel WRITE setDefaultLogLevel REVISION 1) public: + enum DefaultLogLevel { + Debug = QtDebugMsg, + Info = QtInfoMsg, + Warning = QtWarningMsg, + Critical = QtCriticalMsg, + Fatal = QtFatalMsg + }; + Q_ENUM(DefaultLogLevel); + QQmlLoggingCategory(QObject *parent = nullptr); virtual ~QQmlLoggingCategory(); + DefaultLogLevel defaultLogLevel() const; + void setDefaultLogLevel(DefaultLogLevel defaultLogLevel); QString name() const; void setName(const QString &name); @@ -81,6 +93,7 @@ public: private: QByteArray m_name; QScopedPointer<QLoggingCategory> m_category; + DefaultLogLevel m_defaultLogLevel = Debug; bool m_initialized; }; diff --git a/tests/auto/qml/qqmlconsole/data/categorized_logging.qml b/tests/auto/qml/qqmlconsole/data/categorized_logging.qml index d19b6ecc41..d593f0dfa1 100644 --- a/tests/auto/qml/qqmlconsole/data/categorized_logging.qml +++ b/tests/auto/qml/qqmlconsole/data/categorized_logging.qml @@ -37,7 +37,7 @@ ** ****************************************************************************/ -import QtQuick 2.8 +import QtQuick 2.12 Item { id:root @@ -48,6 +48,12 @@ Item { } LoggingCategory { + id: testCategoryStartingFromWarning + name: "qt.test.warning" + defaultLogLevel: LoggingCategory.Warning + } + + LoggingCategory { id: emptyCategory } @@ -57,8 +63,14 @@ Item { console.info(testCategory, "console.info"); console.warn(testCategory, "console.warn"); console.error(testCategory, "console.error"); + console.debug(testCategoryStartingFromWarning, "console.debug"); + console.log(testCategoryStartingFromWarning, "console.log"); + console.info(testCategoryStartingFromWarning, "console.info"); + console.warn(testCategoryStartingFromWarning, "console.warn"); + console.error(testCategoryStartingFromWarning, "console.error"); testCategory.name = "qt.test2"; + testCategory.defaultLogLevel = LoggingCategory.Debug; console.error(emptyCategory, "console.error"); } diff --git a/tests/auto/qml/qqmlconsole/tst_qqmlconsole.cpp b/tests/auto/qml/qqmlconsole/tst_qqmlconsole.cpp index 40a9295e50..77805bbbac 100644 --- a/tests/auto/qml/qqmlconsole/tst_qqmlconsole.cpp +++ b/tests/auto/qml/qqmlconsole/tst_qqmlconsole.cpp @@ -112,8 +112,13 @@ void tst_qqmlconsole::categorized_logging() QVERIFY(messageHandler.messages().contains("qt.test: console.info")); QVERIFY(messageHandler.messages().contains("qt.test: console.warn")); QVERIFY(messageHandler.messages().contains("qt.test: console.error")); + QVERIFY(!messageHandler.messages().contains("qt.test.warning: console.debug")); + QVERIFY(!messageHandler.messages().contains("qt.test.warning: console.log")); + QVERIFY(!messageHandler.messages().contains("qt.test.warning: console.info")); + QVERIFY(messageHandler.messages().contains("qt.test.warning: console.warn")); + QVERIFY(messageHandler.messages().contains("qt.test.warning: console.error")); - QString emptyCategory = "default: " + QString::fromLatin1("%1:%2:%3: ").arg(testUrl.toString()).arg(50).arg(5) + + QString emptyCategory = "default: " + QString::fromLatin1("%1:%2:%3: ").arg(testUrl.toString()).arg(56).arg(5) + "QML LoggingCategory: Declaring the name of the LoggingCategory is mandatory and cannot be changed later !"; QVERIFY(messageHandler.messages().contains(emptyCategory)); @@ -121,7 +126,11 @@ void tst_qqmlconsole::categorized_logging() "QML LoggingCategory: The name of a LoggingCategory cannot be changed after the Item is created"; QVERIFY(messageHandler.messages().contains(changedCategory)); - QString useEmptyCategory = "default: " + QString::fromLatin1("%1:%2: ").arg(testUrl.toString()).arg(63) + + QString changedDefaultLogLevel = "default: " + QString::fromLatin1("%1:%2:%3: ").arg(testUrl.toString()).arg(45).arg(5) + + "QML LoggingCategory: The defaultLogLevel of a LoggingCategory cannot be changed after the Item is created"; + QVERIFY(messageHandler.messages().contains(changedDefaultLogLevel)); + + QString useEmptyCategory = "default: " + QString::fromLatin1("%1:%2: ").arg(testUrl.toString()).arg(75) + "Error: A QmlLoggingCatgory was provided without a valid name"; QVERIFY(messageHandler.messages().contains(useEmptyCategory)); |