alerts refactor to use span, don't break ABI when logging, fix compilation (#1159)

alerts to use span, don't break ABI when logging, fix compilation
This commit is contained in:
Alden Torres 2016-09-27 23:21:18 -04:00 committed by Arvid Norberg
parent f592d7fcb0
commit 8caebbd026
13 changed files with 69 additions and 104 deletions

View File

@ -1436,8 +1436,6 @@ namespace libtorrent
int protocol;
};
#ifndef TORRENT_DISABLE_LOGGING
// This alert is generated to log informational events related to either
// UPnP or NAT-PMP. They contain a log line and the type (0 = NAT-PMP
// and 1 = UPnP). Displaying these messages to an end user is only useful
@ -1471,8 +1469,6 @@ namespace libtorrent
int m_log_idx;
};
#endif
// This alert is generated when a fastresume file has been passed to
// add_torrent() but the files on disk did not match the fastresume file.
// The error_code explains the reason why the resume file was rejected.
@ -2077,7 +2073,6 @@ namespace libtorrent
udp::endpoint ip;
};
#ifndef TORRENT_DISABLE_LOGGING
// This alert is posted by some session wide event. Its main purpose is
// trouble shooting and debugging. It's not enabled by the default alert
// mask and is enabled by the ``alert::session_log_notification`` bit.
@ -2164,8 +2159,6 @@ namespace libtorrent
int m_str_idx;
};
#endif
// posted if the local service discovery socket fails to start properly.
// it's categorized as ``error_notification``.
struct TORRENT_EXPORT lsd_error_alert final : alert
@ -2318,8 +2311,8 @@ namespace libtorrent
enum direction_t
{ incoming, outgoing };
dht_pkt_alert(aux::stack_allocator& alloc, char const* buf, int size
, dht_pkt_alert::direction_t d, udp::endpoint ep);
dht_pkt_alert(aux::stack_allocator& alloc, span<char const> buf
, dht_pkt_alert::direction_t d, udp::endpoint const& ep);
static const int static_category = alert::dht_log_notification;
TORRENT_DEFINE_ALERT(dht_pkt_alert, 86)
@ -2330,20 +2323,19 @@ namespace libtorrent
// respectively. This buffer is only valid for as long as the alert itself
// is valid, which is owned by libtorrent and reclaimed whenever
// pop_alerts() is called on the session.
char const* pkt_buf() const;
int pkt_size() const;
span<char const> pkt_buf() const;
// whether this is an incoming or outgoing packet.
direction_t dir;
direction_t const dir;
// the DHT node we received this packet from, or sent this packet to
// (depending on ``dir``).
udp::endpoint node;
udp::endpoint const node;
private:
std::reference_wrapper<aux::stack_allocator> m_alloc;
int m_msg_idx;
int m_size;
int const m_msg_idx;
int const m_size;
};
struct TORRENT_EXPORT dht_get_peers_reply_alert final : alert {
@ -2369,7 +2361,7 @@ namespace libtorrent
private:
std::reference_wrapper<aux::stack_allocator> m_alloc;
int m_num_peers;
int const m_num_peers;
int m_peers_idx;
};
@ -2389,15 +2381,15 @@ namespace libtorrent
static const int static_category = alert::dht_notification;
virtual std::string message() const override;
void* userdata;
udp::endpoint addr;
void const* userdata;
udp::endpoint const addr;
bdecode_node response() const;
private:
std::reference_wrapper<aux::stack_allocator> m_alloc;
int m_response_idx;
int m_response_size;
int const m_response_idx;
int const m_response_size;
};
// this is posted when one or more blocks are picked by the piece picker,
@ -2405,8 +2397,6 @@ namespace libtorrent
// picker_log_notification).
struct TORRENT_EXPORT picker_log_alert : peer_alert
{
#ifndef TORRENT_DISABLE_LOGGING
// internal
picker_log_alert(aux::stack_allocator& alloc, torrent_handle const& h
, tcp::endpoint const& ep, peer_id const& peer_id, std::uint32_t flags
@ -2417,8 +2407,6 @@ namespace libtorrent
static const int static_category = alert::picker_log_notification;
virtual std::string message() const override;
#endif // TORRENT_DISABLE_LOGGING
enum picker_flags_t
{
// the ratio of partial pieces is too high. This forces a preference
@ -2441,21 +2429,15 @@ namespace libtorrent
end_game = 0x8000
};
#ifndef TORRENT_DISABLE_LOGGING
// this is a bitmask of which features were enabled for this particular
// pick. The bits are defined in the picker_flags_t enum.
std::uint32_t picker_flags;
std::uint32_t const picker_flags;
std::vector<piece_block> blocks() const;
private:
int m_array_idx;
int m_num_blocks;
#else
picker_log_alert(aux::stack_allocator& alloc)
: peer_alert(alloc, torrent_handle(), tcp::endpoint(), peer_id()) {}
#endif // TORRENT_DISABLE_LOGGING
int const m_array_idx;
int const m_num_blocks;
};
#undef TORRENT_DEFINE_ALERT_IMPL

View File

@ -624,7 +624,7 @@ namespace libtorrent
virtual bool should_log(module_t m) const override;
virtual void log(module_t m, char const* fmt, ...)
override TORRENT_FORMAT(3,4);
virtual void log_packet(message_direction_t dir, char const* pkt, int len
virtual void log_packet(message_direction_t dir, span<char const> pkt
, udp::endpoint const& node) override;
virtual bool should_log_portmap(aux::portmap_transport transport) const override;

View File

@ -53,7 +53,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/disk_buffer_pool.hpp"
#include "libtorrent/file.hpp" // for iovec_t
#if TORRENT_USE_ASSERTS
#if TORRENT_USE_ASSERTS || !defined TORRENT_DISABLE_LOGGING
#include "libtorrent/disk_io_job.hpp"
#endif
@ -61,7 +61,6 @@ namespace libtorrent
{
struct disk_io_job;
class piece_manager;
struct disk_buffer_pool;
struct cache_status;
struct block_cache_reference;
struct counters;
@ -70,7 +69,8 @@ namespace libtorrent
class file_storage;
#endif
#if TORRENT_USE_ASSERTS
#if TORRENT_USE_ASSERTS || !defined TORRENT_DISABLE_LOGGING
struct piece_log_t
{
piece_log_t(int j, int b= -1): job(j), block(b) {}
@ -97,9 +97,11 @@ namespace libtorrent
char const* job_name(int j);
#endif // TORRENT_DISABLE_LOGGING
#if TORRENT_USE_ASSERTS
void print_piece_log(std::vector<piece_log_t> const& piece_log);
void assert_print_piece(cached_piece_entry const* pe);
#endif
extern const char* const job_action_name[];

View File

@ -59,7 +59,7 @@ namespace libtorrent { namespace dht
virtual bool should_log(module_t m) const = 0;
virtual void log(module_t m, char const* fmt, ...) TORRENT_FORMAT(3,4) = 0;
virtual void log_packet(message_direction_t dir, char const* pkt, int len
virtual void log_packet(message_direction_t dir, span<char const> pkt
, udp::endpoint const& node) = 0;
#endif

View File

@ -138,7 +138,7 @@ namespace libtorrent { namespace dht
// implements udp_socket_interface
virtual bool has_quota() override;
virtual bool send_packet(libtorrent::entry& e, udp::endpoint const& addr) override;
virtual bool send_packet(entry& e, udp::endpoint const& addr) override;
// this is the bdecode_node DHT messages are parsed into. It's a member
// in order to avoid having to deallocate and re-allocate it for every

View File

@ -33,7 +33,9 @@ POSSIBILITY OF SUCH DAMAGE.
#ifndef TORRENT_STACK_ALLOCATOR
#include "libtorrent/assert.hpp"
#include "libtorrent/buffer.hpp"
#include "libtorrent/span.hpp"
#include <cstring>
namespace libtorrent { namespace aux
{
@ -48,18 +50,18 @@ namespace libtorrent { namespace aux
int copy_string(std::string const& str)
{
int ret = int(m_storage.size());
int const ret = int(m_storage.size());
m_storage.resize(ret + str.length() + 1);
strcpy(&m_storage[ret], str.c_str());
std::strcpy(&m_storage[ret], str.c_str());
return ret;
}
int copy_string(char const* str)
{
int ret = int(m_storage.size());
int len = int(strlen(str));
int const ret = int(m_storage.size());
int const len = int(std::strlen(str));
m_storage.resize(ret + len + 1);
strcpy(&m_storage[ret], str);
std::strcpy(&m_storage[ret], str);
return ret;
}
@ -84,19 +86,19 @@ namespace libtorrent { namespace aux
return ret;
}
// TODO: 3 use span<> here
int copy_buffer(char const* buf, int const size)
int copy_buffer(span<char const> buf)
{
int const ret = int(m_storage.size());
int const size = int(buf.size());
m_storage.resize(ret + size);
memcpy(&m_storage[ret], buf, size);
std::memcpy(&m_storage[ret], buf.data(), size);
return ret;
}
int allocate(int const bytes)
{
TORRENT_ASSERT(bytes >= 0);
int ret = int(m_storage.size());
int const ret = int(m_storage.size());
m_storage.resize(ret + bytes);
return ret;
}
@ -133,4 +135,3 @@ namespace libtorrent { namespace aux
} }
#endif

View File

@ -80,7 +80,7 @@ struct obs : dht::dht_observer
puts("\n");
}
void log_packet(message_direction_t /* dir */
, char const* /* pkt */, int /* len */
, span<char const> /* pkt */
, udp::endpoint const& /* node */) override {}
bool on_dht_request(string_view /* query */
, dht::msg const& /* request */, entry& /* response */) override

View File

@ -1029,8 +1029,6 @@ namespace libtorrent {
return ret;
}
#ifndef TORRENT_DISABLE_LOGGING
portmap_log_alert::portmap_log_alert(aux::stack_allocator& alloc, int t, const char* m)
: map_type(t)
#ifndef TORRENT_NO_DEPRECATE
@ -1057,8 +1055,6 @@ namespace libtorrent {
return ret;
}
#endif
fastresume_rejected_alert::fastresume_rejected_alert(
aux::stack_allocator& alloc
, torrent_handle const& h
@ -1649,8 +1645,6 @@ namespace libtorrent {
return msg;
}
#ifndef TORRENT_DISABLE_LOGGING
log_alert::log_alert(aux::stack_allocator& alloc, char const* log)
: m_alloc(alloc)
, m_str_idx(alloc.copy_string(log))
@ -1710,8 +1704,6 @@ namespace libtorrent {
+ mode[direction] + " " + event_type + " [ " + msg() + " ]";
}
#endif
lsd_error_alert::lsd_error_alert(aux::stack_allocator&, error_code const& ec)
: alert()
, error(ec)
@ -1881,24 +1873,19 @@ namespace libtorrent {
return ret;
}
// TODO: 3 use span<> here
dht_pkt_alert::dht_pkt_alert(aux::stack_allocator& alloc
, char const* buf, int size, dht_pkt_alert::direction_t d, udp::endpoint ep)
, span<char const> buf, dht_pkt_alert::direction_t d
, udp::endpoint const& ep)
: dir(d)
, node(std::move(ep))
, m_alloc(alloc)
, m_msg_idx(alloc.copy_buffer(buf, size))
, m_size(size)
, m_msg_idx(alloc.copy_buffer(buf))
, m_size(int(buf.size()))
{}
char const* dht_pkt_alert::pkt_buf() const
span<char const> dht_pkt_alert::pkt_buf() const
{
return m_alloc.get().ptr(m_msg_idx);
}
int dht_pkt_alert::pkt_size() const
{
return m_size;
return {m_alloc.get().ptr(m_msg_idx), size_t(m_size)};
}
std::string dht_pkt_alert::message() const
@ -1908,7 +1895,8 @@ namespace libtorrent {
// ignore errors here. This is best-effort. It may be a broken encoding
// but at least we'll print the valid parts
bdecode(pkt_buf(), pkt_buf() + pkt_size(), print, ec, nullptr, 100, 100);
span<char const> pkt = pkt_buf();
bdecode(pkt.data(), pkt.data() + int(pkt.size()), print, ec, nullptr, 100, 100);
std::string msg = print_entry(print, true);
@ -1940,7 +1928,7 @@ namespace libtorrent {
std::size_t size = endp.size();
TORRENT_ASSERT(size < 0x100);
detail::write_uint8(uint8_t(size), ptr);
memcpy(ptr, endp.data(), size);
std::memcpy(ptr, endp.data(), size);
ptr += size;
}
}
@ -1972,7 +1960,7 @@ namespace libtorrent {
const char *ptr = m_alloc.get().ptr(m_peers_idx);
for (int i = 0; i < m_num_peers; i++) {
std::size_t size = detail::read_uint8(ptr);
memcpy(peers[i].data(), ptr, size);
std::memcpy(peers[i].data(), ptr, size);
ptr += size;
}
@ -1983,8 +1971,7 @@ namespace libtorrent {
aux::stack_allocator& alloc, void* userdata_
, udp::endpoint const& addr_, bdecode_node const& response)
: userdata(userdata_), addr(addr_), m_alloc(alloc)
, m_response_idx(alloc.copy_buffer(response.data_section().data()
, int(response.data_section().size())))
, m_response_idx(alloc.copy_buffer(response.data_section()))
, m_response_size(int(response.data_section().size()))
{}
@ -2017,27 +2004,25 @@ namespace libtorrent {
return ret;
}
#ifndef TORRENT_DISABLE_LOGGING
picker_log_alert::picker_log_alert(aux::stack_allocator& alloc, torrent_handle const& h
, tcp::endpoint const& ep, peer_id const& peer_id, std::uint32_t flags
, piece_block const* blocks, int num_blocks)
: peer_alert(alloc, h, ep, peer_id)
, picker_flags(flags)
, m_array_idx(alloc.copy_buffer(reinterpret_cast<char const*>(blocks)
, num_blocks * sizeof(piece_block)))
, m_array_idx(alloc.copy_buffer({reinterpret_cast<char const*>(blocks)
, num_blocks * sizeof(piece_block)}))
, m_num_blocks(num_blocks)
{}
std::vector<piece_block> picker_log_alert::blocks() const
{
// we need to copy this array to make sure the structures are properly
// aigned, not just to have a nice API
// aligned, not just to have a nice API
std::vector<piece_block> ret;
ret.resize(m_num_blocks);
char const* start = m_alloc.get().ptr(m_array_idx);
memcpy(&ret[0], start, m_num_blocks * sizeof(piece_block));
std::memcpy(&ret[0], start, m_num_blocks * sizeof(piece_block));
return ret;
}
@ -2088,6 +2073,4 @@ namespace libtorrent {
return ret;
}
#endif // TORRENT_DISABLE_LOGGING
} // namespace libtorrent

View File

@ -219,7 +219,7 @@ static_assert(sizeof(job_action_name)/sizeof(job_action_name[0])
== disk_io_job::num_job_ids, "disk-job-action and action-name-array mismatch");
#endif
#if TORRENT_USE_ASSERTS
#if TORRENT_USE_ASSERTS || !defined TORRENT_DISABLE_LOGGING
char const* const piece_log_t::job_names[7] =
{
@ -232,7 +232,7 @@ static_assert(sizeof(job_action_name)/sizeof(job_action_name[0])
"set_outstanding_jobs",
};
char const* job_name(int j)
char const* job_name(int const j)
{
if (j < 0 || j >= piece_log_t::last_job)
return "unknown";
@ -242,6 +242,10 @@ static_assert(sizeof(job_action_name)/sizeof(job_action_name[0])
return piece_log_t::job_names[j - piece_log_t::flushing];
}
#endif // TORRENT_DISABLE_LOGGING
#if TORRENT_USE_ASSERTS
void print_piece_log(std::vector<piece_log_t> const& piece_log)
{
for (int i = 0; i < int(piece_log.size()); ++i)

View File

@ -499,9 +499,6 @@ namespace libtorrent { namespace dht
return true;
}
using libtorrent::entry;
using libtorrent::bdecode;
TORRENT_ASSERT(buf_size > 0);
int pos;
@ -511,7 +508,7 @@ namespace libtorrent { namespace dht
{
m_counters.inc_stats_counter(counters::dht_messages_in_dropped);
#ifndef TORRENT_DISABLE_LOGGING
m_log->log_packet(dht_logger::incoming_message, buf.data(), buf_size, ep);
m_log->log_packet(dht_logger::incoming_message, buf, ep);
#endif
return false;
}
@ -519,14 +516,14 @@ namespace libtorrent { namespace dht
if (m_msg.type() != bdecode_node::dict_t)
{
#ifndef TORRENT_DISABLE_LOGGING
m_log->log_packet(dht_logger::incoming_message, buf.data(), buf_size, ep);
m_log->log_packet(dht_logger::incoming_message, buf, ep);
#endif
// it's not a good idea to send a response to an invalid messages
return false;
}
#ifndef TORRENT_DISABLE_LOGGING
m_log->log_packet(dht_logger::incoming_message, buf.data(), buf_size, ep);
m_log->log_packet(dht_logger::incoming_message, buf, ep);
#endif
libtorrent::dht::msg m(m_msg, ep);
@ -605,10 +602,8 @@ namespace libtorrent { namespace dht
return m_send_quota > 0;
}
bool dht_tracker::send_packet(libtorrent::entry& e, udp::endpoint const& addr)
bool dht_tracker::send_packet(entry& e, udp::endpoint const& addr)
{
using libtorrent::bencode;
static char const version_str[] = {'L', 'T'
, LIBTORRENT_VERSION_MAJOR, LIBTORRENT_VERSION_MINOR};
e["v"] = std::string(version_str, version_str + 4);
@ -627,8 +622,7 @@ namespace libtorrent { namespace dht
{
m_counters.inc_stats_counter(counters::dht_messages_out_dropped);
#ifndef TORRENT_DISABLE_LOGGING
m_log->log_packet(dht_logger::outgoing_message, &m_send_buf[0]
, int(m_send_buf.size()), addr);
m_log->log_packet(dht_logger::outgoing_message, m_send_buf, addr);
#endif
return false;
}
@ -639,8 +633,7 @@ namespace libtorrent { namespace dht
, addr.address().is_v6() ? 48 : 28);
m_counters.inc_stats_counter(counters::dht_messages_out);
#ifndef TORRENT_DISABLE_LOGGING
m_log->log_packet(dht_logger::outgoing_message, &m_send_buf[0]
, int(m_send_buf.size()), addr);
m_log->log_packet(dht_logger::outgoing_message, m_send_buf, addr);
#endif
return true;
}

View File

@ -6671,7 +6671,7 @@ namespace aux {
va_end(v);
}
void session_impl::log_packet(message_direction_t dir, char const* pkt, int len
void session_impl::log_packet(message_direction_t dir, span<char const> pkt
, udp::endpoint const& node)
{
if (!m_alerts.should_post<dht_pkt_alert>()) return;
@ -6679,7 +6679,7 @@ namespace aux {
dht_pkt_alert::direction_t d = dir == dht::dht_logger::incoming_message
? dht_pkt_alert::incoming : dht_pkt_alert::outgoing;
m_alerts.emplace_alert<dht_pkt_alert>(pkt, len, d, node);
m_alerts.emplace_alert<dht_pkt_alert>(pkt, d, node);
}
bool session_impl::should_log_portmap(aux::portmap_transport) const

View File

@ -492,7 +492,7 @@ struct obs : dht::dht_observer
fprintf(stderr, "%s\n", buf);
m_log.push_back(buf);
}
void log_packet(message_direction_t dir, char const* pkt, int len
void log_packet(message_direction_t dir, span<char const> pkt
, udp::endpoint const& node) override {}
#endif
bool on_dht_request(string_view query

View File

@ -56,12 +56,12 @@ struct log_t : libtorrent::dht::dht_logger
va_end(v);
}
void log_packet(message_direction_t dir, char const* pkt, int len
void log_packet(message_direction_t dir, span<char const> pkt
, udp::endpoint const& node) override
{
libtorrent::bdecode_node print;
libtorrent::error_code ec;
int ret = bdecode(pkt, pkt + len, print, ec, nullptr, 100, 100);
int ret = bdecode(pkt.data(), pkt.data() + int(pkt.size()), print, ec, nullptr, 100, 100);
TEST_EQUAL(ret, 0);
std::string msg = print_entry(print, true);