mirror of
https://github.com/savoirfairelinux/opendht.git
synced 2026-11-08 04:21:51 +00:00
build: make logs optional at build time
This commit is contained in:
1 parent
f233340cb2
commit
96da4cd85d
5 files changed
+166
-118
No files matched your search
+29
-28
@@ -21,6 +21,7 @@
|
||||
#include "network_engine.h"
|
||||
#include "request.h"
|
||||
#include "default_types.h"
|
||||
#include "log_enable.h"
|
||||
|
||||
#include <msgpack.hpp>
|
||||
|
||||
@@ -161,7 +162,7 @@ NetworkEngine::tellListener(std::shared_ptr<Node> node, uint16_t rid, const Info
|
||||
sendNodesValues(node->addr, TransId {TransPrefix::GET_VALUES, (uint16_t)rid}, nnodes.first, nnodes.second,
|
||||
values, query, ntoken);
|
||||
} catch (const std::overflow_error& e) {
|
||||
DHT_LOG.ERR("Can't send value: buffer not large enough !");
|
||||
DHT_LOG_ERR("Can't send value: buffer not large enough !");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -216,7 +217,7 @@ NetworkEngine::requestStep(std::shared_ptr<Request> sreq)
|
||||
auto now = scheduler.time();
|
||||
auto& node = *req.node;
|
||||
if (req.isExpired(now)) {
|
||||
DHT_LOG.ERR("[node %s] expired !", node.toString().c_str());
|
||||
DHT_LOG_ERR("[node %s] expired !", node.toString().c_str());
|
||||
node.setExpired();
|
||||
requests.erase(req.tid);
|
||||
return;
|
||||
@@ -246,7 +247,7 @@ NetworkEngine::sendRequest(std::shared_ptr<Request>& request)
|
||||
request->start = scheduler.time();
|
||||
auto e = requests.emplace(request->tid, request);
|
||||
if (!e.second)
|
||||
DHT_LOG.ERR("Request already existed (tid: %d)!", request->tid);
|
||||
DHT_LOG_ERR("Request already existed (tid: %d)!", request->tid);
|
||||
request->node->requested(request);
|
||||
requestStep(request);
|
||||
}
|
||||
@@ -335,12 +336,12 @@ void
|
||||
NetworkEngine::processMessage(const uint8_t *buf, size_t buflen, const SockAddr& from)
|
||||
{
|
||||
if (isMartian(from)) {
|
||||
DHT_LOG.WARN("Received packet from martian node %s", from.toString().c_str());
|
||||
DHT_LOG_WARN("Received packet from martian node %s", from.toString().c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
if (isNodeBlacklisted(from)) {
|
||||
DHT_LOG.WARN("Received packet from blacklisted node %s", from.toString().c_str());
|
||||
DHT_LOG_WARN("Received packet from blacklisted node %s", from.toString().c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -349,13 +350,13 @@ NetworkEngine::processMessage(const uint8_t *buf, size_t buflen, const SockAddr&
|
||||
msgpack::unpacked msg_res = msgpack::unpack((const char*)buf, buflen);
|
||||
msg->msgpack_unpack(msg_res.get());
|
||||
} catch (const std::exception& e) {
|
||||
DHT_LOG.WARN("Can't process message of size %lu: %s.", buflen, e.what());
|
||||
DHT_LOG.DEBUG.logPrintable(buf, buflen);
|
||||
DHT_LOG_WARN("Can't process message of size %lu: %s.", buflen, e.what());
|
||||
DHT_LOG_DEBUG.logPrintable(buf, buflen);
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg->network != network) {
|
||||
DHT_LOG.DEBUG("Received message from other network %u.", msg->network);
|
||||
DHT_LOG_DEBUG("Received message from other network %u.", msg->network);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -365,12 +366,12 @@ NetworkEngine::processMessage(const uint8_t *buf, size_t buflen, const SockAddr&
|
||||
if (msg->type == MessageType::ValueData) {
|
||||
auto pmsg_it = partial_messages.find(msg->tid);
|
||||
if (pmsg_it == partial_messages.end()) {
|
||||
DHT_LOG.DEBUG("Can't find partial message");
|
||||
DHT_LOG_DEBUG("Can't find partial message");
|
||||
rateLimit(from);
|
||||
return;
|
||||
}
|
||||
if (!pmsg_it->second.from.equals(from)) {
|
||||
DHT_LOG.DEBUG("Received partial message data from unexpected IP address");
|
||||
DHT_LOG_DEBUG("Received partial message data from unexpected IP address");
|
||||
rateLimit(from);
|
||||
return;
|
||||
}
|
||||
@@ -389,14 +390,14 @@ NetworkEngine::processMessage(const uint8_t *buf, size_t buflen, const SockAddr&
|
||||
}
|
||||
|
||||
if (msg->id == myid || msg->id == zeroes) {
|
||||
DHT_LOG.DEBUG("Received message from self.");
|
||||
DHT_LOG_DEBUG("Received message from self.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg->type > MessageType::Reply) {
|
||||
/* Rate limit requests. */
|
||||
if (!rateLimit(from)) {
|
||||
DHT_LOG.WARN("Dropping request due to rate limiting.");
|
||||
DHT_LOG_WARN("Dropping request due to rate limiting.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -415,7 +416,7 @@ NetworkEngine::processMessage(const uint8_t *buf, size_t buflen, const SockAddr&
|
||||
scheduler.add(now + RX_MAX_PACKET_TIME, std::bind(&NetworkEngine::maintainRxBuffer, this, wmsg.first->first));
|
||||
scheduler.add(now + RX_TIMEOUT, std::bind(&NetworkEngine::maintainRxBuffer, this, wmsg.first->first));
|
||||
} else
|
||||
DHT_LOG.ERR("Partial message with given TID already exists.");
|
||||
DHT_LOG_ERR("Partial message with given TID already exists.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -441,7 +442,7 @@ NetworkEngine::process(std::unique_ptr<ParsedMessage>&& msg, const SockAddr& fro
|
||||
// received reply from unexpected node
|
||||
node->received(now, req);
|
||||
onNewNode(node, 2);
|
||||
DHT_LOG.WARN("[node %s] Message received from unexpected node.", node->toString().c_str());
|
||||
DHT_LOG_WARN("[node %s] Message received from unexpected node.", node->toString().c_str());
|
||||
return;
|
||||
}
|
||||
} else
|
||||
@@ -452,7 +453,7 @@ NetworkEngine::process(std::unique_ptr<ParsedMessage>&& msg, const SockAddr& fro
|
||||
onReportedAddr(msg->id, msg->addr);
|
||||
|
||||
if (req->cancelled() or req->expired() or (req->completed() and not req->persistent)) {
|
||||
DHT_LOG.WARN("[node %s] response to expired, cancelled or completed request", node->toString().c_str());
|
||||
DHT_LOG_WARN("[node %s] response to expired, cancelled or completed request", node->toString().c_str());
|
||||
requests.erase(reqp);
|
||||
return;
|
||||
}
|
||||
@@ -468,7 +469,7 @@ NetworkEngine::process(std::unique_ptr<ParsedMessage>&& msg, const SockAddr& fro
|
||||
req->reply_time = TIME_INVALID;
|
||||
onError(req, DhtProtocolException {DhtProtocolException::UNAUTHORIZED});
|
||||
} else {
|
||||
DHT_LOG.WARN("[node %s %s] received unknown error message %u",
|
||||
DHT_LOG_WARN("[node %s %s] received unknown error message %u",
|
||||
msg->id.toString().c_str(), from.toString().c_str(), msg->error_code);
|
||||
}
|
||||
break;
|
||||
@@ -496,12 +497,12 @@ NetworkEngine::process(std::unique_ptr<ParsedMessage>&& msg, const SockAddr& fro
|
||||
switch (msg->type) {
|
||||
case MessageType::Ping:
|
||||
++in_stats.ping;
|
||||
DHT_LOG.DEBUG("Sending pong.");
|
||||
DHT_LOG_DEBUG("Sending pong.");
|
||||
onPing(node);
|
||||
sendPong(from, msg->tid);
|
||||
break;
|
||||
case MessageType::FindNode: {
|
||||
DHT_LOG.DEBUG("[node %s] got 'find' request (%d).", node->toString().c_str(), msg->want);
|
||||
DHT_LOG_DEBUG("[node %s] got 'find' request (%d).", node->toString().c_str(), msg->want);
|
||||
++in_stats.find;
|
||||
RequestAnswer answer = onFindNode(node, msg->target, msg->want);
|
||||
auto nnodes = bufferNodes(from.getFamily(), msg->target, msg->want, answer.nodes4, answer.nodes6);
|
||||
@@ -509,7 +510,7 @@ NetworkEngine::process(std::unique_ptr<ParsedMessage>&& msg, const SockAddr& fro
|
||||
break;
|
||||
}
|
||||
case MessageType::GetValues: {
|
||||
DHT_LOG.DEBUG("[node %s] got 'get' request for %s.", node->toString().c_str(), msg->info_hash.toString().c_str());
|
||||
DHT_LOG_DEBUG("[node %s] got 'get' request for %s.", node->toString().c_str(), msg->info_hash.toString().c_str());
|
||||
++in_stats.get;
|
||||
RequestAnswer answer = onGetValues(node, msg->info_hash, msg->want, msg->query);
|
||||
auto nnodes = bufferNodes(from.getFamily(), msg->info_hash, msg->want, answer.nodes4, answer.nodes6);
|
||||
@@ -517,7 +518,7 @@ NetworkEngine::process(std::unique_ptr<ParsedMessage>&& msg, const SockAddr& fro
|
||||
break;
|
||||
}
|
||||
case MessageType::AnnounceValue: {
|
||||
DHT_LOG.DEBUG("[node %s] got 'put' request for %s.", node->toString().c_str(), msg->info_hash.toString().c_str());
|
||||
DHT_LOG_DEBUG("[node %s] got 'put' request for %s.", node->toString().c_str(), msg->info_hash.toString().c_str());
|
||||
++in_stats.put;
|
||||
onAnnounce(node, msg->info_hash, msg->token, msg->values, msg->created);
|
||||
|
||||
@@ -530,7 +531,7 @@ NetworkEngine::process(std::unique_ptr<ParsedMessage>&& msg, const SockAddr& fro
|
||||
break;
|
||||
}
|
||||
case MessageType::Listen: {
|
||||
DHT_LOG.DEBUG("[node %s] got 'listen' request for %s.", node->toString().c_str(), msg->info_hash.toString().c_str());
|
||||
DHT_LOG_DEBUG("[node %s] got 'listen' request for %s.", node->toString().c_str(), msg->info_hash.toString().c_str());
|
||||
++in_stats.listen;
|
||||
RequestAnswer answer = onListen(node, msg->info_hash, msg->token, msg->tid.getTid(), std::move(msg->query));
|
||||
sendListenConfirmation(from, msg->tid);
|
||||
@@ -540,7 +541,7 @@ NetworkEngine::process(std::unique_ptr<ParsedMessage>&& msg, const SockAddr& fro
|
||||
break;
|
||||
}
|
||||
} catch (const std::overflow_error& e) {
|
||||
DHT_LOG.ERR("Can't send value: buffer not large enough !");
|
||||
DHT_LOG_ERR("Can't send value: buffer not large enough !");
|
||||
} catch (DhtProtocolException& e) {
|
||||
sendError(from, msg->tid, e.getCode(), e.getMsg().c_str(), true);
|
||||
}
|
||||
@@ -607,7 +608,7 @@ NetworkEngine::sendPing(std::shared_ptr<Node> node, RequestCb on_done, RequestEx
|
||||
Blob b {buffer.data(), buffer.data() + buffer.size()};
|
||||
std::shared_ptr<Request> req(new Request {tid.getTid(), node, std::move(b),
|
||||
[=](const Request& req_status, ParsedMessage&&) {
|
||||
DHT_LOG.DEBUG("[node %s] Got pong !", req_status.node->toString().c_str());
|
||||
DHT_LOG_DEBUG("[node %s] Got pong !", req_status.node->toString().c_str());
|
||||
if (on_done) {
|
||||
on_done(req_status, {});
|
||||
}
|
||||
@@ -796,7 +797,7 @@ NetworkEngine::packValueHeader(msgpack::sbuffer& buffer, const std::vector<std::
|
||||
if (svals.size() < 50 && total_size < MAX_PACKET_VALUE_SIZE) {
|
||||
for (const auto& b : svals)
|
||||
buffer.write((const char*)b.data(), b.size());
|
||||
DHT_LOG.DEBUG("sending %lu bytes of values", total_size);
|
||||
DHT_LOG_DEBUG("sending %lu bytes of values", total_size);
|
||||
svals.clear();
|
||||
} else {
|
||||
for (const auto& b : svals)
|
||||
@@ -871,7 +872,7 @@ NetworkEngine::sendNodesValues(const SockAddr& addr, TransId tid, const Blob& no
|
||||
pk.pack(std::string("v")); pk.pack_array(st.size()*fields.size());
|
||||
for (const auto& v : st)
|
||||
v->msgpack_pack_fields(fields, pk);
|
||||
//DHT_LOG.DEBUG("sending closest nodes (%d+%d nodes.), %u value headers containing %u fields",
|
||||
//DHT_LOG_DEBUG("sending closest nodes (%d+%d nodes.), %u value headers containing %u fields",
|
||||
// nodes.size(), nodes6.size(), st.size(), fields.size());
|
||||
}
|
||||
}
|
||||
@@ -1039,7 +1040,7 @@ NetworkEngine::sendAnnounceValue(std::shared_ptr<Node> n, const InfoHash& infoha
|
||||
std::shared_ptr<Request> req(new Request {tid.getTid(), n, std::move(b),
|
||||
[=](const Request& req_status, ParsedMessage&& msg) { /* on done */
|
||||
if (msg.value_id == Value::INVALID_ID) {
|
||||
DHT_LOG.DEBUG("Unknown search or announce!");
|
||||
DHT_LOG_DEBUG("Unknown search or announce!");
|
||||
} else {
|
||||
if (on_done) {
|
||||
RequestAnswer answer {};
|
||||
@@ -1246,7 +1247,7 @@ ParsedMessage::msgpack_unpack(msgpack::object msg)
|
||||
try {
|
||||
values.emplace_back(std::make_shared<Value>(rvalues->via.array.ptr[i]));
|
||||
} catch (const std::exception& e) {
|
||||
//DHT_LOG.WARN("Error reading value: %s", e.what());
|
||||
//DHT_LOG_WARN("Error reading value: %s", e.what());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1296,7 +1297,7 @@ NetworkEngine::maintainRxBuffer(const TransId& tid)
|
||||
if (msg != partial_messages.end()) {
|
||||
if (msg->second.start + RX_MAX_PACKET_TIME < now
|
||||
|| msg->second.last_part + RX_TIMEOUT < now) {
|
||||
DHT_LOG.WARN("Dropping expired partial message from %s", msg->second.from.toString().c_str());
|
||||
DHT_LOG_WARN("Dropping expired partial message from %s", msg->second.from.toString().c_str());
|
||||
partial_messages.erase(msg);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user