Ref T108, avoid duplicates in status message categories

and using string builder in CStatusMessage::convertToQString
This commit is contained in:
Klaus Basan
2017-07-12 23:26:56 +02:00
committed by Mathew Sutcliffe
parent 0f59a123ae
commit 224c61c7e9
2 changed files with 41 additions and 22 deletions

View File

@@ -15,6 +15,7 @@
#include "logmessage.h"
#include "comparefunctions.h"
#include <QMetaEnum>
#include <QStringBuilder>
namespace BlackMisc
{
@@ -258,24 +259,19 @@ namespace BlackMisc
return this->m_handledByObjects.contains(quintptr(object));
}
void CStatusMessage::addValidationCategory()
{
this->addCategory(CLogCategory::validation());
}
QString CStatusMessage::convertToQString(bool /** i18n */) const
{
QString s("Category: ");
s.append(this->m_categories.toQString());
return QLatin1String("Category: ") %
this->m_categories.toQString() %
s.append(" Severity: ");
s.append(QString::number(this->m_severity));
QLatin1String(" Severity: ") %
severityToString(this->m_severity) %
s.append(" when: ");
s.append(this->getFormattedUtcTimestampYmdhms());
QLatin1String(" when: ") %
this->getFormattedUtcTimestampYmdhms() %
s.append(" ").append(this->getMessage());
return s;
QLatin1String(" ") %
this->getMessage();
}
const CIcon &CStatusMessage::convertToIcon(const CStatusMessage &statusMessage)

View File

@@ -53,16 +53,16 @@ namespace BlackMisc
CMessageBase() {}
//! Construct a message with some specific category.
explicit CMessageBase(const CLogCategory &category) : m_categories({ category }) {}
explicit CMessageBase(const CLogCategory &category) : m_categories( { category }) {}
//! Construct a message with some specific categories.
explicit CMessageBase(const CLogCategoryList &categories) : m_categories(categories) {}
//! Construct a message with some specific categories.
CMessageBase(const CLogCategoryList &categories, const CLogCategory &extra) : CMessageBase(categories) { m_categories.push_back(extra); }
CMessageBase(const CLogCategoryList &categories, const CLogCategory &extra) : CMessageBase(categories) { this->addIfNotExisting(extra); }
//! Construct a message with some specific categories.
CMessageBase(const CLogCategoryList &categories, const CLogCategoryList &extra) : CMessageBase(categories) { m_categories.push_back(extra); }
CMessageBase(const CLogCategoryList &categories, const CLogCategoryList &extra) : CMessageBase(categories) { this->addIfNotExisting(extra); }
//! Set the severity to debug.
Derived &debug() { return setSeverityAndMessage(SeverityDebug, ""); }
@@ -107,12 +107,35 @@ namespace BlackMisc
//! @}
private:
void setValidation() { m_categories.remove(CLogCategory::uncategorized()); m_categories.push_back(CLogCategory::validation()); }
void setValidation() { m_categories.remove(CLogCategory::uncategorized()); this->addIfNotExisting(CLogCategory::validation()); }
Derived &setSeverityAndMessage(StatusSeverity s, const QString &m) { m_message = m; m_severity = s; return derived(); }
Derived &arg(const QString &value) { m_args.push_back(value); return derived(); }
Derived &derived() { return static_cast<Derived &>(*this); }
protected:
//! Add category if not already existing
void addIfNotExisting(const CLogCategory &category)
{
if (this->m_categories.contains(category)) { return; }
this->m_categories.push_back(category);
}
//! Add categories if not already existing
void addIfNotExisting(const CLogCategoryList &categories)
{
if (this->m_categories.isEmpty())
{
this->m_categories.push_back(categories);
}
else
{
for (const CLogCategory &cat : categories)
{
this->addIfNotExisting(cat);
}
}
}
//! \private
//! @{
QString m_message;
@@ -266,14 +289,14 @@ namespace BlackMisc
//! Severity
void setSeverity(StatusSeverity severity) { this->m_severity = severity; }
//! Add category
void addCategory(const CLogCategory &category) { this->m_categories.push_back(category); }
//! Add category, avoids duplicates
void addCategory(const CLogCategory &category) { this->addIfNotExisting(category); }
//! Adds validation as category
void addValidationCategory();
void addValidationCategory() { this->addCategory(CLogCategory::validation()); }
//! Add categories
void addCategories(const CLogCategoryList &categories) { this->m_categories.push_back(categories); }
//! Add categories, avoids duplicates
void addCategories(const CLogCategoryList &categories) { this->addIfNotExisting(categories); }
//! Reset category
void setCategory(const CLogCategory &category) { this->m_categories = CLogCategoryList { category }; }