qBittorrent
logger.cpp
Go to the documentation of this file.
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2015 sledgehammer999 <hammered999@gmail.com>
4  *
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.
9  *
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.
14  *
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.
18  *
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.
27  */
28 
29 #include "logger.h"
30 
31 #include <algorithm>
32 
33 #include <QDateTime>
34 #include <QVector>
35 
36 namespace
37 {
38  template <typename T>
39  QVector<T> loadFromBuffer(const boost::circular_buffer_space_optimized<T> &src, const int offset = 0)
40  {
41  QVector<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;
45  }
46 }
47 
48 Logger *Logger::m_instance = nullptr;
49 
51  : m_messages(MAX_LOG_MESSAGES)
52  , m_peers(MAX_LOG_MESSAGES)
53 {
54 }
55 
57 {
58  return m_instance;
59 }
60 
62 {
63  if (!m_instance)
64  m_instance = new Logger;
65 }
66 
68 {
69  delete m_instance;
70  m_instance = nullptr;
71 }
72 
73 void Logger::addMessage(const QString &message, const Log::MsgType &type)
74 {
75  QWriteLocker locker(&m_lock);
76  const Log::Msg msg = {m_msgCounter++, type, QDateTime::currentMSecsSinceEpoch(), message};
77  m_messages.push_back(msg);
78  locker.unlock();
79 
80  emit newLogMessage(msg);
81 }
82 
83 void Logger::addPeer(const QString &ip, const bool blocked, const QString &reason)
84 {
85  QWriteLocker locker(&m_lock);
86  const Log::Peer msg = {m_peerCounter++, blocked, QDateTime::currentMSecsSinceEpoch(), ip, reason};
87  m_peers.push_back(msg);
88  locker.unlock();
89 
90  emit newLogPeer(msg);
91 }
92 
93 QVector<Log::Msg> Logger::getMessages(const int lastKnownId) const
94 {
95  const QReadLocker locker(&m_lock);
96 
97  const int diff = m_msgCounter - lastKnownId - 1;
98  const int size = static_cast<int>(m_messages.size());
99 
100  if ((lastKnownId == -1) || (diff >= size))
101  return loadFromBuffer(m_messages);
102 
103  if (diff <= 0)
104  return {};
105 
106  return loadFromBuffer(m_messages, (size - diff));
107 }
108 
109 QVector<Log::Peer> Logger::getPeers(const int lastKnownId) const
110 {
111  const QReadLocker locker(&m_lock);
112 
113  const int diff = m_peerCounter - lastKnownId - 1;
114  const int size = static_cast<int>(m_peers.size());
115 
116  if ((lastKnownId == -1) || (diff >= size))
117  return loadFromBuffer(m_peers);
118 
119  if (diff <= 0)
120  return {};
121 
122  return loadFromBuffer(m_peers, (size - diff));
123 }
124 
125 void LogMsg(const QString &message, const Log::MsgType &type)
126 {
127  Logger::instance()->addMessage(message, type);
128 }
Definition: logger.h:73
static void initInstance()
Definition: logger.cpp:61
boost::circular_buffer_space_optimized< Log::Peer > m_peers
Definition: logger.h:97
int m_msgCounter
Definition: logger.h:99
static Logger * m_instance
Definition: logger.h:95
void addMessage(const QString &message, const Log::MsgType &type=Log::NORMAL)
Definition: logger.cpp:73
QVector< Log::Peer > getPeers(int lastKnownId=-1) const
Definition: logger.cpp:109
QReadWriteLock m_lock
Definition: logger.h:98
void newLogMessage(const Log::Msg &message)
boost::circular_buffer_space_optimized< Log::Msg > m_messages
Definition: logger.h:96
QVector< Log::Msg > getMessages(int lastKnownId=-1) const
Definition: logger.cpp:93
static Logger * instance()
Definition: logger.cpp:56
int m_peerCounter
Definition: logger.h:100
void newLogPeer(const Log::Peer &peer)
Logger()
Definition: logger.cpp:50
static void freeInstance()
Definition: logger.cpp:67
void addPeer(const QString &ip, bool blocked, const QString &reason={})
Definition: logger.cpp:83
flag icons free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to copy
void LogMsg(const QString &message, const Log::MsgType &type)
Definition: logger.cpp:125
const int MAX_LOG_MESSAGES
Definition: logger.h:38
MsgType
Definition: logger.h:43
QVector< T > loadFromBuffer(const boost::circular_buffer_space_optimized< T > &src, const int offset=0)
Definition: logger.cpp:39