Enable customizing the save statistics time interval
[qBittorrent.git] / src / base / logger.cpp
blob9a91632a9ae9b3f6ce768d6005f38bd4bc63c9df
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2015 sledgehammer999 <hammered999@gmail.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
29 #include "logger.h"
31 #include <algorithm>
33 #include <QDateTime>
34 #include <QList>
36 namespace
38 template <typename T>
39 QList<T> loadFromBuffer(const boost::circular_buffer_space_optimized<T> &src, const int offset = 0)
41 QList<T> ret;
42 ret.reserve(static_cast<typename decltype(ret)::size_type>(src.size()) - offset);
43 std::copy((src.begin() + offset), src.end(), std::back_inserter(ret));
44 return ret;
48 Logger *Logger::m_instance = nullptr;
50 Logger::Logger()
51 : m_messages(MAX_LOG_MESSAGES)
52 , m_peers(MAX_LOG_MESSAGES)
56 Logger *Logger::instance()
58 return m_instance;
61 void Logger::initInstance()
63 if (!m_instance)
64 m_instance = new Logger;
67 void Logger::freeInstance()
69 delete m_instance;
70 m_instance = nullptr;
73 void Logger::addMessage(const QString &message, const Log::MsgType &type)
75 QWriteLocker locker(&m_lock);
76 const Log::Msg msg = {m_msgCounter++, type, QDateTime::currentSecsSinceEpoch(), message};
77 m_messages.push_back(msg);
78 locker.unlock();
80 emit newLogMessage(msg);
83 void Logger::addPeer(const QString &ip, const bool blocked, const QString &reason)
85 QWriteLocker locker(&m_lock);
86 const Log::Peer msg = {m_peerCounter++, blocked, QDateTime::currentSecsSinceEpoch(), ip, reason};
87 m_peers.push_back(msg);
88 locker.unlock();
90 emit newLogPeer(msg);
93 QList<Log::Msg> Logger::getMessages(const int lastKnownId) const
95 const QReadLocker locker(&m_lock);
97 const int diff = m_msgCounter - lastKnownId - 1;
98 const int size = static_cast<int>(m_messages.size());
100 if ((lastKnownId == -1) || (diff >= size))
101 return loadFromBuffer(m_messages);
103 if (diff <= 0)
104 return {};
106 return loadFromBuffer(m_messages, (size - diff));
109 QList<Log::Peer> Logger::getPeers(const int lastKnownId) const
111 const QReadLocker locker(&m_lock);
113 const int diff = m_peerCounter - lastKnownId - 1;
114 const int size = static_cast<int>(m_peers.size());
116 if ((lastKnownId == -1) || (diff >= size))
117 return loadFromBuffer(m_peers);
119 if (diff <= 0)
120 return {};
122 return loadFromBuffer(m_peers, (size - diff));
125 void LogMsg(const QString &message, const Log::MsgType &type)
127 Logger::instance()->addMessage(message, type);