Log refactoring (#1446)

factor out common logging code in some dht traversal algorithms
This commit is contained in:
Pavel Pimenov 2016-12-24 17:46:21 +03:00 committed by Arvid Norberg
parent 96c2bc7e46
commit c34a2541cc
6 changed files with 57 additions and 61 deletions

View File

@ -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

View File

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

View File

@ -116,6 +116,9 @@ private:
#if TORRENT_USE_IPV6
std::set<std::uint64_t> m_peer6_prefixes;
#endif
#ifndef TORRENT_DISABLE_LOGGING
void log_timeout(observer_ptr const& o, char const* prefix) const;
#endif
};
struct traversal_observer : observer

View File

@ -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<void*>(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<tcp::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<tcp::endpoint>(n);
#ifndef TORRENT_DISABLE_LOGGING
log_peers(m, r, n.list_size());
#endif
}
static_cast<get_peers*>(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<get_peers*>(algorithm())->got_peers(peer_list);
}
find_data_observer::reply(m);
}
#endif
void get_peers::got_peers(std::vector<tcp::endpoint> const& peers)
{
if (m_data_callback) m_data_callback(peers);

View File

@ -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

View File

@ -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<void*>(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<void*>(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<void const*>(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