Issue #15 Classes for sharing the history of log messages

This commit is contained in:
Mat Sutcliffe
2020-04-15 18:18:43 +01:00
parent 151810d6fc
commit 7382564633
7 changed files with 144 additions and 1 deletions

View File

@@ -0,0 +1,41 @@
/* Copyright (C) 2020
* 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. 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.
*/
//! \file
#include "blackmisc/loghistory.h"
#include "blackmisc/loghandler.h"
namespace BlackMisc
{
CLogHistory::CLogHistory(QObject *parent) : CListJournal(parent)
{
}
CLogHistorySource::CLogHistorySource(QObject *parent) : CListMutator(parent)
{
connect(CLogHandler::instance(), &CLogHandler::localMessageLogged, this, [this](auto&&... args)
{
this->addElement(args...);
});
}
CLogHistoryReplica::CLogHistoryReplica(QObject* parent) : CListObserver(parent)
{
}
void CLogHistoryReplica::onElementAdded(const CStatusMessage &msg)
{
emit elementAdded(msg);
}
void CLogHistoryReplica::onElementsReplaced(const CStatusMessageList &msgs)
{
emit elementsReplaced(msgs);
}
}

View File

@@ -0,0 +1,75 @@
/* Copyright (C) 2020
* 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. 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.
*/
//! \file
#ifndef BLACKMISC_LOGHISTORY_H
#define BLACKMISC_LOGHISTORY_H
#include "blackmisc/sharedstate/datalink.h"
#include "blackmisc/sharedstate/listjournal.h"
#include "blackmisc/sharedstate/listmutator.h"
#include "blackmisc/sharedstate/listobserver.h"
#include "blackmisc/statusmessagelist.h"
#include "blackmisc/logpattern.h"
#include <QObject>
namespace BlackMisc
{
/*!
* Records all log messages to a list that persists for the lifetime of the application.
*/
class BLACKMISC_EXPORT CLogHistory : public SharedState::CListJournal<CStatusMessageList>
{
Q_OBJECT
BLACK_SHARED_STATE_CHANNEL("swift.log.history")
public:
//! Constructor.
CLogHistory(QObject *parent = nullptr);
};
/*!
* Allows distributed insertion of log messages into a central CLogHistory.
*/
class BLACKMISC_EXPORT CLogHistorySource : public SharedState::CListMutator<CStatusMessageList>
{
Q_OBJECT
BLACK_SHARED_STATE_CHANNEL("swift.log.history")
public:
//! Constructor.
CLogHistorySource(QObject *parent = nullptr);
};
/*!
* Allows distributed access to the log messages of a central CLogHistory.
*/
class BLACKMISC_EXPORT CLogHistoryReplica : public SharedState::CListObserver<CStatusMessageList, CLogPattern>
{
Q_OBJECT
BLACK_SHARED_STATE_CHANNEL("swift.log.history")
public:
//! Constructor.
CLogHistoryReplica(QObject *parent = nullptr);
signals:
//! Signal emitted for each new log message.
void elementAdded(const BlackMisc::CStatusMessage &msg);
//! Signal emitted when the whole history is updated wholesale.
void elementsReplaced(const BlackMisc::CStatusMessageList &msgs);
private:
virtual void onElementAdded(const CStatusMessage &msg) override final;
virtual void onElementsReplaced(const CStatusMessageList &msgs) override final;
};
}
#endif

View File

@@ -328,7 +328,7 @@ namespace BlackMisc
QString categories = m_strings.values().join("|"); // clazy:exclude=container-anti-pattern
switch (m_strategy)
{
case Everything: strategy = "none"; break;
case Everything: strategy = "all"; break;
case ExactMatch: strategy = "exact match:" + categories; break;
case AnyOf: strategy = "any of:" + categories; break;
case AllOf: strategy = "all of:" + categories; break;

View File

@@ -93,6 +93,9 @@ namespace BlackMisc
//! Returns true if the given message matches this pattern.
bool match(const CStatusMessage &message) const;
//! This class acts as a SharedState filter when stored in a CVariant.
bool matches(const CVariant &message) const { return match(message.to<CStatusMessage>()); }
//! Returns true if this pattern is a proper subset of the other pattern.
//! \see https://en.wikipedia.org/wiki/Proper_subset
//! \details Pattern A is a proper subset of pattern B iff pattern B would match every category which pattern A matches,