From c34a2541ccdc76f371a4e8b6a2aad4853d2523e4 Mon Sep 17 00:00:00 2001 From: Pavel Pimenov Date: Sat, 24 Dec 2016 17:46:21 +0300 Subject: [PATCH] Log refactoring (#1446) factor out common logging code in some dht traversal algorithms --- include/libtorrent/kademlia/get_peers.hpp | 4 ++ include/libtorrent/kademlia/routing_table.hpp | 1 + .../kademlia/traversal_algorithm.hpp | 3 ++ src/kademlia/get_peers.cpp | 39 +++++++------------ src/kademlia/routing_table.cpp | 32 +++++++-------- src/kademlia/traversal_algorithm.cpp | 39 +++++++++---------- 6 files changed, 57 insertions(+), 61 deletions(-) diff --git a/include/libtorrent/kademlia/get_peers.hpp b/include/libtorrent/kademlia/get_peers.hpp index c628fcd04..51e8f094a 100644 --- a/include/libtorrent/kademlia/get_peers.hpp +++ b/include/libtorrent/kademlia/get_peers.hpp @@ -92,6 +92,10 @@ struct get_peers_observer : find_data_observer {} virtual void reply(msg const&); +#ifndef TORRENT_DISABLE_LOGGING +private: + void log_peers(msg const& m, bdecode_node const& r, int const size) const; +#endif }; struct obfuscated_get_peers_observer : traversal_observer diff --git a/include/libtorrent/kademlia/routing_table.hpp b/include/libtorrent/kademlia/routing_table.hpp index 93c5ce543..70781d4f4 100644 --- a/include/libtorrent/kademlia/routing_table.hpp +++ b/include/libtorrent/kademlia/routing_table.hpp @@ -277,6 +277,7 @@ private: #ifndef TORRENT_DISABLE_LOGGING dht_logger* m_log; + void log_node_failed(node_id const& nid, node_entry const& ne) const; #endif table_t::iterator find_bucket(node_id const& id); diff --git a/include/libtorrent/kademlia/traversal_algorithm.hpp b/include/libtorrent/kademlia/traversal_algorithm.hpp index 4ab0117eb..f2c85fa99 100644 --- a/include/libtorrent/kademlia/traversal_algorithm.hpp +++ b/include/libtorrent/kademlia/traversal_algorithm.hpp @@ -116,6 +116,9 @@ private: #if TORRENT_USE_IPV6 std::set m_peer6_prefixes; #endif +#ifndef TORRENT_DISABLE_LOGGING + void log_timeout(observer_ptr const& o, char const* prefix) const; +#endif }; struct traversal_observer : observer diff --git a/src/kademlia/get_peers.cpp b/src/kademlia/get_peers.cpp index 1dd0cacbb..d75300672 100644 --- a/src/kademlia/get_peers.cpp +++ b/src/kademlia/get_peers.cpp @@ -69,23 +69,7 @@ void get_peers_observer::reply(msg const& m) char const* end = peers + n.list_at(0).string_length(); #ifndef TORRENT_DISABLE_LOGGING - auto logger = get_observer(); - if (logger != nullptr && logger->should_log(dht_logger::traversal)) - { - bdecode_node const id = r.dict_find_string("id"); - if (id && id.string_length() == 20) - { - logger->log(dht_logger::traversal, "[%p] PEERS " - "invoke-count: %d branch-factor: %d addr: %s id: %s distance: %d p: %d" - , static_cast(algorithm()) - , algorithm()->invoke_count() - , algorithm()->branch_factor() - , print_endpoint(m.addr).c_str() - , aux::to_hex({id.string_ptr(), size_t(id.string_length())}).c_str() - , distance_exp(algorithm()->target(), node_id(id.string_ptr())) - , int((end - peers) / 6)); - } - } + log_peers(m, r, int((end - peers) / 6)); #endif while (end - peers >= 6) peer_list.push_back(detail::read_v4_endpoint(peers)); @@ -95,6 +79,17 @@ void get_peers_observer::reply(msg const& m) // assume it's uTorrent/libtorrent format peer_list = detail::read_endpoint_list(n); #ifndef TORRENT_DISABLE_LOGGING + log_peers(m, r, n.list_size()); +#endif + } + static_cast(algorithm())->got_peers(peer_list); + } + + find_data_observer::reply(m); +} +#ifndef TORRENT_DISABLE_LOGGING +void get_peers_observer::log_peers(msg const& m, bdecode_node const& r, int const size) const +{ auto logger = get_observer(); if (logger != nullptr && logger->should_log(dht_logger::traversal)) { @@ -109,17 +104,11 @@ void get_peers_observer::reply(msg const& m) , print_endpoint(m.addr).c_str() , aux::to_hex({id.string_ptr(), size_t(id.string_length())}).c_str() , distance_exp(algorithm()->target(), node_id(id.string_ptr())) - , n.list_size()); + , size); } } -#endif - } - static_cast(algorithm())->got_peers(peer_list); - } - - find_data_observer::reply(m); } - +#endif void get_peers::got_peers(std::vector const& peers) { if (m_data_callback) m_data_callback(peers); diff --git a/src/kademlia/routing_table.cpp b/src/kademlia/routing_table.cpp index b041bfeba..afbb32613 100644 --- a/src/kademlia/routing_table.cpp +++ b/src/kademlia/routing_table.cpp @@ -1005,14 +1005,7 @@ void routing_table::node_failed(node_id const& nid, udp::endpoint const& ep) j->timed_out(); #ifndef TORRENT_DISABLE_LOGGING - if (m_log != nullptr && m_log->should_log(dht_logger::routing_table)) - { - m_log->log(dht_logger::routing_table, "NODE FAILED id: %s ip: %s fails: %d pinged: %d up-time: %d" - , aux::to_hex(nid).c_str(), print_endpoint(j->ep()).c_str() - , j->fail_count() - , int(j->pinged()) - , int(total_seconds(aux::time_now() - j->first_seen))); - } + log_node_failed(nid, *j); #endif return; } @@ -1027,14 +1020,7 @@ void routing_table::node_failed(node_id const& nid, udp::endpoint const& ep) j->timed_out(); #ifndef TORRENT_DISABLE_LOGGING - if (m_log != nullptr && m_log->should_log(dht_logger::routing_table)) - { - m_log->log(dht_logger::routing_table, "NODE FAILED id: %s ip: %s fails: %d pinged: %d up-time: %d" - , aux::to_hex(nid).c_str(), print_endpoint(j->ep()).c_str() - , j->fail_count() - , int(j->pinged()) - , int(total_seconds(aux::time_now() - j->first_seen))); - } + log_node_failed(nid, *j); #endif // if this node has failed too many times, or if this node @@ -1198,4 +1184,18 @@ bool routing_table::is_full(int const bucket) const && int(i->replacements.size()) >= m_bucket_size); } +#ifndef TORRENT_DISABLE_LOGGING +void routing_table::log_node_failed(node_id const& nid, node_entry const& ne) const +{ + if (m_log != nullptr && m_log->should_log(dht_logger::routing_table)) + { + m_log->log(dht_logger::routing_table, "NODE FAILED id: %s ip: %s fails: %d pinged: %d up-time: %d" + , aux::to_hex(nid).c_str(), print_endpoint(ne.ep()).c_str() + , ne.fail_count() + , int(ne.pinged()) + , int(total_seconds(aux::time_now() - ne.first_seen))); + } +} +#endif + } } // namespace libtorrent::dht diff --git a/src/kademlia/traversal_algorithm.cpp b/src/kademlia/traversal_algorithm.cpp index 1b8406b41..2aeb49bc9 100644 --- a/src/kademlia/traversal_algorithm.cpp +++ b/src/kademlia/traversal_algorithm.cpp @@ -308,16 +308,7 @@ void traversal_algorithm::failed(observer_ptr o, int const flags) } o->flags |= observer::flag_short_timeout; #ifndef TORRENT_DISABLE_LOGGING - dht_observer* logger = get_node().observer(); - if (logger != nullptr && logger->should_log(dht_logger::traversal)) - { - logger->log(dht_logger::traversal - , "[%p] 1ST_TIMEOUT id: %s distance: %d addr: %s branch-factor: %d " - "invoke-count: %d type: %s" - , static_cast(this), aux::to_hex(o->id()).c_str(), distance_exp(m_target, o->id()) - , print_address(o->target_addr()).c_str(), m_branch_factor - , m_invoke_count, name()); - } + log_timeout(o, "1ST_"); #endif } else @@ -328,16 +319,7 @@ void traversal_algorithm::failed(observer_ptr o, int const flags) decrement_branch_factor = (o->flags & observer::flag_short_timeout) != 0; #ifndef TORRENT_DISABLE_LOGGING - dht_observer* logger = get_node().observer(); - if (logger != nullptr && logger->should_log(dht_logger::traversal)) - { - logger->log(dht_logger::traversal - , "[%p] TIMEOUT id: %s distance: %d addr: %s branch-factor: %d " - "invoke-count: %d type: %s" - , static_cast(this), aux::to_hex(o->id()).c_str(), distance_exp(m_target, o->id()) - , print_address(o->target_addr()).c_str(), m_branch_factor - , m_invoke_count, name()); - } + log_timeout(o,""); #endif ++m_timeouts; @@ -360,6 +342,23 @@ void traversal_algorithm::failed(observer_ptr o, int const flags) if (is_done) done(); } +#ifndef TORRENT_DISABLE_LOGGING +void traversal_algorithm::log_timeout(observer_ptr const& o, char const* prefix) const +{ + dht_observer * logger = get_node().observer(); + if (logger != nullptr && logger->should_log(dht_logger::traversal)) + { + logger->log(dht_logger::traversal + , "[%p] %sTIMEOUT id: %s distance: %d addr: %s branch-factor: %d " + "invoke-count: %d type: %s" + , static_cast(this), prefix, aux::to_hex(o->id()).c_str(), distance_exp(m_target, o->id()) + , print_address(o->target_addr()).c_str(), m_branch_factor + , m_invoke_count, name()); + } + +} +#endif + void traversal_algorithm::done() { #ifndef TORRENT_DISABLE_LOGGING