2015-01-04 03:20:59 +03:00
|
|
|
#include "logger.h"
|
|
|
|
|
|
|
|
#include <QDateTime>
|
|
|
|
|
|
|
|
Logger* Logger::m_instance = 0;
|
|
|
|
|
|
|
|
Logger::Logger()
|
|
|
|
: lock(QReadWriteLock::Recursive)
|
|
|
|
, msgCounter(0)
|
|
|
|
, peerCounter(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger::~Logger() {}
|
|
|
|
|
2015-04-19 18:17:47 +03:00
|
|
|
Logger *Logger::instance()
|
|
|
|
{
|
|
|
|
return m_instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Logger::initInstance()
|
2015-01-04 03:20:59 +03:00
|
|
|
{
|
|
|
|
if (!m_instance)
|
|
|
|
m_instance = new Logger;
|
|
|
|
}
|
|
|
|
|
2015-04-19 18:17:47 +03:00
|
|
|
void Logger::freeInstance()
|
2015-01-04 03:20:59 +03:00
|
|
|
{
|
|
|
|
if (m_instance) {
|
|
|
|
delete m_instance;
|
|
|
|
m_instance = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Logger::addMessage(const QString &message, const Log::MsgType &type)
|
|
|
|
{
|
|
|
|
QWriteLocker locker(&lock);
|
|
|
|
|
2016-01-15 08:50:36 +03:00
|
|
|
Log::Msg temp = { msgCounter++, QDateTime::currentMSecsSinceEpoch(), type, message };
|
2015-01-04 03:20:59 +03:00
|
|
|
m_messages.push_back(temp);
|
|
|
|
|
|
|
|
if (m_messages.size() >= MAX_LOG_MESSAGES)
|
|
|
|
m_messages.pop_front();
|
|
|
|
|
|
|
|
emit newLogMessage(temp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Logger::addPeer(const QString &ip, bool blocked, const QString &reason)
|
|
|
|
{
|
|
|
|
QWriteLocker locker(&lock);
|
|
|
|
|
2016-01-15 08:50:36 +03:00
|
|
|
Log::Peer temp = { peerCounter++, QDateTime::currentMSecsSinceEpoch(), ip, blocked, reason };
|
2015-01-04 03:20:59 +03:00
|
|
|
m_peers.push_back(temp);
|
|
|
|
|
|
|
|
if (m_peers.size() >= MAX_LOG_MESSAGES)
|
|
|
|
m_peers.pop_front();
|
|
|
|
|
|
|
|
emit newLogPeer(temp);
|
|
|
|
}
|
|
|
|
|
|
|
|
QVector<Log::Msg> Logger::getMessages(int lastKnownId) const
|
|
|
|
{
|
|
|
|
QReadLocker locker(&lock);
|
|
|
|
|
|
|
|
int diff = msgCounter - lastKnownId - 1;
|
|
|
|
int size = m_messages.size();
|
|
|
|
|
|
|
|
if ((lastKnownId == -1) || (diff >= size))
|
|
|
|
return m_messages;
|
|
|
|
|
|
|
|
if (diff <= 0)
|
|
|
|
return QVector<Log::Msg>();
|
|
|
|
|
|
|
|
return m_messages.mid(size - diff);
|
|
|
|
}
|
|
|
|
|
|
|
|
QVector<Log::Peer> Logger::getPeers(int lastKnownId) const
|
|
|
|
{
|
|
|
|
QReadLocker locker(&lock);
|
|
|
|
|
|
|
|
int diff = peerCounter - lastKnownId - 1;
|
|
|
|
int size = m_peers.size();
|
|
|
|
|
|
|
|
if ((lastKnownId == -1) || (diff >= size))
|
|
|
|
return m_peers;
|
|
|
|
|
|
|
|
if (diff <= 0)
|
|
|
|
return QVector<Log::Peer>();
|
|
|
|
|
|
|
|
return m_peers.mid(size - diff);
|
|
|
|
}
|