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:
Ivan Tkachenko 2021-10-22 17:13:15 +03:00
parent 250d6269e9
commit 7136df0f8e
6 changed files with 56 additions and 35 deletions

View File

@ -129,7 +129,7 @@ void QQmlLoggingCategory::componentComplete()
{
m_initialized = true;
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 {
QScopedPointer<QLoggingCategory> category(new QLoggingCategory(m_name.constData(), QtMsgType(m_defaultLogLevel)));
m_category.swap(category);
@ -138,23 +138,30 @@ void QQmlLoggingCategory::componentComplete()
void QQmlLoggingCategory::setDefaultLogLevel(DefaultLogLevel defaultLogLevel)
{
if (m_defaultLogLevel == defaultLogLevel)
return;
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;
}
m_defaultLogLevel = defaultLogLevel;
}
void QQmlLoggingCategory::setName(const QString &name)
{
const QByteArray newName = name.toUtf8();
if (m_name == newName)
return;
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;
}
m_name = name.toUtf8();
m_name = newName;
}
#include "moc_qqmlloggingcategory_p.cpp"

View File

@ -29,21 +29,22 @@
import QtQuick 2.0
QtObject {
property int q:1
property int q: 1
function assertFail() {
console.assert(0, "This will fail too")
console.assert(0, "This will fail too");
}
Component.onCompleted: {
var x = 12;
const x = 12;
console.assert(x == 12, "This will pass");
try {
console.assert(x < 12, "This will fail");
console.assert(x < 12, "This will fail");
} catch (e) {
console.log(e);
}
console.assert("x < 12", "This will pass too")
console.assert("x < 12", "This will pass too");
assertFail();
console.assert(1)
console.assert(1);
}
}

View File

@ -40,7 +40,7 @@
import QtQuick 2.12
Item {
id:root
id: root
LoggingCategory {
id: testCategory
@ -69,8 +69,11 @@ Item {
console.warn(testCategoryStartingFromWarning, "console.warn");
console.error(testCategoryStartingFromWarning, "console.error");
testCategory.name = "qt.test2";
testCategory.defaultLogLevel = LoggingCategory.Debug;
testCategory.name = "qt.test"; // should be silent
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");
}

View File

@ -30,12 +30,12 @@ import QtQuick 2.0
QtObject {
function exceptionFail() {
console.exception("Exception 2")
console.exception("Exception 2");
}
Component.onCompleted: {
try {
console.exception("Exception 1")
console.exception("Exception 1");
} catch (e) {
console.log(e);
}

View File

@ -30,7 +30,8 @@
import QtQuick 2.0
QtObject {
id:root
id: root
required property var customObject
required property var stringListProperty
@ -49,14 +50,14 @@ QtObject {
consoleCount();
consoleCount();
var a = [1, 2];
var b = {a: "hello", d: 1 };
b.toString = function() { return JSON.stringify(b) }
var c
var d = 12;
var e = function() { return 5;};
var f = true;
var g = {toString: function() { throw new Error('toString'); }};
const a = [1, 2];
const b = { a: "hello", d: 1 };
b.toString = function() { return JSON.stringify(b); }
let c;
const d = 12;
const e = function() { return 5; };
const f = true;
const g = { toString: function() { throw new Error('toString'); } };
console.log(a);
console.log(b);
@ -79,6 +80,6 @@ QtObject {
return;
}
throw ("console.log(exception) should have raised an exception");
throw "console.log(exception) should have raised an exception";
}
}

View File

@ -133,18 +133,27 @@ void tst_qqmlconsole::categorized_logging()
QVERIFY(messageHandler.messages().contains("qt.test.warning: console.error"));
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));
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));
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));
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";
QVERIFY(messageHandler.messages().contains(useEmptyCategory));
@ -190,13 +199,13 @@ void tst_qqmlconsole::testAssert()
QString assert1 = "This will fail\n"
+ QString::fromLatin1("expression for onCompleted (%1:%2)")
.arg(testUrl.toString())
.arg(41);
.arg(42);
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)")
.arg(testUrl.toString())
.arg(46);
.arg(47);
QTest::ignoreMessage(QtCriticalMsg, qPrintable(assert1));
QTest::ignoreMessage(QtCriticalMsg, qPrintable(assert2));