From 01b73bf704086e458df670f4f049caf45f9e61fc Mon Sep 17 00:00:00 2001 From: Gabriele Date: Sun, 21 Dec 2014 14:00:00 +0100 Subject: [PATCH] WebUI: Add command to get the logs Add /query/getLog and /query/getPeerLog to respectively retrieve the main log and the peer log. GET /query/getLog Params: - normal (bool): include normal messages (default true) - info (bool): include info messages (default true) - warning (bool): include warning messages (default true) - critical (bool): include critical messages (default true) - last_known_id (int): exclude messages with id <= 'last_known_id' GET /query/getPeerLog Params: - last_known_id (int): exclude messages with id <= 'last_known_id' --- src/webui/btjson.cpp | 71 ++++++++++++++++++++++++++++++++++++ src/webui/btjson.h | 2 + src/webui/webapplication.cpp | 40 ++++++++++++++++++++ src/webui/webapplication.h | 2 + 4 files changed, 115 insertions(+) diff --git a/src/webui/btjson.cpp b/src/webui/btjson.cpp index a6da20965..495c208ff 100644 --- a/src/webui/btjson.cpp +++ b/src/webui/btjson.cpp @@ -29,6 +29,7 @@ */ #include "btjson.h" +#include "base/logger.h" #include "base/utils/misc.h" #include "base/utils/fs.h" #include "base/preferences.h" @@ -198,6 +199,15 @@ static const char KEY_FULL_UPDATE[] = "full_update"; static const char KEY_RESPONSE_ID[] = "rid"; static const char KEY_SUFFIX_REMOVED[] = "_removed"; +// Log keys +static const char KEY_LOG_ID[] = "id"; +static const char KEY_LOG_TIMESTAMP[] = "timestamp"; +static const char KEY_LOG_MSG_TYPE[] = "type"; +static const char KEY_LOG_MSG_MESSAGE[] = "message"; +static const char KEY_LOG_PEER_IP[] = "ip"; +static const char KEY_LOG_PEER_BLOCKED[] = "blocked"; +static const char KEY_LOG_PEER_REASON[] = "reason"; + QVariantMap getTranserInfoMap(); QVariantMap toMap(BitTorrent::TorrentHandle *const torrent); void processMap(QVariantMap prevData, QVariantMap data, QVariantMap &syncData); @@ -887,3 +897,64 @@ QVariantMap generateSyncData(int acceptedResponseId, QVariantMap data, QVariantM return syncData; } + +/** + * Returns the log in JSON format. + * + * The return value is an array of dictionaries. + * The dictionary keys are: + * - "id": id of the message + * - "timestamp": milliseconds since epoch + * - "type": type of the message (int, see MsgType) + * - "message": text of the message + */ +QByteArray btjson::getLog(bool normal, bool info, bool warning, bool critical, int lastKnownId) +{ + Logger* const logger = Logger::instance(); + QVariantList msgList; + + foreach (const Log::Msg& msg, logger->getMessages(lastKnownId)) { + if (!((msg.type == Log::NORMAL && normal) + || (msg.type == Log::INFO && info) + || (msg.type == Log::WARNING && warning) + || (msg.type == Log::CRITICAL && critical))) + continue; + QVariantMap map; + map[KEY_LOG_ID] = msg.id; + map[KEY_LOG_TIMESTAMP] = msg.timestamp; + map[KEY_LOG_MSG_TYPE] = msg.type; + map[KEY_LOG_MSG_MESSAGE] = msg.message; + msgList.append(map); + } + + return json::toJson(msgList); +} + +/** + * Returns the peer log in JSON format. + * + * The return value is an array of dictionaries. + * The dictionary keys are: + * - "id": id of the message + * - "timestamp": milliseconds since epoch + * - "ip": IP of the peer + * - "blocked": whether or not the peer was blocked + * - "reason": reason of the block + */ +QByteArray btjson::getPeerLog(int lastKnownId) +{ + Logger* const logger = Logger::instance(); + QVariantList peerList; + + foreach (const Log::Peer& peer, logger->getPeers(lastKnownId)) { + QVariantMap map; + map[KEY_LOG_ID] = peer.id; + map[KEY_LOG_TIMESTAMP] = peer.timestamp; + map[KEY_LOG_PEER_IP] = peer.ip; + map[KEY_LOG_PEER_BLOCKED] = peer.blocked; + map[KEY_LOG_PEER_REASON] = peer.reason; + peerList.append(map); + } + + return json::toJson(peerList); +} diff --git a/src/webui/btjson.h b/src/webui/btjson.h index f58e8d032..42c0d379d 100644 --- a/src/webui/btjson.h +++ b/src/webui/btjson.h @@ -53,6 +53,8 @@ public: static QByteArray getFilesForTorrent(const QString& hash); static QByteArray getTransferInfo(); static QByteArray getTorrentsRatesLimits(QStringList& hashes, bool downloadLimits); + static QByteArray getLog(bool normal, bool info, bool warning, bool critical, int lastKnownId); + static QByteArray getPeerLog(int lastKnownId); }; // class btjson #endif // BTJSON_H diff --git a/src/webui/webapplication.cpp b/src/webui/webapplication.cpp index 41d272977..95064854a 100644 --- a/src/webui/webapplication.cpp +++ b/src/webui/webapplication.cpp @@ -81,6 +81,8 @@ QMap > WebApplication::initialize ADD_ACTION(query, propertiesTrackers); ADD_ACTION(query, propertiesWebSeeds); ADD_ACTION(query, propertiesFiles); + ADD_ACTION(query, getLog); + ADD_ACTION(query, getPeerLog); ADD_ACTION(sync, maindata); ADD_ACTION(sync, torrent_peers); ADD_ACTION(command, shutdown); @@ -270,6 +272,44 @@ void WebApplication::action_query_propertiesFiles() print(btjson::getFilesForTorrent(args_.front()), Http::CONTENT_TYPE_JSON); } +// GET params: +// - normal (bool): include normal messages (default true) +// - info (bool): include info messages (default true) +// - warning (bool): include warning messages (default true) +// - critical (bool): include critical messages (default true) +// - last_known_id (int): exclude messages with id <= 'last_known_id' (default -1) +void WebApplication::action_query_getLog() +{ + CHECK_URI(0); + bool normal = request().gets["normal"] != "false"; + bool info = request().gets["info"] != "false"; + bool warning = request().gets["warning"] != "false"; + bool critical = request().gets["critical"] != "false"; + int lastKnownId; + bool ok; + + lastKnownId = request().gets["last_known_id"].toInt(&ok); + if (!ok) + lastKnownId = -1; + + print(btjson::getLog(normal, info, warning, critical, lastKnownId), Http::CONTENT_TYPE_JSON); +} + +// GET params: +// - last_known_id (int): exclude messages with id <= 'last_known_id' (default -1) +void WebApplication::action_query_getPeerLog() +{ + CHECK_URI(0); + int lastKnownId; + bool ok; + + lastKnownId = request().gets["last_known_id"].toInt(&ok); + if (!ok) + lastKnownId = -1; + + print(btjson::getPeerLog(lastKnownId), Http::CONTENT_TYPE_JSON); +} + // GET param: // - rid (int): last response id void WebApplication::action_sync_maindata() diff --git a/src/webui/webapplication.h b/src/webui/webapplication.h index 722f7d5e4..11fefcae9 100644 --- a/src/webui/webapplication.h +++ b/src/webui/webapplication.h @@ -54,6 +54,8 @@ private: void action_query_propertiesTrackers(); void action_query_propertiesWebSeeds(); void action_query_propertiesFiles(); + void action_query_getLog(); + void action_query_getPeerLog(); void action_sync_maindata(); void action_sync_torrent_peers(); void action_command_shutdown();