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'
This commit is contained in:
Gabriele 2014-12-21 14:00:00 +01:00
parent 673b86c6e3
commit 01b73bf704
4 changed files with 115 additions and 0 deletions

View file

@ -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);
}