From bfa9500ae5b9acf24d1b6847003c19a4e49d9749 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Thu, 2 Feb 2017 10:07:39 +0100 Subject: Improve the error messages of QSvgHandler Add a function printing file name and location. Could not resolve property : linearGradient4538 becomes /usr/share/icons/breeze/actions/24/document-new.svg:123: Could not resolve property: linearGradient4538 Task-number: QTBUG-52079 Change-Id: I843f65eb4ca517d103031f9921a16090a6ff6720 Reviewed-by: David Faure --- src/svg/qsvghandler.cpp | 61 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 14 deletions(-) diff --git a/src/svg/qsvghandler.cpp b/src/svg/qsvghandler.cpp index 0b0750b..df654e1 100644 --- a/src/svg/qsvghandler.cpp +++ b/src/svg/qsvghandler.cpp @@ -55,6 +55,7 @@ #include "qvector.h" #include "qfileinfo.h" #include "qfile.h" +#include "qdir.h" #include "qdebug.h" #include "qmath.h" #include "qnumeric.h" @@ -70,6 +71,36 @@ Q_LOGGING_CATEGORY(lcSvgHandler, "qt.svg") static const char *qt_inherit_text = "inherit"; #define QT_INHERIT QLatin1String(qt_inherit_text) +static QByteArray prefixMessage(const QByteArray &msg, const QXmlStreamReader *r) +{ + QByteArray result; + if (r) { + if (const QFile *file = qobject_cast(r->device())) + result.append(QFile::encodeName(QDir::toNativeSeparators(file->fileName()))); + else + result.append(QByteArrayLiteral("")); + result.append(':'); + result.append(QByteArray::number(r->lineNumber())); + if (const qint64 column = r->columnNumber()) { + result.append(':'); + result.append(QByteArray::number(column)); + } + result.append(QByteArrayLiteral(": ")); + } + result.append(msg); + return result; +} + +static inline QByteArray msgProblemParsing(const QString &localName, const QXmlStreamReader *r) +{ + return prefixMessage(QByteArrayLiteral("Problem parsing ") + localName.toLocal8Bit(), r); +} + +static inline QByteArray msgCouldNotResolveProperty(const QString &id, const QXmlStreamReader *r) +{ + return prefixMessage(QByteArrayLiteral("Could not resolve property: ") + id.toLocal8Bit(), r); +} + // ======== duplicated from qcolor_p static inline int qsvg_h2i(char hex) @@ -3621,9 +3652,10 @@ bool QSvgHandler::startElement(const QString &localName, } else if (xmlSpace == QLatin1String("default")) { m_whitespaceMode.push(QSvgText::Default); } else { - qCWarning(lcSvgHandler).noquote() - << QString::fromLatin1("\"%1\" is an invalid value for attribute xml:space. " - "Valid values are \"preserve\" and \"default\".").arg(xmlSpace.toString()); + const QByteArray msg = '"' + xmlSpace.toString().toLocal8Bit() + + "\" is an invalid value for attribute xml:space. " + "Valid values are \"preserve\" and \"default\"."; + qCWarning(lcSvgHandler, "%s", prefixMessage(msg, xml).constData()); m_whitespaceMode.push(QSvgText::Default); } @@ -3679,13 +3711,15 @@ bool QSvgHandler::startElement(const QString &localName, if (node->type() == QSvgNode::TSPAN) { static_cast(m_nodes.top())->addTspan(static_cast(node)); } else { - qCWarning(lcSvgHandler, "\'text\' or \'textArea\' element contains invalid element type."); + const QByteArray msg = QByteArrayLiteral("\'text\' or \'textArea\' element contains invalid element type."); + qCWarning(lcSvgHandler, "%s", prefixMessage(msg, xml).constData()); delete node; node = 0; } break; default: - qCWarning(lcSvgHandler, "Could not add child element to parent element because the types are incorrect."); + const QByteArray msg = QByteArrayLiteral("Could not add child element to parent element because the types are incorrect."); + qCWarning(lcSvgHandler, "%s", prefixMessage(msg, xml).constData()); delete node; node = 0; break; @@ -3706,22 +3740,21 @@ bool QSvgHandler::startElement(const QString &localName, } } else if (ParseMethod method = findUtilFactory(localName)) { Q_ASSERT(!m_nodes.isEmpty()); - if (!method(m_nodes.top(), attributes, this)) { - qCWarning(lcSvgHandler, "Problem parsing %s", qPrintable(localName)); - } + if (!method(m_nodes.top(), attributes, this)) + qCWarning(lcSvgHandler, "%s", msgProblemParsing(localName, xml).constData()); } else if (StyleFactoryMethod method = findStyleFactoryMethod(localName)) { QSvgStyleProperty *prop = method(m_nodes.top(), attributes, this); if (prop) { m_style = prop; m_nodes.top()->appendStyleProperty(prop, someId(attributes)); } else { - qCWarning(lcSvgHandler, "Could not parse node: %s", qPrintable(localName)); + const QByteArray msg = QByteArrayLiteral("Could not parse node: ") + localName.toLocal8Bit(); + qCWarning(lcSvgHandler, "%s", prefixMessage(msg, xml).constData()); } } else if (StyleParseMethod method = findStyleUtilFactoryMethod(localName)) { if (m_style) { - if (!method(m_style, attributes, this)) { - qCWarning(lcSvgHandler, "Problem parsing %s", qPrintable(localName)); - } + if (!method(m_style, attributes, this)) + qCWarning(lcSvgHandler, "%s", msgProblemParsing(localName, xml).constData()); } } else { //qCWarning(lcSvgHandler) <<"Skipping unknown element!"<setFillStyle(style); } else { - qCWarning(lcSvgHandler, "Could not resolve property : %s", qPrintable(id)); + qCWarning(lcSvgHandler, "%s", msgCouldNotResolveProperty(id, xml).constData()); fill->setBrush(Qt::NoBrush); } } @@ -3795,7 +3828,7 @@ void QSvgHandler::resolveGradients(QSvgNode *node) if (style) { stroke->setStyle(style); } else { - qCWarning(lcSvgHandler, "Could not resolve property : %s", qPrintable(id)); + qCWarning(lcSvgHandler, "%s", msgCouldNotResolveProperty(id, xml).constData()); stroke->setStroke(Qt::NoBrush); } } -- cgit v1.2.1