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.
39 QVector
<T
> loadFromBuffer(const boost::circular_buffer_space_optimized
<T
> &src
, const int offset
= 0)
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
));
48 Logger
*Logger::m_instance
= nullptr;
51 : m_messages(MAX_LOG_MESSAGES
)
52 , m_peers(MAX_LOG_MESSAGES
)
56 Logger
*Logger::instance()
61 void Logger::initInstance()
64 m_instance
= new Logger
;
67 void Logger::freeInstance()
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::currentMSecsSinceEpoch(), message
};
77 m_messages
.push_back(msg
);
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::currentMSecsSinceEpoch(), ip
, reason
};
87 m_peers
.push_back(msg
);
93 QVector
<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
);
106 return loadFromBuffer(m_messages
, (size
- diff
));
109 QVector
<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
);
122 return loadFromBuffer(m_peers
, (size
- diff
));
125 void LogMsg(const QString
&message
, const Log::MsgType
&type
)
127 Logger::instance()->addMessage(message
, type
);