Merge pull request #866 from arvidn/peer-log

print peer logs directly into the allocation
This commit is contained in:
Arvid Norberg 2016-06-29 19:50:01 -04:00 committed by GitHub
commit 3ab0623980
6 changed files with 69 additions and 56 deletions

View File

@ -2092,6 +2092,7 @@ namespace libtorrent
{
// internal
log_alert(aux::stack_allocator& alloc, char const* log);
log_alert(aux::stack_allocator& alloc, char const* fmt, va_list v);
TORRENT_DEFINE_ALERT(log_alert, 79)
@ -2114,7 +2115,7 @@ namespace libtorrent
{
// internal
torrent_log_alert(aux::stack_allocator& alloc, torrent_handle const& h
, char const* log);
, char const* fmt, va_list v);
TORRENT_DEFINE_ALERT(torrent_log_alert, 80)
@ -2149,7 +2150,7 @@ namespace libtorrent
peer_log_alert(aux::stack_allocator& alloc, torrent_handle const& h
, tcp::endpoint const& i, peer_id const& pi
, peer_log_alert::direction_t dir
, char const* event, char const* log);
, char const* event, char const* fmt, va_list v);
TORRENT_DEFINE_ALERT(peer_log_alert, 81)
@ -2297,7 +2298,7 @@ namespace libtorrent
};
dht_log_alert(aux::stack_allocator& alloc
, dht_module_t m, char const* msg);
, dht_module_t m, char const* fmt, va_list v);
static const int static_category = alert::dht_log_notification;
TORRENT_DEFINE_ALERT(dht_log_alert, 85)

View File

@ -63,29 +63,51 @@ namespace libtorrent { namespace aux
return ret;
}
int copy_buffer(char const* buf, int size)
int format_string(char const* fmt, va_list v)
{
int ret = int(m_storage.size());
int const ret = int(m_storage.size());
m_storage.resize(ret + 512);
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wformat-nonliteral"
#endif
int const len = std::vsnprintf(m_storage.data() + ret, 512, fmt, v);
#ifdef __clang__
#pragma clang diagnostic pop
#endif
if (len < 0) return copy_string("(format error)");
// +1 is to include the 0-terminator
m_storage.resize(ret + (len > 512 ? 512 : len) + 1);
return ret;
}
int copy_buffer(char const* buf, int const size)
{
int const ret = int(m_storage.size());
m_storage.resize(ret + size);
memcpy(&m_storage[ret], buf, size);
return ret;
}
int allocate(int bytes)
int allocate(int const bytes)
{
TORRENT_ASSERT(bytes >= 0);
int ret = int(m_storage.size());
m_storage.resize(ret + bytes);
return ret;
}
char* ptr(int idx)
char* ptr(int const idx)
{
TORRENT_ASSERT(idx >= 0);
TORRENT_ASSERT(idx < int(m_storage.size()));
return &m_storage[idx];
}
char const* ptr(int idx) const
char const* ptr(int const idx) const
{
TORRENT_ASSERT(idx >= 0);
TORRENT_ASSERT(idx < int(m_storage.size()));

View File

@ -1659,6 +1659,10 @@ namespace libtorrent {
: m_alloc(alloc)
, m_str_idx(alloc.copy_string(log))
{}
log_alert::log_alert(aux::stack_allocator& alloc, char const* fmt, va_list v)
: m_alloc(alloc)
, m_str_idx(alloc.format_string(fmt, v))
{}
char const* log_alert::msg() const
{
@ -1671,9 +1675,9 @@ namespace libtorrent {
}
torrent_log_alert::torrent_log_alert(aux::stack_allocator& alloc, torrent_handle const& h
, char const* log)
, char const* fmt, va_list v)
: torrent_alert(alloc, h)
, m_str_idx(alloc.copy_string(log))
, m_str_idx(alloc.format_string(fmt, v))
{}
char const* torrent_log_alert::msg() const
@ -1688,15 +1692,13 @@ namespace libtorrent {
peer_log_alert::peer_log_alert(aux::stack_allocator& alloc
, torrent_handle const& h
, tcp::endpoint const& i
, peer_id const& pi
, direction_t dir
, char const* event
, char const* log)
, tcp::endpoint const& i, peer_id const& pi
, peer_log_alert::direction_t dir
, char const* event, char const* fmt, va_list v)
: peer_alert(alloc, h, i, pi)
, event_type(event)
, direction(dir)
, m_str_idx(alloc.copy_string(log))
, m_str_idx(alloc.format_string(fmt, v))
{}
char const* peer_log_alert::msg() const
@ -1855,10 +1857,10 @@ namespace libtorrent {
}
dht_log_alert::dht_log_alert(aux::stack_allocator& alloc
, dht_log_alert::dht_module_t m, const char* msg)
, dht_log_alert::dht_module_t m, const char* fmt, va_list v)
: module(m)
, m_alloc(alloc)
, m_msg_idx(alloc.copy_string(msg))
, m_msg_idx(alloc.format_string(fmt, v))
{}
char const* dht_log_alert::log_message() const

View File

@ -34,21 +34,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <functional>
#include <cstdint>
#if TORRENT_USE_ASSERTS
#include <set>
#endif
#ifdef TORRENT_USE_OPENSSL
#include <openssl/rand.h>
#endif
#ifndef TORRENT_DISABLE_LOGGING
#include <cstdarg> // for va_start, va_end
#include <cstdio> // for vsnprintf
#include "libtorrent/socket_io.hpp"
#include "libtorrent/hex.hpp" // to_hex
#endif
#include "libtorrent/config.hpp"
#include "libtorrent/peer_connection.hpp"
#include "libtorrent/identify_client.hpp"
#include "libtorrent/entry.hpp"
@ -79,6 +65,21 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/close_reason.hpp"
#include "libtorrent/aux_/time.hpp"
#if TORRENT_USE_ASSERTS
#include <set>
#endif
#ifdef TORRENT_USE_OPENSSL
#include <openssl/rand.h>
#endif
#ifndef TORRENT_DISABLE_LOGGING
#include <cstdarg> // for va_start, va_end
#include <cstdio> // for vsnprintf
#include "libtorrent/socket_io.hpp"
#include "libtorrent/hex.hpp" // to_hex
#endif
//#define TORRENT_CORRUPT_DATA
using boost::shared_ptr;
@ -509,18 +510,15 @@ namespace libtorrent
va_list v;
va_start(v, fmt);
// TODO: it would be neat to be able to print this straight into the
// alert's stack allocator
char buf[512];
std::vsnprintf(buf, sizeof(buf), fmt, v);
va_end(v);
torrent_handle h;
boost::shared_ptr<torrent> t = m_torrent.lock();
if (t) h = t->get_handle();
m_ses.alerts().emplace_alert<peer_log_alert>(
h, m_remote, m_peer_id, direction, event, buf);
h, m_remote, m_peer_id, direction, event, fmt, v);
va_end(v);
}
#endif

View File

@ -4443,10 +4443,7 @@ namespace aux {
void session_impl::session_vlog(char const* fmt, va_list& v) const
{
if (!m_alerts.should_post<log_alert>()) return;
char buf[1024];
vsnprintf(buf, sizeof(buf), fmt, v);
m_alerts.emplace_alert<log_alert>(buf);
m_alerts.emplace_alert<log_alert>(fmt, v);
}
#endif
@ -6593,10 +6590,9 @@ namespace aux {
va_list v;
va_start(v, fmt);
char buf[1024];
std::vsnprintf(buf, sizeof(buf), fmt, v);
m_alerts.emplace_alert<dht_log_alert>(
static_cast<dht_log_alert::dht_module_t>(m), fmt, v);
va_end(v);
m_alerts.emplace_alert<dht_log_alert>(static_cast<dht_log_alert::dht_module_t>(m), buf);
}
void session_impl::log_packet(message_direction_t dir, char const* pkt, int len
@ -6938,10 +6934,8 @@ namespace aux {
{
va_list v;
va_start(v, fmt);
char usr[1024];
vsnprintf(usr, sizeof(usr), fmt, v);
m_ses.session_vlog(fmt, v);
va_end(v);
m_ses.session_log("%s", usr);
}
#endif // TORRENT_DISABLE_LOGGING
}}

View File

@ -11310,13 +11310,9 @@ namespace libtorrent
va_list v;
va_start(v, fmt);
char buf[400];
vsnprintf(buf, sizeof(buf), fmt, v);
va_end(v);
alerts().emplace_alert<torrent_log_alert>(
const_cast<torrent*>(this)->get_handle(), buf);
const_cast<torrent*>(this)->get_handle(), fmt, v);
va_end(v);
}
#endif