Allow more fine grained control over QMetaEnum debug output

Useful in contexts such as other QDebug operators, where the class
is already known, and the full scope of the enum is not needed.

Change-Id: Ibd04b1fd4f0f914c7224a007fc248d4ebabcde3e
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Tor Arne Vestbø 2018-12-19 16:46:47 +01:00
parent a1ad9b590b
commit 25fc90e48b
2 changed files with 90 additions and 13 deletions

View File

@ -916,23 +916,63 @@ void qt_QMetaEnum_flagDebugOperator(QDebug &debug, size_t sizeofT, int value)
#ifndef QT_NO_QOBJECT
/*!
\fn QDebug qt_QMetaEnum_debugOperator(QDebug &, int value, const QMetaObject *, const char *name)
\internal
Formats the given enum \a value for debug output.
The supported verbosity are:
0: Just the key, or value with enum name if no key is found:
MyEnum2
MyEnum(123)
MyScopedEnum::Enum3
MyScopedEnum(456)
1: Same as 0, but treating all enums as scoped:
MyEnum::MyEnum2
MyEnum(123)
MyScopedEnum::Enum3
MyScopedEnum(456)
2: The QDebug default. Same as 0, and includes class/namespace scope:
MyNamespace::MyClass::MyEnum2
MyNamespace::MyClass::MyEnum(123)
MyNamespace::MyClass::MyScopedEnum::Enum3
MyNamespace::MyClass::MyScopedEnum(456)
3: Same as 2, but treating all enums as scoped:
MyNamespace::MyClass::MyEnum::MyEnum2
MyNamespace::MyClass::MyEnum(123)
MyNamespace::MyClass::MyScopedEnum::Enum3
MyNamespace::MyClass::MyScopedEnum(456)
*/
QDebug qt_QMetaEnum_debugOperator(QDebug &dbg, int value, const QMetaObject *meta, const char *name)
{
QDebugStateSaver saver(dbg);
dbg.nospace();
QMetaEnum me = meta->enumerator(meta->indexOfEnumerator(name));
const char *key = me.valueToKey(value);
if (key) {
const int verbosity = dbg.verbosity();
if (verbosity >= QDebug::DefaultVerbosity) {
if (const char *scope = me.scope())
dbg << scope << "::";
if (me.isScoped())
dbg << me.enumName() << "::";
dbg << key;
} else {
dbg << meta->className() << "::" << name << "(" << value << ")";
}
const char *key = me.valueToKey(value);
const bool scoped = me.isScoped() || verbosity & 1;
if (scoped || !key)
dbg << me.enumName() << (!key ? "(" : "::");
if (key)
dbg << key;
else
dbg << value << ")";
return dbg;
}

View File

@ -324,6 +324,7 @@ private slots:
void signal();
void signalIndex_data();
void signalIndex();
void enumDebugStream_data();
void enumDebugStream();
void inherits_data();
@ -1741,16 +1742,52 @@ void tst_QMetaObject::signalIndex()
SignalTestHelper::signalIndex(mm));
}
void tst_QMetaObject::enumDebugStream_data()
{
QTest::addColumn<int>("verbosity");
QTest::addColumn<QString>("normalEnumMsg");
QTest::addColumn<QString>("scopedEnumMsg");
QTest::addColumn<QString>("globalEnumMsg");
QTest::newRow("verbosity=0") << 0
<< "WindowTitleHint Window Desktop WindowSystemMenuHint";
<< "hello MyEnum2 world"
<< "hello MyScopedEnum::Enum3 scoped world"
QTest::newRow("verbosity=1") << 1
<< "WindowType::WindowTitleHint WindowType::Window WindowType::Desktop WindowType::WindowSystemMenuHint";
<< "hello MyEnum::MyEnum2 world"
<< "hello MyScopedEnum::Enum3 scoped world"
QTest::newRow("verbosity=2") << 2
<< "Qt::WindowTitleHint Qt::Window Qt::Desktop Qt::WindowSystemMenuHint";
<< "hello MyNamespace::MyClass::MyEnum2 world"
<< "hello MyNamespace::MyClass::MyScopedEnum::Enum3 scoped world"
QTest::newRow("verbosity=3") << 3
<< "Qt::WindowType::WindowTitleHint Qt::WindowType::Window Qt::WindowType::Desktop Qt::WindowType::WindowSystemMenuHint";
<< "hello MyNamespace::MyClass::MyEnum::MyEnum2 world"
<< "hello MyNamespace::MyClass::MyScopedEnum::Enum3 scoped world"
}
void tst_QMetaObject::enumDebugStream()
{
QTest::ignoreMessage(QtDebugMsg, "hello MyNamespace::MyClass::MyEnum2 world ");
qDebug() << "hello" << MyNamespace::MyClass::MyEnum2 << "world";
QFETCH(int, verbosity);
QFETCH(QString, normalEnumMsg);
QFETCH(QString, scopedEnumMsg);
QFETCH(QString, globalEnumMsg);
QTest::ignoreMessage(QtDebugMsg, "hello MyNamespace::MyClass::MyScopedEnum::Enum3 scoped world ");
qDebug() << "hello" << MyNamespace::MyClass::MyScopedEnum::Enum3 << "scoped world";
QTest::ignoreMessage(QtDebugMsg, qPrintable(normalEnumMsg));
qDebug().verbosity(verbosity) << "hello" << MyNamespace::MyClass::MyEnum2 << "world";
QTest::ignoreMessage(QtDebugMsg, "Qt::WindowTitleHint Qt::Window Qt::Desktop Qt::WindowSystemMenuHint");
qDebug() << Qt::WindowTitleHint << Qt::Window << Qt::Desktop << Qt::WindowSystemMenuHint;
QTest::ignoreMessage(QtDebugMsg, qPrintable(scopedEnumMsg));
qDebug().verbosity(verbosity) << "hello" << MyNamespace::MyClass::MyScopedEnum::Enum3 << "scoped world";
QTest::ignoreMessage(QtDebugMsg, qPrintable(globalEnumMsg));
qDebug().verbosity(verbosity) << Qt::WindowTitleHint << Qt::Window << Qt::Desktop << Qt::WindowSystemMenuHint;
if (verbosity != 2)
return;
QTest::ignoreMessage(QtDebugMsg, "hello QFlags<MyNamespace::MyClass::MyFlag>(MyFlag1) world");
MyNamespace::MyClass::MyFlags f1 = MyNamespace::MyClass::MyFlag1;