From b1e36a53e3c8ed1f577b1bbe7ed278231829046a Mon Sep 17 00:00:00 2001 From: arvidn Date: Tue, 28 Jun 2016 01:30:25 -0400 Subject: [PATCH 1/3] print peer logs directly into the allocation --- include/libtorrent/alert_types.hpp | 4 ++++ include/libtorrent/stack_allocator.hpp | 29 +++++++++++++++++++++----- src/alert.cpp | 11 ++++++++++ src/peer_connection.cpp | 11 ++++------ 4 files changed, 43 insertions(+), 12 deletions(-) diff --git a/include/libtorrent/alert_types.hpp b/include/libtorrent/alert_types.hpp index 77f6971ff..e93a674d6 100644 --- a/include/libtorrent/alert_types.hpp +++ b/include/libtorrent/alert_types.hpp @@ -2150,6 +2150,10 @@ namespace libtorrent , tcp::endpoint const& i, peer_id const& pi , peer_log_alert::direction_t dir , char const* event, char const* log); + 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* fmt, va_list v); TORRENT_DEFINE_ALERT(peer_log_alert, 81) diff --git a/include/libtorrent/stack_allocator.hpp b/include/libtorrent/stack_allocator.hpp index 8b1468b71..cfe084acf 100644 --- a/include/libtorrent/stack_allocator.hpp +++ b/include/libtorrent/stack_allocator.hpp @@ -63,29 +63,48 @@ 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 + + m_storage.resize(len > 512 ? 512 : len); + 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())); diff --git a/src/alert.cpp b/src/alert.cpp index f77650ed0..c179369cc 100644 --- a/src/alert.cpp +++ b/src/alert.cpp @@ -1699,6 +1699,17 @@ namespace libtorrent { , m_str_idx(alloc.copy_string(log)) {} + peer_log_alert::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* fmt, va_list v) + : peer_alert(alloc, h, i, pi) + , event_type(event) + , direction(dir) + , m_str_idx(alloc.format_string(fmt, v)) + {} + char const* peer_log_alert::msg() const { return m_alloc.get().ptr(m_str_idx); diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 78dcb9698..146a4b1d0 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -509,18 +509,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 t = m_torrent.lock(); if (t) h = t->get_handle(); m_ses.alerts().emplace_alert( - h, m_remote, m_peer_id, direction, event, buf); + h, m_remote, m_peer_id, direction, event, fmt, v); + + va_end(v); + } #endif From 55d8b0625fa4aa34e0debefb1881a5a3ae3c000f Mon Sep 17 00:00:00 2001 From: arvidn Date: Tue, 28 Jun 2016 08:43:31 -0400 Subject: [PATCH 2/3] fix --- include/libtorrent/stack_allocator.hpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/include/libtorrent/stack_allocator.hpp b/include/libtorrent/stack_allocator.hpp index cfe084acf..926d8d46e 100644 --- a/include/libtorrent/stack_allocator.hpp +++ b/include/libtorrent/stack_allocator.hpp @@ -77,7 +77,10 @@ namespace libtorrent { namespace aux #pragma clang diagnostic pop #endif - m_storage.resize(len > 512 ? 512 : len); + 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; } From 08aebd2729600f0836bd4454a5ef793f4217343a Mon Sep 17 00:00:00 2001 From: arvidn Date: Tue, 28 Jun 2016 18:52:51 -0400 Subject: [PATCH 3/3] format more log messages in alert constructors --- include/libtorrent/alert_types.hpp | 9 +++------ src/alert.cpp | 25 ++++++++---------------- src/peer_connection.cpp | 31 +++++++++++++++--------------- src/session_impl.cpp | 14 ++++---------- src/torrent.cpp | 8 ++------ 5 files changed, 33 insertions(+), 54 deletions(-) diff --git a/include/libtorrent/alert_types.hpp b/include/libtorrent/alert_types.hpp index e93a674d6..5fb144cae 100644 --- a/include/libtorrent/alert_types.hpp +++ b/include/libtorrent/alert_types.hpp @@ -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) @@ -2146,10 +2147,6 @@ namespace libtorrent }; // internal - 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); 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 @@ -2301,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) diff --git a/src/alert.cpp b/src/alert.cpp index c179369cc..46d6305c8 100644 --- a/src/alert.cpp +++ b/src/alert.cpp @@ -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 @@ -1686,19 +1690,6 @@ namespace libtorrent { return torrent_alert::message() + ": " + msg(); } - 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) - : peer_alert(alloc, h, i, pi) - , event_type(event) - , direction(dir) - , m_str_idx(alloc.copy_string(log)) - {} - peer_log_alert::peer_log_alert(aux::stack_allocator& alloc , torrent_handle const& h , tcp::endpoint const& i, peer_id const& pi @@ -1866,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 diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 146a4b1d0..a5327b137 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -34,21 +34,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include -#if TORRENT_USE_ASSERTS -#include -#endif - -#ifdef TORRENT_USE_OPENSSL -#include -#endif - -#ifndef TORRENT_DISABLE_LOGGING -#include // for va_start, va_end -#include // 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 +#endif + +#ifdef TORRENT_USE_OPENSSL +#include +#endif + +#ifndef TORRENT_DISABLE_LOGGING +#include // for va_start, va_end +#include // for vsnprintf +#include "libtorrent/socket_io.hpp" +#include "libtorrent/hex.hpp" // to_hex +#endif + //#define TORRENT_CORRUPT_DATA using boost::shared_ptr; diff --git a/src/session_impl.cpp b/src/session_impl.cpp index c927223d1..7cf4752c6 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -4443,10 +4443,7 @@ namespace aux { void session_impl::session_vlog(char const* fmt, va_list& v) const { if (!m_alerts.should_post()) return; - - char buf[1024]; - vsnprintf(buf, sizeof(buf), fmt, v); - m_alerts.emplace_alert(buf); + m_alerts.emplace_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( + static_cast(m), fmt, v); va_end(v); - m_alerts.emplace_alert(static_cast(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 }} diff --git a/src/torrent.cpp b/src/torrent.cpp index 807aab605..218a0c56c 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -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( - const_cast(this)->get_handle(), buf); + const_cast(this)->get_handle(), fmt, v); + va_end(v); } #endif