refs #316 Added CLogMessage for logging messages, and corresponding changes to CStatusMessage and CTextMessage.

This commit is contained in:
Mathew Sutcliffe
2014-09-25 22:44:34 +01:00
parent 8ba1a37ea7
commit ce6cdf12a7
10 changed files with 447 additions and 158 deletions

View File

@@ -90,7 +90,7 @@ namespace BlackGui
} }
this->ui->le_SmSeverity->setText(statusMessage.getSeverityAsString()); this->ui->le_SmSeverity->setText(statusMessage.getSeverityAsString());
this->ui->le_SmType->setText(statusMessage.getTypeAsString()); this->ui->le_SmType->setText(statusMessage.getCategory()); // TODO should be called ui->le_SmCategory
this->ui->te_SmStatusMessage->setText(statusMessage.getMessage()); this->ui->te_SmStatusMessage->setText(statusMessage.getMessage());
this->ui->lbl_SmSeverityIcon->setPixmap(statusMessage.toPixmap()); this->ui->lbl_SmSeverityIcon->setPixmap(statusMessage.toPixmap());

View File

@@ -29,7 +29,7 @@ namespace BlackGui
this->m_columns.addColumn(CColumn("time", CStatusMessage::IndexTimestamp, new CDateTimeFormatter(CDateTimeFormatter::formatHms()))); this->m_columns.addColumn(CColumn("time", CStatusMessage::IndexTimestamp, new CDateTimeFormatter(CDateTimeFormatter::formatHms())));
this->m_columns.addColumn(CColumn("severity", CStatusMessage::IndexSeverity)); this->m_columns.addColumn(CColumn("severity", CStatusMessage::IndexSeverity));
this->m_columns.addColumn(CColumn::standardString("message", CStatusMessage::IndexMessage)); this->m_columns.addColumn(CColumn::standardString("message", CStatusMessage::IndexMessage));
this->m_columns.addColumn(CColumn::standardString("type", CStatusMessage::IndexTypeAsString)); this->m_columns.addColumn(CColumn::standardString("category", CStatusMessage::IndexCategory));
this->m_sortedColumn = CStatusMessage::IndexTimestamp; this->m_sortedColumn = CStatusMessage::IndexTimestamp;
this->m_sortOrder = Qt::DescendingOrder; this->m_sortOrder = Qt::DescendingOrder;

View File

@@ -0,0 +1,154 @@
/* Copyright (C) 2014
* Swift Project Community / Contributors
*
* This file is part of Swift Project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of Swift Project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
#include "logmessage.h"
#include "blackmiscfreefunctions.h"
namespace BlackMisc
{
CLogMessage &CLogMessage::debugImpl(QString format, QString category)
{
m_severity = CStatusMessage::SeverityDebug;
m_category = category;
m_message = format;
return *this;
}
CLogMessage &CLogMessage::infoImpl(QString format, QString category)
{
m_severity = CStatusMessage::SeverityInfo;
m_category = category;
m_message = format;
return *this;
}
CLogMessage &CLogMessage::warningImpl(QString format, QString category)
{
m_severity = CStatusMessage::SeverityWarning;
m_category = category;
m_message = format;
return *this;
}
CLogMessage &CLogMessage::errorImpl(QString format, QString category)
{
m_severity = CStatusMessage::SeverityError;
m_category = category;
m_message = format;
return *this;
}
CLogMessage::operator CStatusMessage()
{
m_redundant = true;
return { m_category, m_severity, message() };
}
CLogMessage::operator CVariant()
{
return CVariant::from(static_cast<CStatusMessage>(*this));
}
CLogMessage::~CLogMessage()
{
// ostream(encodedCategory()) << message(); // QDebug::operator<<(QString) puts quote characters around the message
// FIXME hack to avoid putting quote characters around the message
// should be safe, but still it's horrible, we could directly call qt_message_output instead
QByteArray category = encodedCategory();
QDebug debug = ostream(category);
auto &stream = **reinterpret_cast<QTextStream**>(&debug); // should be safe because it is relying on Qt's guarantee of ABI compatibility
stream << message();
}
QByteArray CLogMessage::encodedCategory() const
{
if (m_category.isEmpty())
{
return {};
}
else
{
QString category = m_category;
if (m_severity == CStatusMessage::SeverityDebug) { category = CLogMessageHelper::addDebugFlag(category); }
if (m_redundant) { category = CLogMessageHelper::addRedundantFlag(category); }
return category.toLatin1();
}
}
QDebug CLogMessage::ostream(const QByteArray &category) const
{
if (m_category.isEmpty())
{
switch (m_severity)
{
default:
case CStatusMessage::SeverityDebug: return m_logger.debug();
case CStatusMessage::SeverityInfo: return m_logger.debug();
case CStatusMessage::SeverityWarning: return m_logger.warning();
case CStatusMessage::SeverityError: return m_logger.critical();
}
}
else
{
switch (m_severity)
{
default:
case CStatusMessage::SeverityDebug: return m_logger.debug(QLoggingCategory(category.constData()));
case CStatusMessage::SeverityInfo: return m_logger.debug(QLoggingCategory(category.constData()));
case CStatusMessage::SeverityWarning: return m_logger.warning(QLoggingCategory(category.constData()));
case CStatusMessage::SeverityError: return m_logger.critical(QLoggingCategory(category.constData()));
}
}
}
QString CLogMessage::message() const
{
if (m_message.isEmpty())
{
return m_args.join(" ");
}
else
{
// TODO would like to have a QString::arg(QStringList) overload
switch (m_args.size())
{
case 0: return m_message;
case 1: return m_message.arg(m_args[0]);
case 2: return m_message.arg(m_args[0], m_args[1]);
case 3: return m_message.arg(m_args[0], m_args[1], m_args[2]);
case 4: return m_message.arg(m_args[0], m_args[1], m_args[2], m_args[3]);
case 5: return m_message.arg(m_args[0], m_args[1], m_args[2], m_args[3], m_args[4]);
case 6: return m_message.arg(m_args[0], m_args[1], m_args[2], m_args[3], m_args[4], m_args[5]);
case 7: return m_message.arg(m_args[0], m_args[1], m_args[2], m_args[3], m_args[4], m_args[5], m_args[6]);
case 8: return m_message.arg(m_args[0], m_args[1], m_args[2], m_args[3], m_args[4], m_args[5], m_args[6], m_args[7]);
default: qWarning("Too many arguments");
case 9: return m_message.arg(m_args[0], m_args[1], m_args[2], m_args[3], m_args[4], m_args[5], m_args[6], m_args[7], m_args[8]);
}
}
}
bool hasFlag(const QString &category, const QString &flag)
{
return category.section("/", 1, -1).split("/").contains(flag);
}
QString addFlag(QString category, const QString &flag)
{
if (category.isEmpty() || hasFlag(category, flag)) return category;
return category + "/" + flag;
}
QString CLogMessageHelper::addRedundantFlag(const QString &category) { return addFlag(category, "redundant"); }
QString CLogMessageHelper::addDebugFlag(const QString &category) { return addFlag(category, "debug"); }
QString CLogMessageHelper::stripFlags(const QString &category) { return category.section("/", 0, 1); }
bool CLogMessageHelper::hasRedundantFlag(const QString &category) { return hasFlag(category, "redundant"); }
bool CLogMessageHelper::hasDebugFlag(const QString &category) { return hasFlag(category, "debug") || category.isEmpty()
|| (QLoggingCategory::defaultCategory() && category == QLoggingCategory::defaultCategory()->categoryName()); }
}

178
src/blackmisc/logmessage.h Normal file
View File

@@ -0,0 +1,178 @@
/* Copyright (C) 2014
* Swift Project Community / Contributors
*
* This file is part of Swift Project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of Swift Project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
#ifndef BLACKMISC_LOGMESSAGE_H
#define BLACKMISC_LOGMESSAGE_H
//! \file
#include "statusmessage.h"
#include "index_sequence.h"
#include <QDebug>
#include <QLoggingCategory>
#include <QList>
#include <type_traits>
namespace BlackMisc
{
/*!
* Helper with static methods for dealing with metadata embedded in log message category strings.
*
* There are certain aspects of log messages which cannot be represented in Qt's native log message machinery.
* Therefore we are forced to use a special encoding of the message category string to encode these aspects.
*
* An encoded category string consists of a plain category string with zero or more flag strings appended.
* The plain category and the flags are all separated by forward-slash characters ('/').
*
* There are currently two flags:
* \li \c "debug" Qt only has 3 ordinary severities (debug, warning, critical), so we use QtMsgDebug for both
* debug and info messages, and we use this flag to distinguish between them.
* \li \c "redundant" To avoid handling the same message twice, this flag identifies a message which has already
* been directly returned as the return value of the method which generated it.
*/
class CLogMessageHelper
{
public:
//! Deleted constructor.
CLogMessageHelper() = delete;
//! Returns an encoded category string with the debug flag appended.
static QString addDebugFlag(const QString &category);
//! Returns an encoded category string with the redundant flag appended.
static QString addRedundantFlag(const QString &category);
//! Strips all flags from an encoded category string, returning only the plain category string.
static QString stripFlags(const QString &category);
//! Returns true if the given encoded category string has the debug flag.
static bool hasDebugFlag(const QString &category);
//! Returns true if the given encoded category string has the redundant flag.
static bool hasRedundantFlag(const QString &category);
};
/*!
* Class for emitting a log message. Works similar to the qDebug, qWarning, qCritical family of functions.
*
* The member functions debug, info, warning, error, and the stream operators all return a reference to <tt>*this</tt>,
* so they can be chained together.
*
* The category string identifies the origin of the message (e.g. network system) or its subtype (e.g. validation).
*/
class CLogMessage
{
public:
//! Constructor.
CLogMessage() {}
//! Constructor taking filename, line number, and function name, for verbose debug messages.
CLogMessage(const char *file, int line, const char *function): m_logger(file, line, function) {}
//! Destructor. This actually emits the message.
~CLogMessage();
//! Convert to CStatusMessage for returning the message directly from the function which generated it.
operator CStatusMessage();
//! Convert to CVariant for returning the message directly from the function which generated it.
operator CVariant();
//! Set the severity to debug, with the default category.
CLogMessage &debug() { return debugImpl(""); }
//! Set the severity to debug, with a category string.
CLogMessage &debug(QString category) { return debugImpl("", category); }
//! Set the severity to debug, with the category string obtained from the getMessageCategory method of the sender.
//! \note To avoid overload ambiguity, this method is disabled if T is not a class type.
template <class T, class = typename std::enable_if<std::is_class<typename std::decay<T>::type>::value>::type>
CLogMessage &debug(T *sender) { Q_UNUSED(sender); return debugImpl("", sender->getMessageCategory()); }
//! Set the severity to info, providing a format string, with the default category.
CLogMessage &info(QString format) { return infoImpl(format); }
//! Set the severity to info, providing a format string and category string.
CLogMessage &info(QString category, QString format) { return infoImpl(format, category); }
//! Set the severity to info, providing a format string, with the category string obtained from the getMessageCategory method of the sender.
//! \note To avoid overload ambiguity, this method is disabled if T is not a class type.
template <class T, class = typename std::enable_if<std::is_class<typename std::decay<T>::type>::value>::type>
CLogMessage &info(T *sender, QString format) { Q_UNUSED(sender); return infoImpl(format, sender->getMessageCategory()); }
//! Set the severity to warning, providing a format string, with the default category.
CLogMessage &warning(QString format) { return warningImpl(format); }
//! Set the severity to warning, providing a format string and category string.
CLogMessage &warning(QString category, QString format) { return warningImpl(format, category); }
//! Set the severity to warning, providing a format string, with the category string obtained from the getMessageCategory method of the sender.
//! \note To avoid overload ambiguity, this method is disabled if T is not a class type.
template <class T, class = typename std::enable_if<std::is_class<typename std::decay<T>::type>::value>::type>
CLogMessage &warning(T *sender, QString format) { Q_UNUSED(sender); return warningImpl(format, sender->getMessageCategory()); }
//! Set the severity to error, providing a format string, with the default category.
CLogMessage &error(QString format) { return errorImpl(format); }
//! Set the severity to error, providing a format string and category string.
CLogMessage &error(QString category, QString format) { return errorImpl(format, category); }
//! Set the severity to error, providing a format string, with the category string obtained from the getMessageCategory method of the sender.
template <class T, class = typename std::enable_if<std::is_class<typename std::decay<T>::type>::value>::type>
CLogMessage &error(T *sender, QString format) { Q_UNUSED(sender); return errorImpl(format, sender->getMessageCategory()); }
//! Streaming operators.
//! \details If the format string is empty, the message will consist of all streamed values separated by spaces.
//! Otherwise, the streamed values will replace the place markers %1, %2, %3... in the format string.
//! \see QString::arg
//! @{
CLogMessage &operator <<(const QString &v) { return arg(v); }
CLogMessage &operator <<(int v) { return arg(QString::number(v)); }
CLogMessage &operator <<(uint v) { return arg(QString::number(v)); }
CLogMessage &operator <<(long v) { return arg(QString::number(v)); }
CLogMessage &operator <<(ulong v) { return arg(QString::number(v)); }
CLogMessage &operator <<(qlonglong v) { return arg(QString::number(v)); }
CLogMessage &operator <<(qulonglong v) { return arg(QString::number(v)); }
CLogMessage &operator <<(short v) { return arg(QString::number(v)); }
CLogMessage &operator <<(ushort v) { return arg(QString::number(v)); }
CLogMessage &operator <<(QChar v) { return arg(v); }
CLogMessage &operator <<(char v) { return arg(QChar(v)); }
CLogMessage &operator <<(double v) { return arg(QString::number(v)); }
CLogMessage &operator <<(const CValueObject &v) { return arg(v.toQString()); }
//! @}
//! The default message category which is used if a category is not provided.
static const char *defaultMessageCategory() { return "swift"; }
private:
QMessageLogger m_logger;
CStatusMessage::StatusSeverity m_severity { CStatusMessage::SeverityDebug };
QString m_category;
QString m_message;
QStringList m_args;
bool m_redundant = false;
CLogMessage &debugImpl(QString format, QString category = defaultMessageCategory());
CLogMessage &infoImpl(QString format, QString category = defaultMessageCategory());
CLogMessage &warningImpl(QString format, QString category = defaultMessageCategory());
CLogMessage &errorImpl(QString format, QString category = defaultMessageCategory());
CLogMessage &arg(QString value) { m_args.push_back(value); return *this; }
QString message() const;
QByteArray encodedCategory() const;
QDebug ostream(const QByteArray &category) const;
};
}
/*!
* Convenience macro to construct a CLogMessage with the filename, line number, and function name,
* for verbose debug messages.
*/
#define BLACK_LOG (BlackMisc::CLogMessage{ __FILE__, __LINE__, Q_FUNC_INFO })
#endif

View File

@@ -153,7 +153,7 @@ namespace BlackMisc
CStatusMessage CTextMessage::asStatusMessage(bool withSender, bool withRecipient, const QString separator) const CStatusMessage CTextMessage::asStatusMessage(bool withSender, bool withRecipient, const QString separator) const
{ {
QString m = this->asString(withSender, withRecipient, separator); QString m = this->asString(withSender, withRecipient, separator);
return CStatusMessage::getInfoMessage(m, CStatusMessage::TypeTrafficNetwork); return { this->getMessageCategory(), CStatusMessage::SeverityInfo, m };
} }
/* /*

View File

@@ -42,6 +42,9 @@ namespace BlackMisc
CTextMessage(const QString &message, const BlackMisc::Aviation::CCallsign &senderCallsign, const BlackMisc::Aviation::CCallsign &recipientCallsign = BlackMisc::Aviation::CCallsign()) CTextMessage(const QString &message, const BlackMisc::Aviation::CCallsign &senderCallsign, const BlackMisc::Aviation::CCallsign &recipientCallsign = BlackMisc::Aviation::CCallsign())
: m_message(message), m_received(QDateTime::currentDateTimeUtc()), m_senderCallsign(senderCallsign), m_recipientCallsign(recipientCallsign), m_frequency(0, BlackMisc::PhysicalQuantities::CFrequencyUnit::nullUnit()) {} : m_message(message), m_received(QDateTime::currentDateTimeUtc()), m_senderCallsign(senderCallsign), m_recipientCallsign(recipientCallsign), m_frequency(0, BlackMisc::PhysicalQuantities::CFrequencyUnit::nullUnit()) {}
//! Logging category
static QString getMessageCategory() { return "swift.textMessage"; }
//! \brief Get callsign (from) //! \brief Get callsign (from)
const BlackMisc::Aviation::CCallsign &getSenderCallsign() const const BlackMisc::Aviation::CCallsign &getSenderCallsign() const
{ {

View File

@@ -11,17 +11,57 @@
#include "blackmiscfreefunctions.h" #include "blackmiscfreefunctions.h"
#include "propertyindex.h" #include "propertyindex.h"
#include "iconlist.h" #include "iconlist.h"
#include "loghandler.h"
#include "logmessage.h"
#include <QMetaEnum> #include <QMetaEnum>
namespace BlackMisc namespace BlackMisc
{ {
/* /*
* Constructor * Constructors
*/ */
CStatusMessage::CStatusMessage(StatusType type, StatusSeverity severity, const QString &message) CStatusMessage::CStatusMessage()
: m_type(type), m_severity(severity), m_message(message), m_timestamp(QDateTime::currentDateTimeUtc()) : m_timestamp(QDateTime::currentDateTimeUtc())
{ } {}
CStatusMessage::CStatusMessage(const QString &message)
: m_message(message), m_timestamp(QDateTime::currentDateTimeUtc())
{}
CStatusMessage::CStatusMessage(StatusSeverity severity, const QString &message)
: m_severity(severity), m_message(message), m_timestamp(QDateTime::currentDateTimeUtc())
{}
CStatusMessage::CStatusMessage(const QString &category, StatusSeverity severity, const QString &message)
: m_category(category), m_severity(severity), m_message(message), m_timestamp(QDateTime::currentDateTimeUtc())
{}
CStatusMessage::CStatusMessage(QtMsgType type, const QMessageLogContext &context, const QString &message)
: CStatusMessage(context.category, SeverityInfo, message)
{
m_redundant = CLogMessageHelper::hasRedundantFlag(m_category);
bool debug = CLogMessageHelper::hasDebugFlag(m_category);
m_category = CLogMessageHelper::stripFlags(m_category);
switch(type)
{
default:
case QtDebugMsg:
if (debug)
this->m_severity = SeverityDebug;
else
this->m_severity = SeverityInfo;
break;
case QtWarningMsg:
this->m_severity = SeverityWarning;
break;
case QtCriticalMsg:
case QtFatalMsg:
this->m_severity = SeverityError;
break;
}
}
/* /*
* Equal? * Equal?
@@ -41,11 +81,38 @@ namespace BlackMisc
} }
/* /*
* Constructor * Conversion
*/ */
CStatusMessage::CStatusMessage(StatusType type, StatusSeverity severity, const char *message) void CStatusMessage::toQtLogTriple(QtMsgType *o_type, QString *o_category, QString *o_message) const
: m_type(type), m_severity(severity), m_message(QString(message)), m_timestamp(QDateTime::currentDateTimeUtc()) {
{ } QString category = m_category;
if (this->m_severity == SeverityDebug && ! category.isEmpty())
{
category = CLogMessageHelper::addDebugFlag(category);
}
if (this->m_redundant)
{
category = CLogMessageHelper::addRedundantFlag(category);
}
*o_category = category;
*o_message = this->m_message;
switch (this->m_severity)
{
default:
case SeverityDebug:
case SeverityInfo:
*o_type = QtDebugMsg;
break;
case SeverityWarning:
*o_type = QtWarningMsg;
break;
case SeverityError:
*o_type = QtCriticalMsg;
break;
}
}
/* /*
* To string * To string
@@ -53,8 +120,8 @@ namespace BlackMisc
QString CStatusMessage::convertToQString(bool /** i18n */) const QString CStatusMessage::convertToQString(bool /** i18n */) const
{ {
QString s("Index: "); QString s("Category: ");
s.append(QString::number(this->m_type)); s.append(this->m_category);
s.append(" Severity: "); s.append(" Severity: ");
s.append(QString::number(this->m_severity)); s.append(QString::number(this->m_severity));
@@ -66,38 +133,6 @@ namespace BlackMisc
return s; return s;
} }
/*
* Validation Error
*/
CStatusMessage CStatusMessage::getValidationError(const QString &message)
{
return CStatusMessage(CStatusMessage::TypeValidation, CStatusMessage::SeverityError, message);
}
/*
* Unspecific info message
*/
CStatusMessage CStatusMessage::getInfoMessage(const QString &message, StatusType type)
{
return CStatusMessage(type, CStatusMessage::SeverityInfo, message);
}
/*
* Unspecific warning message
*/
CStatusMessage CStatusMessage::getWarningMessage(const QString &message, StatusType type)
{
return CStatusMessage(type, CStatusMessage::SeverityWarning, message);
}
/*
* Unspecific error message
*/
CStatusMessage CStatusMessage::getErrorMessage(const QString &message, StatusType type)
{
return CStatusMessage(type, CStatusMessage::SeverityError, message);
}
/* /*
* Pixmap * Pixmap
*/ */
@@ -105,6 +140,7 @@ namespace BlackMisc
{ {
switch (statusMessage.getSeverity()) switch (statusMessage.getSeverity())
{ {
case SeverityDebug: return CIconList::iconForIndex(CIcons::StandardIconUnknown16); // TODO
case SeverityInfo: return CIconList::iconForIndex(CIcons::StandardIconInfo16); case SeverityInfo: return CIconList::iconForIndex(CIcons::StandardIconInfo16);
case SeverityWarning: return CIconList::iconForIndex(CIcons::StandardIconWarning16); case SeverityWarning: return CIconList::iconForIndex(CIcons::StandardIconWarning16);
case SeverityError: return CIconList::iconForIndex(CIcons::StandardIconError16); case SeverityError: return CIconList::iconForIndex(CIcons::StandardIconError16);
@@ -112,70 +148,6 @@ namespace BlackMisc
} }
} }
/*
* Type
*/
const QString &CStatusMessage::getTypeAsString() const
{
switch (this->m_type)
{
case TypeAudio:
{
static QString t("audio");
return t;
}
case TypeCore:
{
static QString t("core");
return t;
}
case TypeGui:
{
static QString t("gui");
return t;
}
case TypeSettings:
{
static QString t("settings");
return t;
}
case TypeSimulator:
{
static QString t("simulator");
return t;
}
case TypeStdoutRedirect:
{
static QString t("redirection");
return t;
}
case TypeTrafficNetwork:
{
static QString t("traffic network");
return t;
}
case TypeUnknown:
{
static QString t("unknown");
return t;
}
case TypeUnspecific:
{
static QString t("unspecific");
return t;
}
case TypeValidation:
{
static QString t("validation");
return t;
}
default:
static QString x("unknown type");
qFatal("Unknown type");
return x;
}
}
/* /*
* Severity * Severity
*/ */
@@ -227,10 +199,8 @@ namespace BlackMisc
if (this->m_timestamp.isNull() || !this->m_timestamp.isValid()) return ""; if (this->m_timestamp.isNull() || !this->m_timestamp.isValid()) return "";
return this->m_timestamp.toString("HH:mm::ss.zzz"); return this->m_timestamp.toString("HH:mm::ss.zzz");
} }
case IndexType: case IndexCategory:
return QVariant(static_cast<uint>(this->m_type)); return QVariant(this->m_category);
case IndexTypeAsString:
return QVariant(this->getTypeAsString());
default: default:
break; break;
} }
@@ -262,8 +232,8 @@ namespace BlackMisc
case IndexSeverity: case IndexSeverity:
this->m_severity = static_cast<StatusSeverity>(variant.value<uint>()); this->m_severity = static_cast<StatusSeverity>(variant.value<uint>());
break; break;
case IndexType: case IndexCategory:
this->m_type = static_cast<StatusType>(variant.value<uint>()); this->m_category = variant.value<QString>();
break; break;
default: default:
CValueObject::setPropertyByIndex(variant, index); CValueObject::setPropertyByIndex(variant, index);

View File

@@ -25,24 +25,10 @@ namespace BlackMisc
class CStatusMessage : public CValueObjectStdTuple<CStatusMessage> class CStatusMessage : public CValueObjectStdTuple<CStatusMessage>
{ {
public: public:
//! Status types
enum StatusType
{
TypeUnknown, //!< not set
TypeUnspecific, //!< intentionally set, but not specific
TypeValidation,
TypeTrafficNetwork,
TypeSimulator,
TypeSettings,
TypeCore,
TypeAudio,
TypeGui,
TypeStdoutRedirect
};
//! Status severities //! Status severities
enum StatusSeverity enum StatusSeverity
{ {
SeverityDebug,
SeverityInfo, SeverityInfo,
SeverityWarning, SeverityWarning,
SeverityError SeverityError
@@ -51,8 +37,7 @@ namespace BlackMisc
//! Properties by index //! Properties by index
enum ColumnIndex enum ColumnIndex
{ {
IndexType = BlackMisc::CPropertyIndex::GlobalIndexCStatusMessage, IndexCategory = BlackMisc::CPropertyIndex::GlobalIndexCStatusMessage,
IndexTypeAsString,
IndexSeverity, IndexSeverity,
IndexSeverityAsString, IndexSeverityAsString,
IndexMessage, IndexMessage,
@@ -61,13 +46,24 @@ namespace BlackMisc
}; };
//! Constructor //! Constructor
CStatusMessage() : m_type(TypeUnknown), m_severity(SeverityInfo) {} CStatusMessage();
//! Constructor //! Constructor
CStatusMessage(StatusType type, StatusSeverity severity, const char *message); CStatusMessage(const QString &message);
//! Constructor //! Constructor
CStatusMessage(StatusType type, StatusSeverity severity, const QString &message); CStatusMessage(StatusSeverity severity, const QString &message);
//! Constructor
CStatusMessage(const QString &category, StatusSeverity severity, const QString &message);
//! Construct from a Qt logging triple
//! \sa QtMessageHandler
CStatusMessage(QtMsgType type, const QMessageLogContext &context, const QString &message);
//! Convert to a Qt logging triple
//! \sa QtMessageHandler
void toQtLogTriple(QtMsgType *o_type, QString *o_category, QString *o_message) const;
//! Equal operator == //! Equal operator ==
bool operator ==(const CStatusMessage &other) const; bool operator ==(const CStatusMessage &other) const;
@@ -75,10 +71,10 @@ namespace BlackMisc
//! Unequal operator != //! Unequal operator !=
bool operator !=(const CStatusMessage &other) const; bool operator !=(const CStatusMessage &other) const;
//! Status type //! Message category
StatusType getType() const { return this->m_type; } QString getCategory() const { return this->m_category; }
//! Status severity //! Message severity
StatusSeverity getSeverity() const { return this->m_severity; } StatusSeverity getSeverity() const { return this->m_severity; }
//! Message //! Message
@@ -87,8 +83,8 @@ namespace BlackMisc
//! Message empty //! Message empty
bool isEmpty() const { return this->m_message.isEmpty(); } bool isEmpty() const { return this->m_message.isEmpty(); }
//! Type as string //! Message may already have been handled directly
const QString &getTypeAsString() const; bool isRedundant() const { return this->m_redundant; }
//! Severity //! Severity
void setSeverity(StatusSeverity severity) { this->m_severity = severity; } void setSeverity(StatusSeverity severity) { this->m_severity = severity; }
@@ -108,18 +104,6 @@ namespace BlackMisc
//! To HTML //! To HTML
QString toHtml() const; QString toHtml() const;
//! Validation error
static CStatusMessage getValidationError(const QString &message);
//! (Unspecific) Info message
static CStatusMessage getInfoMessage(const QString &message, StatusType type = CStatusMessage::TypeUnspecific);
//! (Unspecific) Warning message
static CStatusMessage getWarningMessage(const QString &message, StatusType type = CStatusMessage::TypeUnspecific);
//! (Unspecific) Error message
static CStatusMessage getErrorMessage(const QString &message, StatusType type = CStatusMessage::TypeUnspecific);
//! Representing icon //! Representing icon
static const CIcon &convertToIcon(const CStatusMessage &statusMessage); static const CIcon &convertToIcon(const CStatusMessage &statusMessage);
@@ -129,16 +113,16 @@ namespace BlackMisc
private: private:
BLACK_ENABLE_TUPLE_CONVERSION(CStatusMessage) BLACK_ENABLE_TUPLE_CONVERSION(CStatusMessage)
StatusType m_type; QString m_category;
StatusSeverity m_severity; StatusSeverity m_severity;
QString m_message; QString m_message;
QDateTime m_timestamp; QDateTime m_timestamp;
bool m_redundant = false;
}; };
} // namespace } // namespace
BLACK_DECLARE_TUPLE_CONVERSION(BlackMisc::CStatusMessage, (o.m_type, o.m_severity, o.m_message, o.m_timestamp)) BLACK_DECLARE_TUPLE_CONVERSION(BlackMisc::CStatusMessage, (o.m_category, o.m_severity, o.m_message, o.m_timestamp, o.m_redundant))
Q_DECLARE_METATYPE(BlackMisc::CStatusMessage) Q_DECLARE_METATYPE(BlackMisc::CStatusMessage)
#endif // guard #endif // guard

View File

@@ -22,9 +22,9 @@ namespace BlackMisc
/* /*
* Messages by type * Messages by type
*/ */
CStatusMessageList CStatusMessageList::findByType(CStatusMessage::StatusType type) const CStatusMessageList CStatusMessageList::findByCategory(const QString &category) const
{ {
return this->findBy(&CStatusMessage::getType, type); return this->findBy(&CStatusMessage::getCategory, category);
} }
/* /*

View File

@@ -33,7 +33,7 @@ namespace BlackMisc
CStatusMessageList(const CSequence<CStatusMessage> &other); CStatusMessageList(const CSequence<CStatusMessage> &other);
//! Find by type //! Find by type
CStatusMessageList findByType(CStatusMessage::StatusType type) const; CStatusMessageList findByCategory(const QString &category) const;
//! Find by severity //! Find by severity
CStatusMessageList findBySeverity(CStatusMessage::StatusSeverity severity) const; CStatusMessageList findBySeverity(CStatusMessage::StatusSeverity severity) const;