diff --git a/include/libtorrent/alert_types.hpp b/include/libtorrent/alert_types.hpp index 0a63445b9..c342d28cb 100644 --- a/include/libtorrent/alert_types.hpp +++ b/include/libtorrent/alert_types.hpp @@ -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 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 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 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 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 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 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 diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index b0a184c81..d987bfe9c 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -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 pkt , udp::endpoint const& node) override; virtual bool should_log_portmap(aux::portmap_transport transport) const override; diff --git a/include/libtorrent/block_cache.hpp b/include/libtorrent/block_cache.hpp index 474df8e9f..43dc89b50 100644 --- a/include/libtorrent/block_cache.hpp +++ b/include/libtorrent/block_cache.hpp @@ -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 const& piece_log); void assert_print_piece(cached_piece_entry const* pe); - #endif extern const char* const job_action_name[]; diff --git a/include/libtorrent/kademlia/dht_observer.hpp b/include/libtorrent/kademlia/dht_observer.hpp index 4e71bd35d..03d12e524 100644 --- a/include/libtorrent/kademlia/dht_observer.hpp +++ b/include/libtorrent/kademlia/dht_observer.hpp @@ -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 pkt , udp::endpoint const& node) = 0; #endif diff --git a/include/libtorrent/kademlia/dht_tracker.hpp b/include/libtorrent/kademlia/dht_tracker.hpp index 414534a52..744369057 100644 --- a/include/libtorrent/kademlia/dht_tracker.hpp +++ b/include/libtorrent/kademlia/dht_tracker.hpp @@ -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 diff --git a/include/libtorrent/stack_allocator.hpp b/include/libtorrent/stack_allocator.hpp index c956e7feb..29c59dad8 100644 --- a/include/libtorrent/stack_allocator.hpp +++ b/include/libtorrent/stack_allocator.hpp @@ -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 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 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 - diff --git a/simulation/test_dht_rate_limit.cpp b/simulation/test_dht_rate_limit.cpp index 30942b2c1..52fc2ea4c 100644 --- a/simulation/test_dht_rate_limit.cpp +++ b/simulation/test_dht_rate_limit.cpp @@ -80,7 +80,7 @@ struct obs : dht::dht_observer puts("\n"); } void log_packet(message_direction_t /* dir */ - , char const* /* pkt */, int /* len */ + , span /* pkt */ , udp::endpoint const& /* node */) override {} bool on_dht_request(string_view /* query */ , dht::msg const& /* request */, entry& /* response */) override diff --git a/src/alert.cpp b/src/alert.cpp index 307545bd6..cf560d508 100644 --- a/src/alert.cpp +++ b/src/alert.cpp @@ -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 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 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 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(blocks) - , num_blocks * sizeof(piece_block))) + , m_array_idx(alloc.copy_buffer({reinterpret_cast(blocks) + , num_blocks * sizeof(piece_block)})) , m_num_blocks(num_blocks) {} std::vector 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 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 diff --git a/src/block_cache.cpp b/src/block_cache.cpp index ad47c624e..7889b0f07 100644 --- a/src/block_cache.cpp +++ b/src/block_cache.cpp @@ -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 const& piece_log) { for (int i = 0; i < int(piece_log.size()); ++i) diff --git a/src/kademlia/dht_tracker.cpp b/src/kademlia/dht_tracker.cpp index 07b32e259..fe0a8332d 100644 --- a/src/kademlia/dht_tracker.cpp +++ b/src/kademlia/dht_tracker.cpp @@ -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; } diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 545f64e90..00139fa33 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -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 pkt , udp::endpoint const& node) { if (!m_alerts.should_post()) 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(pkt, len, d, node); + m_alerts.emplace_alert(pkt, d, node); } bool session_impl::should_log_portmap(aux::portmap_transport) const diff --git a/test/test_dht.cpp b/test/test_dht.cpp index 1e5363434..a89d20a9f 100644 --- a/test/test_dht.cpp +++ b/test/test_dht.cpp @@ -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 pkt , udp::endpoint const& node) override {} #endif bool on_dht_request(string_view query diff --git a/test/test_dos_blocker.cpp b/test/test_dos_blocker.cpp index 62d2a715a..5b6292755 100644 --- a/test/test_dos_blocker.cpp +++ b/test/test_dos_blocker.cpp @@ -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 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);