mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-27 02:55:44 +08:00
refs #336 Added class CLogCategory to represent a log category. The category logic of CLogMessage is moved into this new class.
This commit is contained in:
@@ -173,6 +173,8 @@ void BlackMisc::registerMetadata()
|
|||||||
CIcon::registerMetadata();
|
CIcon::registerMetadata();
|
||||||
CIconList::registerMetadata();
|
CIconList::registerMetadata();
|
||||||
CHotkeyFunction::registerMetadata();
|
CHotkeyFunction::registerMetadata();
|
||||||
|
CLogCategory::registerMetadata();
|
||||||
|
CLogCategoryList::registerMetadata();
|
||||||
|
|
||||||
// sub namespaces
|
// sub namespaces
|
||||||
PhysicalQuantities::registerMetadata();
|
PhysicalQuantities::registerMetadata();
|
||||||
|
|||||||
67
src/blackmisc/logcategory.cpp
Normal file
67
src/blackmisc/logcategory.cpp
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
/* 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 "logcategory.h"
|
||||||
|
|
||||||
|
namespace BlackMisc
|
||||||
|
{
|
||||||
|
void CLogCategory::registerMetadata()
|
||||||
|
{
|
||||||
|
qRegisterMetaType<CLogCategory>();
|
||||||
|
qDBusRegisterMetaType<CLogCategory>();
|
||||||
|
}
|
||||||
|
|
||||||
|
uint CLogCategory::getValueHash() const
|
||||||
|
{
|
||||||
|
return qHash(TupleConverter<CLogCategory>::toMetaTuple(*this));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CLogCategory::operator ==(const CLogCategory &other) const
|
||||||
|
{
|
||||||
|
return TupleConverter<CLogCategory>::toMetaTuple(*this) == TupleConverter<CLogCategory>::toMetaTuple(other);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CLogCategory::operator !=(const CLogCategory &other) const
|
||||||
|
{
|
||||||
|
return TupleConverter<CLogCategory>::toMetaTuple(*this) != TupleConverter<CLogCategory>::toMetaTuple(other);
|
||||||
|
}
|
||||||
|
|
||||||
|
QString CLogCategory::convertToQString(bool i18n) const
|
||||||
|
{
|
||||||
|
Q_UNUSED(i18n);
|
||||||
|
return m_string;
|
||||||
|
}
|
||||||
|
|
||||||
|
int CLogCategory::getMetaTypeId() const
|
||||||
|
{
|
||||||
|
return qMetaTypeId<CLogCategory>();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CLogCategory::isA(int metaTypeId) const
|
||||||
|
{
|
||||||
|
if (metaTypeId == qMetaTypeId<CLogCategory>()) { return true; }
|
||||||
|
return this->CValueObject::isA(metaTypeId);
|
||||||
|
}
|
||||||
|
|
||||||
|
int CLogCategory::compareImpl(const CValueObject &otherBase) const
|
||||||
|
{
|
||||||
|
const auto &other = static_cast<const CLogCategory &>(otherBase);
|
||||||
|
return compare(TupleConverter<CLogCategory>::toMetaTuple(*this), TupleConverter<CLogCategory>::toMetaTuple(other));
|
||||||
|
}
|
||||||
|
|
||||||
|
void CLogCategory::marshallToDbus(QDBusArgument &argument) const
|
||||||
|
{
|
||||||
|
argument << TupleConverter<CLogCategory>::toMetaTuple(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CLogCategory::unmarshallFromDbus(const QDBusArgument &argument)
|
||||||
|
{
|
||||||
|
argument >> TupleConverter<CLogCategory>::toMetaTuple(*this);
|
||||||
|
}
|
||||||
|
}
|
||||||
153
src/blackmisc/logcategory.h
Normal file
153
src/blackmisc/logcategory.h
Normal file
@@ -0,0 +1,153 @@
|
|||||||
|
/* 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_LOGCATEGORY_H
|
||||||
|
#define BLACKMISC_LOGCATEGORY_H
|
||||||
|
|
||||||
|
//! \file
|
||||||
|
|
||||||
|
#include "sequence.h"
|
||||||
|
|
||||||
|
namespace BlackMisc
|
||||||
|
{
|
||||||
|
/*!
|
||||||
|
* A log category is an arbitrary string tag which can be attached to log messages.
|
||||||
|
*
|
||||||
|
* A log handler can filter messages based on their categories.
|
||||||
|
*/
|
||||||
|
class CLogCategory : public CValueObject
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
//! \name Predefined special categories (public static methods)
|
||||||
|
//! @{
|
||||||
|
|
||||||
|
//! Uncategorized
|
||||||
|
static const CLogCategory &uncategorized()
|
||||||
|
{
|
||||||
|
static const CLogCategory cat { "swift.uncategorized" };
|
||||||
|
return cat;
|
||||||
|
}
|
||||||
|
|
||||||
|
//! Validation
|
||||||
|
static const CLogCategory &validation()
|
||||||
|
{
|
||||||
|
static const CLogCategory cat { "swift.validation" };
|
||||||
|
return cat;
|
||||||
|
}
|
||||||
|
|
||||||
|
//! Settings updates
|
||||||
|
static const CLogCategory &settingsUpdate()
|
||||||
|
{
|
||||||
|
static const CLogCategory cat { "swift.settings.update" };
|
||||||
|
return cat;
|
||||||
|
}
|
||||||
|
|
||||||
|
//! Contexts
|
||||||
|
static const CLogCategory &context()
|
||||||
|
{
|
||||||
|
static const CLogCategory cat { "swift.context" };
|
||||||
|
return cat;
|
||||||
|
}
|
||||||
|
|
||||||
|
//! Context slots
|
||||||
|
static const CLogCategory &contextSlot()
|
||||||
|
{
|
||||||
|
static const CLogCategory cat { "swift.context.slot" };
|
||||||
|
return cat;
|
||||||
|
}
|
||||||
|
|
||||||
|
//! GUI components
|
||||||
|
static const CLogCategory &guiComponent()
|
||||||
|
{
|
||||||
|
static const CLogCategory cat { "swift.gui.component" };
|
||||||
|
return cat;
|
||||||
|
}
|
||||||
|
|
||||||
|
//! All predefined special categories
|
||||||
|
static const QList<CLogCategory> &allSpecialCategories()
|
||||||
|
{
|
||||||
|
static const QList<CLogCategory> cats
|
||||||
|
{
|
||||||
|
uncategorized(),
|
||||||
|
validation(),
|
||||||
|
settingsUpdate(),
|
||||||
|
context(),
|
||||||
|
contextSlot(),
|
||||||
|
guiComponent()
|
||||||
|
};
|
||||||
|
return cats;
|
||||||
|
}
|
||||||
|
|
||||||
|
//! @}
|
||||||
|
|
||||||
|
//! Constructor.
|
||||||
|
CLogCategory() = default;
|
||||||
|
|
||||||
|
//! Constructor.
|
||||||
|
CLogCategory(const QString &categoryString) : m_string(categoryString) {}
|
||||||
|
|
||||||
|
//! Constructor.
|
||||||
|
CLogCategory(const char *categoryString) : m_string(categoryString) {}
|
||||||
|
|
||||||
|
//! Returns true if the category string starts with the given prefix.
|
||||||
|
bool startsWith(const QString &prefix) const { return m_string.startsWith(prefix); }
|
||||||
|
|
||||||
|
//! Returns true if the category string ends with the given suffix.
|
||||||
|
bool endsWith(const QString &suffix) const { return m_string.endsWith(suffix); }
|
||||||
|
|
||||||
|
//! Returns true if the category string contains the given substring.
|
||||||
|
bool contains(const QString &substring) const { return m_string.contains(substring); }
|
||||||
|
|
||||||
|
//! Register metadata
|
||||||
|
static void registerMetadata();
|
||||||
|
|
||||||
|
//! \copydoc CValueObject::toQVariant
|
||||||
|
virtual QVariant toQVariant() const override { return QVariant::fromValue(*this); }
|
||||||
|
|
||||||
|
//! \copydoc CValueObject::convertFromQVariant
|
||||||
|
virtual void convertFromQVariant(const QVariant &variant) override { BlackMisc::setFromQVariant(this, variant); }
|
||||||
|
|
||||||
|
//! \copydoc CValueObject::getValueHash
|
||||||
|
virtual uint getValueHash() const override;
|
||||||
|
|
||||||
|
//! Equal operator
|
||||||
|
bool operator ==(const CLogCategory &other) const;
|
||||||
|
|
||||||
|
//! Not equal operator
|
||||||
|
bool operator !=(const CLogCategory &other) const;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
//! \copydoc CValueObject::convertToQString()
|
||||||
|
virtual QString convertToQString(bool i18n = false) const override;
|
||||||
|
|
||||||
|
//! \copydoc CValueObject::getMetaTypeId
|
||||||
|
virtual int getMetaTypeId() const override;
|
||||||
|
|
||||||
|
//! \copydoc CValueObject::isA
|
||||||
|
virtual bool isA(int metaTypeId) const override;
|
||||||
|
|
||||||
|
//! \copydoc CValueObject::compareImpl
|
||||||
|
virtual int compareImpl(const CValueObject &other) const override;
|
||||||
|
|
||||||
|
//! \copydoc CValueObject::marshallToDbus()
|
||||||
|
virtual void marshallToDbus(QDBusArgument &argument) const override;
|
||||||
|
|
||||||
|
//! \copydoc CValueObject::marshallFromDbus()
|
||||||
|
virtual void unmarshallFromDbus(const QDBusArgument &argument) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
BLACK_ENABLE_TUPLE_CONVERSION(CLogCategory)
|
||||||
|
QString m_string;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
Q_DECLARE_METATYPE(BlackMisc::CLogCategory)
|
||||||
|
BLACK_DECLARE_TUPLE_CONVERSION(BlackMisc::CLogCategory, (o.m_string))
|
||||||
|
|
||||||
|
#endif
|
||||||
58
src/blackmisc/logcategorylist.cpp
Normal file
58
src/blackmisc/logcategorylist.cpp
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
/* 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 "logcategorylist.h"
|
||||||
|
|
||||||
|
namespace BlackMisc
|
||||||
|
{
|
||||||
|
void CLogCategoryList::appendCategoriesFromMetaObject(const QMetaObject &metaObject)
|
||||||
|
{
|
||||||
|
for (auto *meta = &metaObject; meta; meta = meta->superClass())
|
||||||
|
{
|
||||||
|
push_back(meta->className());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
QStringList CLogCategoryList::toQStringList() const
|
||||||
|
{
|
||||||
|
return transform([](const CLogCategory &cat) { return cat.toQString(); });
|
||||||
|
}
|
||||||
|
|
||||||
|
QString CLogCategoryList::convertToQString(bool i18n) const
|
||||||
|
{
|
||||||
|
Q_UNUSED(i18n); // log categories are always Latin-1
|
||||||
|
return toQStringList().join("|");
|
||||||
|
}
|
||||||
|
|
||||||
|
CLogCategoryList CLogCategoryList::fromQStringList(const QStringList &stringList)
|
||||||
|
{
|
||||||
|
return makeRange(Iterators::makeTransformIterator(stringList.begin(), [](const QString &str) { return CLogCategory{str}; }), stringList.end());
|
||||||
|
}
|
||||||
|
|
||||||
|
CLogCategoryList CLogCategoryList::fromQString(const QString &string)
|
||||||
|
{
|
||||||
|
return fromQStringList(string.split("|"));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CLogCategoryList::anyStartWith(const QString &prefix) const
|
||||||
|
{
|
||||||
|
return containsBy([ = ](const CLogCategory &cat) { return cat.startsWith(prefix); });
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CLogCategoryList::anyEndWith(const QString &suffix) const
|
||||||
|
{
|
||||||
|
return containsBy([ = ](const CLogCategory &cat) { return cat.endsWith(suffix); });
|
||||||
|
}
|
||||||
|
|
||||||
|
void CLogCategoryList::registerMetadata()
|
||||||
|
{
|
||||||
|
qRegisterMetaType<CLogCategoryList>();
|
||||||
|
qDBusRegisterMetaType<CLogCategoryList>();
|
||||||
|
}
|
||||||
|
}
|
||||||
144
src/blackmisc/logcategorylist.h
Normal file
144
src/blackmisc/logcategorylist.h
Normal file
@@ -0,0 +1,144 @@
|
|||||||
|
/* 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_LOGCATEGORYLIST_H
|
||||||
|
#define BLACKMISC_LOGCATEGORYLIST_H
|
||||||
|
|
||||||
|
//! \file
|
||||||
|
|
||||||
|
#include "logcategory.h"
|
||||||
|
#include "sequence.h"
|
||||||
|
#include "collection.h"
|
||||||
|
#include <QObject>
|
||||||
|
#include <QThreadStorage>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
|
namespace BlackMisc
|
||||||
|
{
|
||||||
|
namespace Private
|
||||||
|
{
|
||||||
|
//! \private Trait to detect whether a class T has a member function called getLogCategories
|
||||||
|
template <class T> class HasGetLogCategories
|
||||||
|
{
|
||||||
|
struct Base { int getLogCategories; };
|
||||||
|
struct Derived : T, Base {};
|
||||||
|
template <typename U, U> struct TypeCheck {};
|
||||||
|
template <typename U> static std::false_type test(TypeCheck<decltype(&Base::getLogCategories), &U::getLogCategories> *);
|
||||||
|
template <typename U> static std::true_type test(...);
|
||||||
|
public:
|
||||||
|
using type = decltype(test<Derived>(nullptr));
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* A sequence of log categories.
|
||||||
|
*/
|
||||||
|
class CLogCategoryList : public CSequence<CLogCategory>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
//! Empty constructor.
|
||||||
|
CLogCategoryList() = default;
|
||||||
|
|
||||||
|
//! Copy constructor.
|
||||||
|
CLogCategoryList(const CLogCategoryList &) = default;
|
||||||
|
|
||||||
|
//! Copy construct from superclass instance.
|
||||||
|
CLogCategoryList(const CSequence<CLogCategory> &other) : CSequence<CLogCategory>(other) {}
|
||||||
|
|
||||||
|
//! Initializer list constructor.
|
||||||
|
CLogCategoryList(std::initializer_list<CLogCategory> il) : CSequence<CLogCategory>(il) {}
|
||||||
|
|
||||||
|
//! Copy assignment.
|
||||||
|
CLogCategoryList &operator =(const CLogCategoryList &) = default;
|
||||||
|
|
||||||
|
//! Copy assign from superclass instance.
|
||||||
|
CLogCategoryList &operator =(const CSequence<CLogCategory> &other) { CSequence<CLogCategory>::operator =(other); return *this; }
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* Construct by extracting categories from a class T.
|
||||||
|
*
|
||||||
|
* If T has a member function getLogCategories, then this will be called.
|
||||||
|
* If T inherits from QObject then we get the name of the class and all its superclasses.
|
||||||
|
* If T is registered with QMetaType then we get the name of the class.
|
||||||
|
* If more than one of the above cases is true, then all are combined into the list.
|
||||||
|
* If none of the above cases is true, then the constructed list will be the uncategorized category.
|
||||||
|
*
|
||||||
|
* This constructor will be disabled if T is not a class type.
|
||||||
|
*
|
||||||
|
* \param pointer The value of pointer is unimportant. Only the static type T is considered.
|
||||||
|
* It is legal to pass static_cast<T>(nullptr), but in member functions passing the <tt>this</tt> pointer is easier.
|
||||||
|
*/
|
||||||
|
template <typename T, typename = typename std::enable_if<std::is_class<T>::value>::type>
|
||||||
|
CLogCategoryList(const T *pointer) : CLogCategoryList(fromClass<T>()) { Q_UNUSED(pointer); }
|
||||||
|
|
||||||
|
//! Convert each of the categories to a QString and return the result as a QStringList.
|
||||||
|
QStringList toQStringList() const;
|
||||||
|
|
||||||
|
//! Convert a string list, such as that returned by toQStringList(), into a CLogCategoryList.
|
||||||
|
static CLogCategoryList fromQStringList(const QStringList &stringList);
|
||||||
|
|
||||||
|
//! Convert a string, such as that returned by toQString(), into a CLogCategoryList.
|
||||||
|
static CLogCategoryList fromQString(const QString &string);
|
||||||
|
|
||||||
|
//! Returns true if any of the categories in the list start with the given prefix.
|
||||||
|
bool anyStartWith(const QString &prefix) const;
|
||||||
|
|
||||||
|
//! Returns true if any of the categories in the list end with the given suffix.
|
||||||
|
bool anyEndWith(const QString &suffix) const;
|
||||||
|
|
||||||
|
//! Register metadata
|
||||||
|
static void registerMetadata();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
//! \copydoc BlackMisc::CValueObject::convertToQString
|
||||||
|
virtual QString convertToQString(bool i18n = false) const override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
/*
|
||||||
|
* Templates used by the constructor template:
|
||||||
|
*/
|
||||||
|
template <typename T>
|
||||||
|
struct tag {};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
static const CLogCategoryList &fromClass()
|
||||||
|
{
|
||||||
|
static_assert(sizeof(T) > 0, "T must be a complete type, not forward declared");
|
||||||
|
static QThreadStorage<CLogCategoryList> list;
|
||||||
|
if (! list.hasLocalData())
|
||||||
|
{
|
||||||
|
list.localData().appendCategoriesFromMemberFunction(tag<T>(), typename Private::HasGetLogCategories<T>::type());
|
||||||
|
list.localData().appendCategoriesFromMetaType(tag<T>(), std::integral_constant<bool, QMetaTypeId<T>::Defined>());
|
||||||
|
list.localData().appendCategoriesFromMetaObject(tag<T>(), std::is_base_of<QObject, T>());
|
||||||
|
if (list.localData().isEmpty()) { list.localData().push_back(CLogCategory::uncategorized()); }
|
||||||
|
}
|
||||||
|
return list.localData();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void appendCategoriesFromMemberFunction(tag<T>, std::true_type) { push_back(T::getLogCategories()); }
|
||||||
|
void appendCategoriesFromMemberFunction(...) {}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void appendCategoriesFromMetaType(tag<T>, std::true_type) { push_back(QMetaType::typeName(qMetaTypeId<T>())); }
|
||||||
|
void appendCategoriesFromMetaType(...) {}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void appendCategoriesFromMetaObject(tag<T>, std::true_type) { appendCategoriesFromMetaObject(T::staticMetaObject); }
|
||||||
|
void appendCategoriesFromMetaObject(...) {}
|
||||||
|
|
||||||
|
void appendCategoriesFromMetaObject(const QMetaObject &);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
Q_DECLARE_METATYPE(BlackMisc::CLogCategoryList)
|
||||||
|
Q_DECLARE_METATYPE(BlackMisc::CCollection<BlackMisc::CLogCategory>)
|
||||||
|
Q_DECLARE_METATYPE(BlackMisc::CSequence<BlackMisc::CLogCategory>)
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -64,12 +64,12 @@ namespace BlackMisc
|
|||||||
return m_categoryPrefixHandlers[category];
|
return m_categoryPrefixHandlers[category];
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<CLogCategoryHandler *> CLogHandler::handlersForCategory(const QString &category) const
|
QList<CLogCategoryHandler *> CLogHandler::handlersForCategories(const CLogCategoryList &categories) const
|
||||||
{
|
{
|
||||||
QList<CLogCategoryHandler *> m_handlers;
|
QList<CLogCategoryHandler *> m_handlers;
|
||||||
for (auto i = m_categoryPrefixHandlers.begin(); i != m_categoryPrefixHandlers.end(); ++i)
|
for (auto i = m_categoryPrefixHandlers.begin(); i != m_categoryPrefixHandlers.end(); ++i)
|
||||||
{
|
{
|
||||||
if (category.startsWith(i.key()))
|
if (categories.anyStartWith(i.key()))
|
||||||
{
|
{
|
||||||
m_handlers.push_back(i.value());
|
m_handlers.push_back(i.value());
|
||||||
}
|
}
|
||||||
@@ -114,7 +114,7 @@ namespace BlackMisc
|
|||||||
|
|
||||||
void CLogHandler::logMessage(const CStatusMessage &statusMessage)
|
void CLogHandler::logMessage(const CStatusMessage &statusMessage)
|
||||||
{
|
{
|
||||||
auto handlers = handlersForCategory(statusMessage.getCategory());
|
auto handlers = handlersForCategories(statusMessage.getCategories());
|
||||||
|
|
||||||
if (isFallThroughEnabled(handlers))
|
if (isFallThroughEnabled(handlers))
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ namespace BlackMisc
|
|||||||
bool m_enableFallThrough = true;
|
bool m_enableFallThrough = true;
|
||||||
bool isFallThroughEnabled(const QList<CLogCategoryHandler *> &handlers) const;
|
bool isFallThroughEnabled(const QList<CLogCategoryHandler *> &handlers) const;
|
||||||
QMap<QString, CLogCategoryHandler *> m_categoryPrefixHandlers;
|
QMap<QString, CLogCategoryHandler *> m_categoryPrefixHandlers;
|
||||||
QList<CLogCategoryHandler *> handlersForCategory(const QString &category) const;
|
QList<CLogCategoryHandler *> handlersForCategories(const CLogCategoryList &categories) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
|||||||
@@ -13,34 +13,29 @@
|
|||||||
namespace BlackMisc
|
namespace BlackMisc
|
||||||
{
|
{
|
||||||
|
|
||||||
CLogMessage &CLogMessage::debugImpl(QString format, QString category)
|
CLogMessage &CLogMessage::debug()
|
||||||
{
|
{
|
||||||
m_severity = CStatusMessage::SeverityDebug;
|
m_severity = CStatusMessage::SeverityDebug;
|
||||||
m_category = category;
|
|
||||||
m_message = format;
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
CLogMessage &CLogMessage::infoImpl(QString format, QString category)
|
CLogMessage &CLogMessage::info(QString format)
|
||||||
{
|
{
|
||||||
m_severity = CStatusMessage::SeverityInfo;
|
m_severity = CStatusMessage::SeverityInfo;
|
||||||
m_category = category;
|
|
||||||
m_message = format;
|
m_message = format;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
CLogMessage &CLogMessage::warningImpl(QString format, QString category)
|
CLogMessage &CLogMessage::warning(QString format)
|
||||||
{
|
{
|
||||||
m_severity = CStatusMessage::SeverityWarning;
|
m_severity = CStatusMessage::SeverityWarning;
|
||||||
m_category = category;
|
|
||||||
m_message = format;
|
m_message = format;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
CLogMessage &CLogMessage::errorImpl(QString format, QString category)
|
CLogMessage &CLogMessage::error(QString format)
|
||||||
{
|
{
|
||||||
m_severity = CStatusMessage::SeverityError;
|
m_severity = CStatusMessage::SeverityError;
|
||||||
m_category = category;
|
|
||||||
m_message = format;
|
m_message = format;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@@ -48,7 +43,7 @@ namespace BlackMisc
|
|||||||
CLogMessage::operator CStatusMessage()
|
CLogMessage::operator CStatusMessage()
|
||||||
{
|
{
|
||||||
m_redundant = true;
|
m_redundant = true;
|
||||||
return { m_category, m_severity, message() };
|
return { m_categories, m_severity, message() };
|
||||||
}
|
}
|
||||||
|
|
||||||
CLogMessage::operator CVariant()
|
CLogMessage::operator CVariant()
|
||||||
@@ -62,21 +57,21 @@ namespace BlackMisc
|
|||||||
|
|
||||||
// FIXME hack to avoid putting 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
|
// should be safe, but still it's horrible, we could directly call qt_message_output instead
|
||||||
QByteArray category = encodedCategory();
|
QByteArray category = qtCategory();
|
||||||
QDebug debug = ostream(category);
|
QDebug debug = ostream(category);
|
||||||
auto &stream = **reinterpret_cast<QTextStream**>(&debug); // should be safe because it is relying on Qt's guarantee of ABI compatibility
|
auto &stream = **reinterpret_cast<QTextStream**>(&debug); // should be safe because it is relying on Qt's guarantee of ABI compatibility
|
||||||
stream << message();
|
stream << message();
|
||||||
}
|
}
|
||||||
|
|
||||||
QByteArray CLogMessage::encodedCategory() const
|
QByteArray CLogMessage::qtCategory() const
|
||||||
{
|
{
|
||||||
if (m_category.isEmpty())
|
if (m_categories.isEmpty())
|
||||||
{
|
{
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
QString category = m_category;
|
QString category = m_categories.toQString();
|
||||||
if (m_severity == CStatusMessage::SeverityDebug) { category = CLogMessageHelper::addDebugFlag(category); }
|
if (m_severity == CStatusMessage::SeverityDebug) { category = CLogMessageHelper::addDebugFlag(category); }
|
||||||
if (m_redundant) { category = CLogMessageHelper::addRedundantFlag(category); }
|
if (m_redundant) { category = CLogMessageHelper::addRedundantFlag(category); }
|
||||||
return category.toLatin1();
|
return category.toLatin1();
|
||||||
@@ -85,7 +80,7 @@ namespace BlackMisc
|
|||||||
|
|
||||||
QDebug CLogMessage::ostream(const QByteArray &category) const
|
QDebug CLogMessage::ostream(const QByteArray &category) const
|
||||||
{
|
{
|
||||||
if (m_category.isEmpty())
|
if (m_categories.isEmpty())
|
||||||
{
|
{
|
||||||
switch (m_severity)
|
switch (m_severity)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
//! \file
|
//! \file
|
||||||
|
|
||||||
#include "statusmessage.h"
|
#include "statusmessage.h"
|
||||||
|
#include "logcategorylist.h"
|
||||||
#include "index_sequence.h"
|
#include "index_sequence.h"
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QLoggingCategory>
|
#include <QLoggingCategory>
|
||||||
@@ -64,16 +65,29 @@ namespace BlackMisc
|
|||||||
* The member functions debug, info, warning, error, and the stream operators all return a reference to <tt>*this</tt>,
|
* 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.
|
* 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).
|
* The categories are arbitrary string tags which can be attached to the message to categorize it.
|
||||||
|
* A message can have more than one category. The categories can be used for filtering by message handlers.
|
||||||
*/
|
*/
|
||||||
class CLogMessage
|
class CLogMessage
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
//! Constructor.
|
//! Construct a message with the "uncategorized" category.
|
||||||
CLogMessage() {}
|
CLogMessage() {}
|
||||||
|
|
||||||
//! Constructor taking filename, line number, and function name, for verbose debug messages.
|
//! Constructor taking filename, line number, and function name, for uncategorized verbose debug messages.
|
||||||
CLogMessage(const char *file, int line, const char *function): m_logger(file, line, function) {}
|
CLogMessage(const char *file, int line, const char *function) : m_logger(file, line, function) {}
|
||||||
|
|
||||||
|
//! Construct a message with some specific category.
|
||||||
|
CLogMessage(const CLogCategory &category) : m_categories({ category }) {}
|
||||||
|
|
||||||
|
//! Construct a message with some specific categories.
|
||||||
|
CLogMessage(const CLogCategoryList &categories) : m_categories(categories) {}
|
||||||
|
|
||||||
|
//! Construct a message with some specific categories.
|
||||||
|
CLogMessage(const CLogCategoryList &categories, const CLogCategory &extra) : CLogMessage(categories) { m_categories.push_back(extra); }
|
||||||
|
|
||||||
|
//! Construct a message with some specific categories.
|
||||||
|
CLogMessage(const CLogCategoryList &categories, const CLogCategoryList &extra) : CLogMessage(categories) { m_categories.push_back(extra); }
|
||||||
|
|
||||||
//! Destructor. This actually emits the message.
|
//! Destructor. This actually emits the message.
|
||||||
~CLogMessage();
|
~CLogMessage();
|
||||||
@@ -84,48 +98,17 @@ namespace BlackMisc
|
|||||||
//! Convert to CVariant for returning the message directly from the function which generated it.
|
//! Convert to CVariant for returning the message directly from the function which generated it.
|
||||||
operator CVariant();
|
operator CVariant();
|
||||||
|
|
||||||
//! Set the severity to debug, with the default category.
|
//! Set the severity to debug.
|
||||||
CLogMessage &debug() { return debugImpl(""); }
|
CLogMessage &debug();
|
||||||
|
|
||||||
//! Set the severity to debug, with a category string.
|
//! Set the severity to info, providing a format string.
|
||||||
CLogMessage &debug(QString category) { return debugImpl("", category); }
|
CLogMessage &info(QString format);
|
||||||
|
|
||||||
//! Set the severity to debug, with the category string obtained from the getMessageCategory method of the sender.
|
//! Set the severity to warning, providing a format string.
|
||||||
//! \note To avoid overload ambiguity, this method is disabled if T is not a class type.
|
CLogMessage &warning(QString format);
|
||||||
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.
|
//! Set the severity to error, providing a format string.
|
||||||
CLogMessage &info(QString format) { return infoImpl(format); }
|
CLogMessage &error(QString 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.
|
//! Streaming operators.
|
||||||
//! \details If the format string is empty, the message will consist of all streamed values separated by spaces.
|
//! \details If the format string is empty, the message will consist of all streamed values separated by spaces.
|
||||||
@@ -147,24 +130,17 @@ namespace BlackMisc
|
|||||||
CLogMessage &operator <<(const CValueObject &v) { return arg(v.toQString()); }
|
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:
|
private:
|
||||||
QMessageLogger m_logger;
|
QMessageLogger m_logger;
|
||||||
CStatusMessage::StatusSeverity m_severity = CStatusMessage::SeverityDebug;
|
CStatusMessage::StatusSeverity m_severity = CStatusMessage::SeverityDebug;
|
||||||
QString m_category;
|
CLogCategoryList m_categories = CLogCategoryList { CLogCategory::uncategorized() };
|
||||||
QString m_message;
|
QString m_message;
|
||||||
QStringList m_args;
|
QStringList m_args;
|
||||||
bool m_redundant = false;
|
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; }
|
CLogMessage &arg(QString value) { m_args.push_back(value); return *this; }
|
||||||
QString message() const;
|
QString message() const;
|
||||||
QByteArray encodedCategory() const;
|
QByteArray qtCategory() const;
|
||||||
QDebug ostream(const QByteArray &category) const;
|
QDebug ostream(const QByteArray &category) const;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,16 +33,17 @@ namespace BlackMisc
|
|||||||
: m_severity(severity), m_message(message), m_timestamp(QDateTime::currentDateTimeUtc())
|
: m_severity(severity), m_message(message), m_timestamp(QDateTime::currentDateTimeUtc())
|
||||||
{}
|
{}
|
||||||
|
|
||||||
CStatusMessage::CStatusMessage(const QString &category, StatusSeverity severity, const QString &message)
|
CStatusMessage::CStatusMessage(const CLogCategoryList &categories, StatusSeverity severity, const QString &message)
|
||||||
: m_category(category), m_severity(severity), m_message(message), m_timestamp(QDateTime::currentDateTimeUtc())
|
: m_categories(categories), m_severity(severity), m_message(message), m_timestamp(QDateTime::currentDateTimeUtc())
|
||||||
{}
|
{}
|
||||||
|
|
||||||
CStatusMessage::CStatusMessage(QtMsgType type, const QMessageLogContext &context, const QString &message)
|
CStatusMessage::CStatusMessage(QtMsgType type, const QMessageLogContext &context, const QString &message)
|
||||||
: CStatusMessage(context.category, SeverityInfo, message)
|
: CStatusMessage(message)
|
||||||
{
|
{
|
||||||
m_redundant = CLogMessageHelper::hasRedundantFlag(m_category);
|
m_redundant = CLogMessageHelper::hasRedundantFlag(context.category);
|
||||||
bool debug = CLogMessageHelper::hasDebugFlag(m_category);
|
bool debug = CLogMessageHelper::hasDebugFlag(context.category);
|
||||||
m_category = CLogMessageHelper::stripFlags(m_category);
|
auto categories = CLogMessageHelper::stripFlags(context.category);
|
||||||
|
m_categories = CLogCategoryList::fromQString(categories);
|
||||||
|
|
||||||
switch(type)
|
switch(type)
|
||||||
{
|
{
|
||||||
@@ -85,7 +86,7 @@ namespace BlackMisc
|
|||||||
*/
|
*/
|
||||||
void CStatusMessage::toQtLogTriple(QtMsgType *o_type, QString *o_category, QString *o_message) const
|
void CStatusMessage::toQtLogTriple(QtMsgType *o_type, QString *o_category, QString *o_message) const
|
||||||
{
|
{
|
||||||
QString category = m_category;
|
auto category = m_categories.toQString();
|
||||||
if (this->m_severity == SeverityDebug && ! category.isEmpty())
|
if (this->m_severity == SeverityDebug && ! category.isEmpty())
|
||||||
{
|
{
|
||||||
category = CLogMessageHelper::addDebugFlag(category);
|
category = CLogMessageHelper::addDebugFlag(category);
|
||||||
@@ -133,7 +134,7 @@ namespace BlackMisc
|
|||||||
{
|
{
|
||||||
|
|
||||||
QString s("Category: ");
|
QString s("Category: ");
|
||||||
s.append(this->m_category);
|
s.append(this->m_categories.toQString());
|
||||||
|
|
||||||
s.append(" Severity: ");
|
s.append(" Severity: ");
|
||||||
s.append(QString::number(this->m_severity));
|
s.append(QString::number(this->m_severity));
|
||||||
@@ -212,7 +213,7 @@ namespace BlackMisc
|
|||||||
return this->m_timestamp.toString("HH:mm::ss.zzz");
|
return this->m_timestamp.toString("HH:mm::ss.zzz");
|
||||||
}
|
}
|
||||||
case IndexCategory:
|
case IndexCategory:
|
||||||
return QVariant(this->m_category);
|
return QVariant(this->m_categories.toQString());
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -245,7 +246,7 @@ namespace BlackMisc
|
|||||||
this->m_severity = static_cast<StatusSeverity>(variant.value<uint>());
|
this->m_severity = static_cast<StatusSeverity>(variant.value<uint>());
|
||||||
break;
|
break;
|
||||||
case IndexCategory:
|
case IndexCategory:
|
||||||
this->m_category = variant.value<QString>();
|
this->m_categories = variant.value<CLogCategoryList>();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
CValueObject::setPropertyByIndex(variant, index);
|
CValueObject::setPropertyByIndex(variant, index);
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
|
|
||||||
#include "icon.h"
|
#include "icon.h"
|
||||||
#include "propertyindex.h"
|
#include "propertyindex.h"
|
||||||
|
#include "logcategorylist.h"
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
|
|
||||||
namespace BlackMisc
|
namespace BlackMisc
|
||||||
@@ -55,7 +56,7 @@ namespace BlackMisc
|
|||||||
CStatusMessage(StatusSeverity severity, const QString &message);
|
CStatusMessage(StatusSeverity severity, const QString &message);
|
||||||
|
|
||||||
//! Constructor
|
//! Constructor
|
||||||
CStatusMessage(const QString &category, StatusSeverity severity, const QString &message);
|
CStatusMessage(const CLogCategoryList &categories, StatusSeverity severity, const QString &message);
|
||||||
|
|
||||||
//! Construct from a Qt logging triple
|
//! Construct from a Qt logging triple
|
||||||
//! \sa QtMessageHandler
|
//! \sa QtMessageHandler
|
||||||
@@ -72,7 +73,7 @@ namespace BlackMisc
|
|||||||
bool operator !=(const CStatusMessage &other) const;
|
bool operator !=(const CStatusMessage &other) const;
|
||||||
|
|
||||||
//! Message category
|
//! Message category
|
||||||
QString getCategory() const { return this->m_category; }
|
const CLogCategoryList &getCategories() const { return this->m_categories; }
|
||||||
|
|
||||||
//! Message severity
|
//! Message severity
|
||||||
StatusSeverity getSeverity() const { return this->m_severity; }
|
StatusSeverity getSeverity() const { return this->m_severity; }
|
||||||
@@ -119,7 +120,7 @@ namespace BlackMisc
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
BLACK_ENABLE_TUPLE_CONVERSION(CStatusMessage)
|
BLACK_ENABLE_TUPLE_CONVERSION(CStatusMessage)
|
||||||
QString m_category;
|
CLogCategoryList m_categories;
|
||||||
StatusSeverity m_severity;
|
StatusSeverity m_severity;
|
||||||
QString m_message;
|
QString m_message;
|
||||||
QDateTime m_timestamp;
|
QDateTime m_timestamp;
|
||||||
@@ -130,7 +131,7 @@ namespace BlackMisc
|
|||||||
|
|
||||||
|
|
||||||
BLACK_DECLARE_TUPLE_CONVERSION(BlackMisc::CStatusMessage, (
|
BLACK_DECLARE_TUPLE_CONVERSION(BlackMisc::CStatusMessage, (
|
||||||
o.m_category,
|
o.m_categories,
|
||||||
o.m_severity,
|
o.m_severity,
|
||||||
o.m_message,
|
o.m_message,
|
||||||
o.m_timestamp,
|
o.m_timestamp,
|
||||||
|
|||||||
@@ -22,9 +22,9 @@ namespace BlackMisc
|
|||||||
/*
|
/*
|
||||||
* Messages by type
|
* Messages by type
|
||||||
*/
|
*/
|
||||||
CStatusMessageList CStatusMessageList::findByCategory(const QString &category) const
|
CStatusMessageList CStatusMessageList::findByCategory(const CLogCategory &category) const
|
||||||
{
|
{
|
||||||
return this->findBy(&CStatusMessage::getCategory, category);
|
return this->findBy([ & ](const CStatusMessage &msg) { return msg.getCategories().contains(category); });
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ namespace BlackMisc
|
|||||||
CStatusMessageList(const CSequence<CStatusMessage> &other);
|
CStatusMessageList(const CSequence<CStatusMessage> &other);
|
||||||
|
|
||||||
//! Find by type
|
//! Find by type
|
||||||
CStatusMessageList findByCategory(const QString &category) const;
|
CStatusMessageList findByCategory(const CLogCategory &category) const;
|
||||||
|
|
||||||
//! Find by severity
|
//! Find by severity
|
||||||
CStatusMessageList findBySeverity(CStatusMessage::StatusSeverity severity) const;
|
CStatusMessageList findBySeverity(CStatusMessage::StatusSeverity severity) const;
|
||||||
|
|||||||
Reference in New Issue
Block a user