qBittorrent
logcontroller.cpp
Go to the documentation of this file.
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2018 Vladimir Golovnev <glassez@yandex.ru>
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 "logcontroller.h"
30 
31 #include <QJsonArray>
32 #include <QJsonObject>
33 #include <QVector>
34 
35 #include "base/global.h"
36 #include "base/logger.h"
37 #include "base/utils/string.h"
38 
39 const char KEY_LOG_ID[] = "id";
40 const char KEY_LOG_TIMESTAMP[] = "timestamp";
41 const char KEY_LOG_MSG_TYPE[] = "type";
42 const char KEY_LOG_MSG_MESSAGE[] = "message";
43 const char KEY_LOG_PEER_IP[] = "ip";
44 const char KEY_LOG_PEER_BLOCKED[] = "blocked";
45 const char KEY_LOG_PEER_REASON[] = "reason";
46 
47 // Returns the log in JSON format.
48 // The return value is an array of dictionaries.
49 // The dictionary keys are:
50 // - "id": id of the message
51 // - "timestamp": milliseconds since epoch
52 // - "type": type of the message (int, see MsgType)
53 // - "message": text of the message
54 // GET params:
55 // - normal (bool): include normal messages (default true)
56 // - info (bool): include info messages (default true)
57 // - warning (bool): include warning messages (default true)
58 // - critical (bool): include critical messages (default true)
59 // - last_known_id (int): exclude messages with id <= 'last_known_id' (default -1)
61 {
63 
64  const bool isNormal = parseBool(params()["normal"]).value_or(true);
65  const bool isInfo = parseBool(params()["info"]).value_or(true);
66  const bool isWarning = parseBool(params()["warning"]).value_or(true);
67  const bool isCritical = parseBool(params()["critical"]).value_or(true);
68 
69  bool ok = false;
70  int lastKnownId = params()["last_known_id"].toInt(&ok);
71  if (!ok)
72  lastKnownId = -1;
73 
74  Logger *const logger = Logger::instance();
75  QJsonArray msgList;
76 
77  for (const Log::Msg &msg : asConst(logger->getMessages(lastKnownId)))
78  {
79  if (!(((msg.type == Log::NORMAL) && isNormal)
80  || ((msg.type == Log::INFO) && isInfo)
81  || ((msg.type == Log::WARNING) && isWarning)
82  || ((msg.type == Log::CRITICAL) && isCritical)))
83  continue;
84 
85  msgList.append(QJsonObject
86  {
87  {QLatin1String(KEY_LOG_ID), msg.id},
88  {QLatin1String(KEY_LOG_TIMESTAMP), msg.timestamp},
89  {QLatin1String(KEY_LOG_MSG_TYPE), msg.type},
90  {QLatin1String(KEY_LOG_MSG_MESSAGE), msg.message}
91  });
92  }
93 
94  setResult(msgList);
95 }
96 
97 // Returns the peer log in JSON format.
98 // The return value is an array of dictionaries.
99 // The dictionary keys are:
100 // - "id": id of the message
101 // - "timestamp": milliseconds since epoch
102 // - "ip": IP of the peer
103 // - "blocked": whether or not the peer was blocked
104 // - "reason": reason of the block
105 // GET params:
106 // - last_known_id (int): exclude messages with id <= 'last_known_id' (default -1)
108 {
109  bool ok = false;
110  int lastKnownId = params()["last_known_id"].toInt(&ok);
111  if (!ok)
112  lastKnownId = -1;
113 
114  Logger *const logger = Logger::instance();
115  QJsonArray peerList;
116 
117  for (const Log::Peer &peer : asConst(logger->getPeers(lastKnownId)))
118  {
119  peerList.append(QJsonObject
120  {
121  {QLatin1String(KEY_LOG_ID), peer.id},
122  {QLatin1String(KEY_LOG_TIMESTAMP), peer.timestamp},
123  {QLatin1String(KEY_LOG_PEER_IP), peer.ip},
124  {QLatin1String(KEY_LOG_PEER_BLOCKED), peer.blocked},
125  {QLatin1String(KEY_LOG_PEER_REASON), peer.reason}
126  });
127  }
128 
129  setResult(peerList);
130 }
const StringMap & params() const
void setResult(const QString &result)
Definition: logger.h:73
QVector< Log::Peer > getPeers(int lastKnownId=-1) const
Definition: logger.cpp:109
QVector< Log::Msg > getMessages(int lastKnownId=-1) const
Definition: logger.cpp:93
static Logger * instance()
Definition: logger.cpp:56
constexpr std::add_const_t< T > & asConst(T &t) noexcept
Definition: global.h:42
const char KEY_LOG_PEER_REASON[]
const char KEY_LOG_TIMESTAMP[]
const char KEY_LOG_MSG_MESSAGE[]
const char KEY_LOG_ID[]
const char KEY_LOG_PEER_BLOCKED[]
const char KEY_LOG_MSG_TYPE[]
const char KEY_LOG_PEER_IP[]
@ WARNING
Definition: logger.h:47
@ NORMAL
Definition: logger.h:45
@ CRITICAL
Definition: logger.h:48
@ INFO
Definition: logger.h:46
std::optional< bool > parseBool(const QString &string)
Definition: string.cpp:72