Log refactoring (#1446)
factor out common logging code in some dht traversal algorithms
This commit is contained in:
parent
96c2bc7e46
commit
c34a2541cc
|
@ -92,6 +92,10 @@ struct get_peers_observer : find_data_observer
|
||||||
{}
|
{}
|
||||||
|
|
||||||
virtual void reply(msg const&);
|
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
|
struct obfuscated_get_peers_observer : traversal_observer
|
||||||
|
|
|
@ -277,6 +277,7 @@ private:
|
||||||
|
|
||||||
#ifndef TORRENT_DISABLE_LOGGING
|
#ifndef TORRENT_DISABLE_LOGGING
|
||||||
dht_logger* m_log;
|
dht_logger* m_log;
|
||||||
|
void log_node_failed(node_id const& nid, node_entry const& ne) const;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
table_t::iterator find_bucket(node_id const& id);
|
table_t::iterator find_bucket(node_id const& id);
|
||||||
|
|
|
@ -116,6 +116,9 @@ private:
|
||||||
#if TORRENT_USE_IPV6
|
#if TORRENT_USE_IPV6
|
||||||
std::set<std::uint64_t> m_peer6_prefixes;
|
std::set<std::uint64_t> m_peer6_prefixes;
|
||||||
#endif
|
#endif
|
||||||
|
#ifndef TORRENT_DISABLE_LOGGING
|
||||||
|
void log_timeout(observer_ptr const& o, char const* prefix) const;
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
struct traversal_observer : observer
|
struct traversal_observer : observer
|
||||||
|
|
|
@ -69,23 +69,7 @@ void get_peers_observer::reply(msg const& m)
|
||||||
char const* end = peers + n.list_at(0).string_length();
|
char const* end = peers + n.list_at(0).string_length();
|
||||||
|
|
||||||
#ifndef TORRENT_DISABLE_LOGGING
|
#ifndef TORRENT_DISABLE_LOGGING
|
||||||
auto logger = get_observer();
|
log_peers(m, r, int((end - peers) / 6));
|
||||||
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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
while (end - peers >= 6)
|
while (end - peers >= 6)
|
||||||
peer_list.push_back(detail::read_v4_endpoint<tcp::endpoint>(peers));
|
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
|
// assume it's uTorrent/libtorrent format
|
||||||
peer_list = detail::read_endpoint_list<tcp::endpoint>(n);
|
peer_list = detail::read_endpoint_list<tcp::endpoint>(n);
|
||||||
#ifndef TORRENT_DISABLE_LOGGING
|
#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();
|
auto logger = get_observer();
|
||||||
if (logger != nullptr && logger->should_log(dht_logger::traversal))
|
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()
|
, print_endpoint(m.addr).c_str()
|
||||||
, aux::to_hex({id.string_ptr(), size_t(id.string_length())}).c_str()
|
, aux::to_hex({id.string_ptr(), size_t(id.string_length())}).c_str()
|
||||||
, distance_exp(algorithm()->target(), node_id(id.string_ptr()))
|
, 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)
|
void get_peers::got_peers(std::vector<tcp::endpoint> const& peers)
|
||||||
{
|
{
|
||||||
if (m_data_callback) m_data_callback(peers);
|
if (m_data_callback) m_data_callback(peers);
|
||||||
|
|
|
@ -1005,14 +1005,7 @@ void routing_table::node_failed(node_id const& nid, udp::endpoint const& ep)
|
||||||
j->timed_out();
|
j->timed_out();
|
||||||
|
|
||||||
#ifndef TORRENT_DISABLE_LOGGING
|
#ifndef TORRENT_DISABLE_LOGGING
|
||||||
if (m_log != nullptr && m_log->should_log(dht_logger::routing_table))
|
log_node_failed(nid, *j);
|
||||||
{
|
|
||||||
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)));
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -1027,14 +1020,7 @@ void routing_table::node_failed(node_id const& nid, udp::endpoint const& ep)
|
||||||
j->timed_out();
|
j->timed_out();
|
||||||
|
|
||||||
#ifndef TORRENT_DISABLE_LOGGING
|
#ifndef TORRENT_DISABLE_LOGGING
|
||||||
if (m_log != nullptr && m_log->should_log(dht_logger::routing_table))
|
log_node_failed(nid, *j);
|
||||||
{
|
|
||||||
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)));
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// if this node has failed too many times, or if this node
|
// 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);
|
&& 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
|
} } // namespace libtorrent::dht
|
||||||
|
|
|
@ -308,16 +308,7 @@ void traversal_algorithm::failed(observer_ptr o, int const flags)
|
||||||
}
|
}
|
||||||
o->flags |= observer::flag_short_timeout;
|
o->flags |= observer::flag_short_timeout;
|
||||||
#ifndef TORRENT_DISABLE_LOGGING
|
#ifndef TORRENT_DISABLE_LOGGING
|
||||||
dht_observer* logger = get_node().observer();
|
log_timeout(o, "1ST_");
|
||||||
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());
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
else
|
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;
|
decrement_branch_factor = (o->flags & observer::flag_short_timeout) != 0;
|
||||||
|
|
||||||
#ifndef TORRENT_DISABLE_LOGGING
|
#ifndef TORRENT_DISABLE_LOGGING
|
||||||
dht_observer* logger = get_node().observer();
|
log_timeout(o,"");
|
||||||
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());
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
++m_timeouts;
|
++m_timeouts;
|
||||||
|
@ -360,6 +342,23 @@ void traversal_algorithm::failed(observer_ptr o, int const flags)
|
||||||
if (is_done) done();
|
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()
|
void traversal_algorithm::done()
|
||||||
{
|
{
|
||||||
#ifndef TORRENT_DISABLE_LOGGING
|
#ifndef TORRENT_DISABLE_LOGGING
|
||||||
|
|
Loading…
Reference in New Issue