qBittorrent/src/base/logger.cpp

98 lines
2 KiB
C++
Raw Normal View History

2015-01-04 03:20:59 +03:00
#include "logger.h"
#include <QDateTime>
2017-02-11 09:04:06 +03:00
#include "base/utils/string.h"
2015-01-04 03:20:59 +03:00
2018-04-14 22:53:45 +03:00
Logger *Logger::m_instance = nullptr;
2015-01-04 03:20:59 +03:00
Logger::Logger()
2018-04-14 22:53:45 +03:00
: m_lock(QReadWriteLock::Recursive)
, m_msgCounter(0)
, m_peerCounter(0)
2015-01-04 03:20:59 +03:00
{
}
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 = nullptr;
2015-01-04 03:20:59 +03:00
}
}
void Logger::addMessage(const QString &message, const Log::MsgType &type)
{
2018-04-14 22:53:45 +03:00
QWriteLocker locker(&m_lock);
2015-01-04 03:20:59 +03:00
2018-04-14 22:53:45 +03:00
Log::Msg temp = {m_msgCounter++, QDateTime::currentMSecsSinceEpoch(), type, message.toHtmlEscaped()};
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)
{
2018-04-14 22:53:45 +03:00
QWriteLocker locker(&m_lock);
2015-01-04 03:20:59 +03:00
2018-04-14 22:53:45 +03:00
Log::Peer temp = {m_peerCounter++, QDateTime::currentMSecsSinceEpoch(), ip.toHtmlEscaped(), blocked, reason.toHtmlEscaped()};
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
{
2018-04-14 22:53:45 +03:00
QReadLocker locker(&m_lock);
2015-01-04 03:20:59 +03:00
2018-04-14 22:53:45 +03:00
int diff = m_msgCounter - lastKnownId - 1;
2015-01-04 03:20:59 +03:00
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
{
2018-04-14 22:53:45 +03:00
QReadLocker locker(&m_lock);
2015-01-04 03:20:59 +03:00
2018-04-14 22:53:45 +03:00
int diff = m_peerCounter - lastKnownId - 1;
2015-01-04 03:20:59 +03:00
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);
}
void LogMsg(const QString &message, const Log::MsgType &type)
{
Logger::instance()->addMessage(message, type);
}