add compile-time feature to enable logging of all peer requests

This commit is contained in:
Arvid Norberg 2011-12-29 12:15:29 +00:00
parent fcab6a257f
commit 152cee19f1
4 changed files with 56 additions and 0 deletions

View File

@ -284,6 +284,9 @@ feature invariant-checks : on off full : composite propagated link-incompatible
feature.compose <invariant-checks>off : <define>TORRENT_DISABLE_INVARIANT_CHECKS ;
feature.compose <invariant-checks>full : <define>TORRENT_EXPENSIVE_INVARIANT_CHECKS ;
feature request-log : off on : composite propagated link-incompatible ;
feature.compose <request-log>on : <define>TORRENT_REQUEST_LOGGING ;
feature disk-stats : off on : composite propagated link-incompatible ;
feature.compose <disk-stats>on : <define>TORRENT_DISK_STATS ;

View File

@ -907,6 +907,11 @@ namespace libtorrent
int m_buffer_allocations;
#endif
#ifdef TORRENT_REQUEST_LOGGING
// used to log all requests from peers
FILE* m_request_log;
#endif
#ifdef TORRENT_STATS
void rotate_stats_log();
void print_log_line(int tick_interval_ms, ptime now);

View File

@ -73,6 +73,36 @@ namespace libtorrent
return ((v & 7) == 0) ? v : v + (8 - (v & 7));
}
#if defined TORRENT_REQUEST_LOGGING
void write_request_log(FILE* f, sha1_hash const& ih
, peer_connection* p, peer_request const& r)
{
// the event format in the log is:
// uint64_t timestamp (microseconds)
// uint64_t info-hash prefix
// uint32_t peer identifier
// uint32_t piece
// uint32_t start offset
// uint32_t length
char event[32];
char* ptr = event;
detail::write_uint64(total_microseconds((time_now_hires() - min_time())), ptr);
memcpy(ptr, &ih[0], 8);
ptr += 8;
detail::write_uint32(boost::uint32_t(p), ptr);
detail::write_uint32(r.piece, ptr);
detail::write_uint32(r.start, ptr);
detail::write_uint32(r.length, ptr);
int ret = fwrite(event, 1, sizeof(event), f);
if (ret != sizeof(event))
{
fprintf(stderr, "ERROR writing to request log: (%d) %s\n"
, errno, strerror(errno));
}
}
#endif
// outbound connection
peer_connection::peer_connection(
session_impl& ses
@ -2103,6 +2133,10 @@ namespace libtorrent
{
m_choke_rejects = 0;
m_requests.push_back(r);
#ifdef TORRENT_REQUEST_LOGGING
if (m_ses.m_request_log)
write_request_log(m_ses.m_request_log, t->info_hash(), this, r);
#endif
m_last_incoming_request = time_now();
fill_send_buffer();
}

View File

@ -628,6 +628,16 @@ namespace aux {
m_disk_queues[0] = 0;
m_disk_queues[1] = 0;
#ifdef TORRENT_REQUEST_LOGGING
char log_filename[200];
snprintf(log_filename, sizeof(log_filename), "requests-%d.log", getpid());
m_request_log = fopen(log_filename, "w+");
if (m_request_log == 0)
{
fprintf(stderr, "failed to open request log file: (%d) %s\n", errno, strerror(errno));
}
#endif
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
m_logger = create_log("main_session", listen_port(), false);
(*m_logger) << time_now_string() << "\n";
@ -5072,6 +5082,10 @@ namespace aux {
TORRENT_ASSERT(m_connections.empty());
TORRENT_ASSERT(m_connections.empty());
#ifdef TORRENT_REQUEST_LOGGING
if (m_request_log) fclose(m_request_log);
#endif
#ifdef TORRENT_STATS
if (m_stats_logger) fclose(m_stats_logger);
#endif