Skip to content

Commit

Permalink
Improve error logging
Browse files Browse the repository at this point in the history
  • Loading branch information
bvschaik committed Oct 25, 2017
1 parent 9a80d20 commit 915578b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
20 changes: 17 additions & 3 deletions engconverter/src/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
9 changes: 9 additions & 0 deletions engconverter/src/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions engconverter/src/messagefilexmlstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ bool MessageFileXmlStream::readFile(MessageFile &file, QXmlStreamReader &xml, Lo
return false;
}
}
logger.setContext("");
return readCloseTag(xml, "messages", logger);
}

Expand All @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions engconverter/src/xmlstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 </%1> not found").arg(tag));
Expand Down

0 comments on commit 915578b

Please sign in to comment.