From 915578b287889dff9e677749c638172145d0e64a Mon Sep 17 00:00:00 2001 From: Bianca van Schaik Date: Wed, 25 Oct 2017 17:14:47 +0200 Subject: [PATCH] Improve error logging --- engconverter/src/logger.cpp | 20 +++++++++++++++++--- engconverter/src/logger.h | 9 +++++++++ engconverter/src/messagefilexmlstream.cpp | 3 +++ engconverter/src/xmlstream.cpp | 3 +++ 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/engconverter/src/logger.cpp b/engconverter/src/logger.cpp index 7580824..61bbd56 100644 --- a/engconverter/src/logger.cpp +++ b/engconverter/src/logger.cpp @@ -25,20 +25,34 @@ void Logger::error(const QString &message) { - m_messages.append(QString("ERROR: %1").arg(message)); + addMessage(QString("ERROR: %1").arg(message)); } void Logger::warn(const QString &message) { - m_messages.append(QString("Warning: %1").arg(message)); + addMessage(QString("Warning: %1").arg(message)); } void Logger::info(const QString &message) { - m_messages.append(message); + addMessage(message); +} + +void Logger::addMessage(const QString &message) +{ + if (m_context.isEmpty()) { + m_messages.append(message); + } else { + m_messages.append(QString("%1: %2").arg(m_context, message)); + } } const QStringList Logger::messages() const { return m_messages; } + +void Logger::setContext(const QString &context) +{ + m_context = context; +} diff --git a/engconverter/src/logger.h b/engconverter/src/logger.h index b10d88f..4745b06 100644 --- a/engconverter/src/logger.h +++ b/engconverter/src/logger.h @@ -56,8 +56,17 @@ class Logger */ const QStringList messages() const; + /** + * Set context data which is logged before every error/warning + * \param context Context info + */ + void setContext(const QString &context); + private: + void addMessage(const QString &message); + QStringList m_messages; + QString m_context; }; #endif // LOGGER_H diff --git a/engconverter/src/messagefilexmlstream.cpp b/engconverter/src/messagefilexmlstream.cpp index 91a7f49..91520e2 100644 --- a/engconverter/src/messagefilexmlstream.cpp +++ b/engconverter/src/messagefilexmlstream.cpp @@ -57,6 +57,7 @@ bool MessageFileXmlStream::readFile(MessageFile &file, QXmlStreamReader &xml, Lo return false; } } + logger.setContext(""); return readCloseTag(xml, "messages", logger); } @@ -68,6 +69,8 @@ bool MessageFileXmlStream::readMessageEntry(MessageFile &file, QXmlStreamReader !readIntegerAttribute(xml, "subtype", &subtype, logger)) { return false; } + logger.setContext(QString("Message %1").arg(id)); + MessageEntry entry(id); entry.setType(type); entry.setSubtype(subtype); diff --git a/engconverter/src/xmlstream.cpp b/engconverter/src/xmlstream.cpp index 9d1c10c..0a853e4 100644 --- a/engconverter/src/xmlstream.cpp +++ b/engconverter/src/xmlstream.cpp @@ -94,6 +94,9 @@ bool XmlStream::readCloseTag(QXmlStreamReader &xml, const QString &tag, Logger & QXmlStreamReader::TokenType token = xml.readNext(); if (token == QXmlStreamReader::EndElement && xml.name() == tag) { return true; + } else if (token == QXmlStreamReader::Invalid) { + logger.error(QString("Invalid XML: %1").arg(xml.errorString())); + return false; } } logger.error(QString("Invalid XML: end element not found").arg(tag));