QQmlLoggingCategory: Avoid unnecessary warnings from setters
Assigning same value should be a no-op, especially because such assignments may happen without explicit user/developer intent. For example, it can be triggered with QQmlEngine::retranslate() call on 5.15 and early 6.x branches. Since the recent optimizations to retranslate() which now only forces to re-evaluate what's really dependent on qsTr, it became harder, but still possible to pull it off. Fixes: QTBUG-97717 Pick-to: 5.15 6.2 Change-Id: I60ca8e36ed98a15ea5f3c15290865cec27084fbc Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
This commit is contained in:
parent
250d6269e9
commit
7136df0f8e
|
@ -129,7 +129,7 @@ void QQmlLoggingCategory::componentComplete()
|
||||||
{
|
{
|
||||||
m_initialized = true;
|
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 !");
|
qmlWarning(this) << QLatin1String("Declaring the name of a LoggingCategory is mandatory and cannot be changed later");
|
||||||
} else {
|
} else {
|
||||||
QScopedPointer<QLoggingCategory> category(new QLoggingCategory(m_name.constData(), QtMsgType(m_defaultLogLevel)));
|
QScopedPointer<QLoggingCategory> category(new QLoggingCategory(m_name.constData(), QtMsgType(m_defaultLogLevel)));
|
||||||
m_category.swap(category);
|
m_category.swap(category);
|
||||||
|
@ -138,23 +138,30 @@ void QQmlLoggingCategory::componentComplete()
|
||||||
|
|
||||||
void QQmlLoggingCategory::setDefaultLogLevel(DefaultLogLevel defaultLogLevel)
|
void QQmlLoggingCategory::setDefaultLogLevel(DefaultLogLevel defaultLogLevel)
|
||||||
{
|
{
|
||||||
|
if (m_defaultLogLevel == defaultLogLevel)
|
||||||
|
return;
|
||||||
|
|
||||||
if (m_initialized) {
|
if (m_initialized) {
|
||||||
qmlWarning(this) << QLatin1String("The defaultLogLevel of a LoggingCategory cannot be changed after the Item is created");
|
qmlWarning(this) << QLatin1String("The defaultLogLevel of a LoggingCategory cannot be changed after the component is completed");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_defaultLogLevel = defaultLogLevel;
|
m_defaultLogLevel = defaultLogLevel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void QQmlLoggingCategory::setName(const QString &name)
|
void QQmlLoggingCategory::setName(const QString &name)
|
||||||
{
|
{
|
||||||
|
const QByteArray newName = name.toUtf8();
|
||||||
|
|
||||||
|
if (m_name == newName)
|
||||||
|
return;
|
||||||
|
|
||||||
if (m_initialized) {
|
if (m_initialized) {
|
||||||
qmlWarning(this) << QLatin1String("The name of a LoggingCategory cannot be changed after the Item is created");
|
qmlWarning(this) << QLatin1String("The name of a LoggingCategory cannot be changed after the component is completed");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_name = name.toUtf8();
|
m_name = newName;
|
||||||
}
|
}
|
||||||
|
|
||||||
#include "moc_qqmlloggingcategory_p.cpp"
|
#include "moc_qqmlloggingcategory_p.cpp"
|
||||||
|
|
|
@ -29,21 +29,22 @@
|
||||||
import QtQuick 2.0
|
import QtQuick 2.0
|
||||||
|
|
||||||
QtObject {
|
QtObject {
|
||||||
property int q:1
|
property int q: 1
|
||||||
|
|
||||||
function assertFail() {
|
function assertFail() {
|
||||||
console.assert(0, "This will fail too")
|
console.assert(0, "This will fail too");
|
||||||
}
|
}
|
||||||
|
|
||||||
Component.onCompleted: {
|
Component.onCompleted: {
|
||||||
var x = 12;
|
const x = 12;
|
||||||
console.assert(x == 12, "This will pass");
|
console.assert(x == 12, "This will pass");
|
||||||
try {
|
try {
|
||||||
console.assert(x < 12, "This will fail");
|
console.assert(x < 12, "This will fail");
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e);
|
console.log(e);
|
||||||
}
|
}
|
||||||
console.assert("x < 12", "This will pass too")
|
console.assert("x < 12", "This will pass too");
|
||||||
assertFail();
|
assertFail();
|
||||||
console.assert(1)
|
console.assert(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,7 +40,7 @@
|
||||||
import QtQuick 2.12
|
import QtQuick 2.12
|
||||||
|
|
||||||
Item {
|
Item {
|
||||||
id:root
|
id: root
|
||||||
|
|
||||||
LoggingCategory {
|
LoggingCategory {
|
||||||
id: testCategory
|
id: testCategory
|
||||||
|
@ -69,8 +69,11 @@ Item {
|
||||||
console.warn(testCategoryStartingFromWarning, "console.warn");
|
console.warn(testCategoryStartingFromWarning, "console.warn");
|
||||||
console.error(testCategoryStartingFromWarning, "console.error");
|
console.error(testCategoryStartingFromWarning, "console.error");
|
||||||
|
|
||||||
testCategory.name = "qt.test2";
|
testCategory.name = "qt.test"; // should be silent
|
||||||
testCategory.defaultLogLevel = LoggingCategory.Debug;
|
testCategoryStartingFromWarning.name = "qt.test.other"; // should issue a warning
|
||||||
|
|
||||||
|
testCategory.defaultLogLevel = LoggingCategory.Debug; // should be silent
|
||||||
|
testCategoryStartingFromWarning.defaultLogLevel = LoggingCategory.Debug; // should issue a warning
|
||||||
|
|
||||||
console.error(emptyCategory, "console.error");
|
console.error(emptyCategory, "console.error");
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,12 +30,12 @@ import QtQuick 2.0
|
||||||
|
|
||||||
QtObject {
|
QtObject {
|
||||||
function exceptionFail() {
|
function exceptionFail() {
|
||||||
console.exception("Exception 2")
|
console.exception("Exception 2");
|
||||||
}
|
}
|
||||||
|
|
||||||
Component.onCompleted: {
|
Component.onCompleted: {
|
||||||
try {
|
try {
|
||||||
console.exception("Exception 1")
|
console.exception("Exception 1");
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e);
|
console.log(e);
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,8 @@
|
||||||
import QtQuick 2.0
|
import QtQuick 2.0
|
||||||
|
|
||||||
QtObject {
|
QtObject {
|
||||||
id:root
|
id: root
|
||||||
|
|
||||||
required property var customObject
|
required property var customObject
|
||||||
required property var stringListProperty
|
required property var stringListProperty
|
||||||
|
|
||||||
|
@ -49,14 +50,14 @@ QtObject {
|
||||||
consoleCount();
|
consoleCount();
|
||||||
consoleCount();
|
consoleCount();
|
||||||
|
|
||||||
var a = [1, 2];
|
const a = [1, 2];
|
||||||
var b = {a: "hello", d: 1 };
|
const b = { a: "hello", d: 1 };
|
||||||
b.toString = function() { return JSON.stringify(b) }
|
b.toString = function() { return JSON.stringify(b); }
|
||||||
var c
|
let c;
|
||||||
var d = 12;
|
const d = 12;
|
||||||
var e = function() { return 5;};
|
const e = function() { return 5; };
|
||||||
var f = true;
|
const f = true;
|
||||||
var g = {toString: function() { throw new Error('toString'); }};
|
const g = { toString: function() { throw new Error('toString'); } };
|
||||||
|
|
||||||
console.log(a);
|
console.log(a);
|
||||||
console.log(b);
|
console.log(b);
|
||||||
|
@ -79,6 +80,6 @@ QtObject {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
throw ("console.log(exception) should have raised an exception");
|
throw "console.log(exception) should have raised an exception";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -133,18 +133,27 @@ void tst_qqmlconsole::categorized_logging()
|
||||||
QVERIFY(messageHandler.messages().contains("qt.test.warning: console.error"));
|
QVERIFY(messageHandler.messages().contains("qt.test.warning: console.error"));
|
||||||
|
|
||||||
QString emptyCategory = "default: " + QString::fromLatin1("%1:%2:%3: ").arg(testUrl.toString()).arg(56).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 !";
|
"QML LoggingCategory: Declaring the name of a LoggingCategory is mandatory and cannot be changed later";
|
||||||
QVERIFY(messageHandler.messages().contains(emptyCategory));
|
QVERIFY(messageHandler.messages().contains(emptyCategory));
|
||||||
|
|
||||||
QString changedCategory = "default: " + QString::fromLatin1("%1:%2:%3: ").arg(testUrl.toString()).arg(45).arg(5) +
|
|
||||||
"QML LoggingCategory: The name of a LoggingCategory cannot be changed after the Item is created";
|
QString notChangedCategory = "default: " + QString::fromLatin1("%1:%2:%3: ").arg(testUrl.toString()).arg(45).arg(5) +
|
||||||
|
"QML LoggingCategory: The name of a LoggingCategory cannot be changed after the component is completed";
|
||||||
|
QVERIFY(!messageHandler.messages().contains(notChangedCategory));
|
||||||
|
QString changedCategory = "default: " + QString::fromLatin1("%1:%2:%3: ").arg(testUrl.toString()).arg(50).arg(5) +
|
||||||
|
"QML LoggingCategory: The name of a LoggingCategory cannot be changed after the component is completed";
|
||||||
QVERIFY(messageHandler.messages().contains(changedCategory));
|
QVERIFY(messageHandler.messages().contains(changedCategory));
|
||||||
|
|
||||||
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";
|
QString notChangedDefaultLogLevel = "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 component is completed";
|
||||||
|
QVERIFY(!messageHandler.messages().contains(notChangedDefaultLogLevel));
|
||||||
|
QString changedDefaultLogLevel = "default: " + QString::fromLatin1("%1:%2:%3: ").arg(testUrl.toString()).arg(50).arg(5) +
|
||||||
|
"QML LoggingCategory: The defaultLogLevel of a LoggingCategory cannot be changed after the component is completed";
|
||||||
QVERIFY(messageHandler.messages().contains(changedDefaultLogLevel));
|
QVERIFY(messageHandler.messages().contains(changedDefaultLogLevel));
|
||||||
|
|
||||||
QString useEmptyCategory = "default: " + QString::fromLatin1("%1:%2: ").arg(testUrl.toString()).arg(75) +
|
|
||||||
|
QString useEmptyCategory = "default: " + QString::fromLatin1("%1:%2: ").arg(testUrl.toString()).arg(78) +
|
||||||
"Error: A QmlLoggingCatgory was provided without a valid name";
|
"Error: A QmlLoggingCatgory was provided without a valid name";
|
||||||
QVERIFY(messageHandler.messages().contains(useEmptyCategory));
|
QVERIFY(messageHandler.messages().contains(useEmptyCategory));
|
||||||
|
|
||||||
|
@ -190,13 +199,13 @@ void tst_qqmlconsole::testAssert()
|
||||||
QString assert1 = "This will fail\n"
|
QString assert1 = "This will fail\n"
|
||||||
+ QString::fromLatin1("expression for onCompleted (%1:%2)")
|
+ QString::fromLatin1("expression for onCompleted (%1:%2)")
|
||||||
.arg(testUrl.toString())
|
.arg(testUrl.toString())
|
||||||
.arg(41);
|
.arg(42);
|
||||||
|
|
||||||
QString assert2 = "This will fail too\n"
|
QString assert2 = "This will fail too\n"
|
||||||
+ QString::fromLatin1("assertFail (%1:%2)\n").arg(testUrl.toString()).arg(34)
|
+ QString::fromLatin1("assertFail (%1:%2)\n").arg(testUrl.toString()).arg(35)
|
||||||
+ QString::fromLatin1("expression for onCompleted (%1:%2)")
|
+ QString::fromLatin1("expression for onCompleted (%1:%2)")
|
||||||
.arg(testUrl.toString())
|
.arg(testUrl.toString())
|
||||||
.arg(46);
|
.arg(47);
|
||||||
|
|
||||||
QTest::ignoreMessage(QtCriticalMsg, qPrintable(assert1));
|
QTest::ignoreMessage(QtCriticalMsg, qPrintable(assert1));
|
||||||
QTest::ignoreMessage(QtCriticalMsg, qPrintable(assert2));
|
QTest::ignoreMessage(QtCriticalMsg, qPrintable(assert2));
|
||||||
|
|
Loading…
Reference in New Issue