refs #466 Resolved TODO items in logging.

This commit is contained in:
Mathew Sutcliffe
2015-09-14 21:35:58 +01:00
parent 64a25a7860
commit a102fc73b6
4 changed files with 62 additions and 15 deletions

View File

@@ -9,6 +9,7 @@
#include "logmessage.h"
#include "blackmiscfreefunctions.h"
#include "index_sequence.h"
namespace BlackMisc
{
@@ -76,8 +77,8 @@ namespace BlackMisc
{
// 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
// hack to avoid putting quote characters around the message
// should be safe, we could directly call qt_message_output instead, but it's undocumented
QByteArray category = qtCategory();
QDebug debug = ostream(category);
auto &stream = **reinterpret_cast<QTextStream **>(&debug); // should be safe because it is relying on Qt's guarantee of ABI compatibility
@@ -125,6 +126,12 @@ namespace BlackMisc
}
}
namespace Private
{
template <size_t... Is> QString arg(index_sequence<Is...>, const QString &format, const QStringList &args) { return format.arg(args[Is]...); }
QString arg(index_sequence<>, const QString &format, const QStringList &) { return format; }
}
QString CLogMessage::message() const
{
if (m_message.isEmpty())
@@ -133,20 +140,19 @@ namespace BlackMisc
}
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]);
case 0: return Private::arg(Private::make_index_sequence<0>(), m_message, m_args);
case 1: return Private::arg(Private::make_index_sequence<1>(), m_message, m_args);
case 2: return Private::arg(Private::make_index_sequence<2>(), m_message, m_args);
case 3: return Private::arg(Private::make_index_sequence<3>(), m_message, m_args);
case 4: return Private::arg(Private::make_index_sequence<4>(), m_message, m_args);
case 5: return Private::arg(Private::make_index_sequence<5>(), m_message, m_args);
case 6: return Private::arg(Private::make_index_sequence<6>(), m_message, m_args);
case 7: return Private::arg(Private::make_index_sequence<7>(), m_message, m_args);
case 8: return Private::arg(Private::make_index_sequence<8>(), m_message, m_args);
default: qWarning("Too many arguments"); // intentional fall-through
case 9: return Private::arg(Private::make_index_sequence<9>(), m_message, m_args);
}
}
}

View File

@@ -248,7 +248,21 @@ namespace BlackMisc
QString CLogPattern::convertToQString(bool i18n) const
{
Q_UNUSED(i18n);
return {}; //TODO
QString strategy;
QString categories = QStringList(m_strings.toList()).join("|");
switch (m_strategy)
{
case Everything: strategy = "none"; break;
case ExactMatch: strategy = "exact match:" + categories; break;
case AnyOf: strategy = "any of:" + categories; break;
case AllOf: strategy = "all of:" + categories; break;
case StartsWith: strategy = "starts with:" + categories; break;
case EndsWith: strategy = "ends with:" + categories; break;
case Contains: strategy = "contains:" + categories; break;
case Nothing: strategy = "none"; break;
default: strategy = "<invalid>"; break;
}
return "{" + CStatusMessage::severitiesToString(m_severities) + "," + strategy + "}";
}
void CLogPattern::marshallToDbus(QDBusArgument &argument) const

View File

@@ -236,6 +236,30 @@ namespace BlackMisc
}
}
QString CStatusMessage::severitiesToString(const QSet<CStatusMessage::StatusSeverity> &severities)
{
auto minmax = std::minmax_element(severities.begin(), severities.end());
auto min = *minmax.first;
auto max = *minmax.second;
if (min == SeverityDebug && max == SeverityError)
{
return "all severities";
}
if (min == SeverityDebug)
{
return "at or below " + severityToString(max);
}
if (max == SeverityError)
{
return "at or above " + severityToString(min);
}
auto list = severities.toList();
std::sort(list.begin(), list.end());
QStringList ret;
std::transform(list.cbegin(), list.cend(), std::back_inserter(ret), severityToString);
return ret.join("|");
}
const QString &CStatusMessage::getSeverityAsString() const
{
return severityToString(this->m_severity);

View File

@@ -132,6 +132,9 @@ namespace BlackMisc
//! Severity as string
static const QString &severityToString(StatusSeverity severity);
//! Severity set as string
static QString severitiesToString(const QSet<StatusSeverity> &severities);
//! Severity as string, if not possible to convert \sa CSeverityInfo
static StatusSeverity stringToSeverity(const QString &severity);