diff --git a/Makefile.am b/Makefile.am index b698f739e..888ea5680 100644 --- a/Makefile.am +++ b/Makefile.am @@ -40,6 +40,7 @@ src_libbitcoin_network_la_SOURCES = \ src/settings.cpp \ src/async/deadline.cpp \ src/async/logger.cpp \ + src/async/reporter.cpp \ src/async/thread.cpp \ src/async/threadpool.cpp \ src/async/time.cpp \ @@ -217,12 +218,13 @@ include_bitcoin_network_async_HEADERS = \ include/bitcoin/network/async/enable_shared_from_base.hpp \ include/bitcoin/network/async/handlers.hpp \ include/bitcoin/network/async/logger.hpp \ + include/bitcoin/network/async/reporter.hpp \ include/bitcoin/network/async/subscriber.hpp \ include/bitcoin/network/async/thread.hpp \ include/bitcoin/network/async/threadpool.hpp \ include/bitcoin/network/async/time.hpp \ include/bitcoin/network/async/timer.hpp \ - include/bitcoin/network/async/track.hpp + include/bitcoin/network/async/tracker.hpp include_bitcoin_network_configdir = ${includedir}/bitcoin/network/config include_bitcoin_network_config_HEADERS = \ @@ -234,7 +236,7 @@ include_bitcoin_network_impl_asyncdir = ${includedir}/bitcoin/network/impl/async include_bitcoin_network_impl_async_HEADERS = \ include/bitcoin/network/impl/async/enable_shared_from_base.ipp \ include/bitcoin/network/impl/async/subscriber.ipp \ - include/bitcoin/network/impl/async/track.ipp + include/bitcoin/network/impl/async/tracker.ipp include_bitcoin_network_messagesdir = ${includedir}/bitcoin/network/messages include_bitcoin_network_messages_HEADERS = \ diff --git a/builds/cmake/CMakeLists.txt b/builds/cmake/CMakeLists.txt index 22efb2b10..a7942825a 100644 --- a/builds/cmake/CMakeLists.txt +++ b/builds/cmake/CMakeLists.txt @@ -159,6 +159,7 @@ add_library( ${CANONICAL_LIB_NAME} "../../src/settings.cpp" "../../src/async/deadline.cpp" "../../src/async/logger.cpp" + "../../src/async/reporter.cpp" "../../src/async/thread.cpp" "../../src/async/threadpool.cpp" "../../src/async/time.cpp" diff --git a/builds/msvc/vs2022/libbitcoin-network/libbitcoin-network.vcxproj b/builds/msvc/vs2022/libbitcoin-network/libbitcoin-network.vcxproj index 1d97ea8d9..2456c621b 100644 --- a/builds/msvc/vs2022/libbitcoin-network/libbitcoin-network.vcxproj +++ b/builds/msvc/vs2022/libbitcoin-network/libbitcoin-network.vcxproj @@ -75,6 +75,7 @@ + @@ -152,12 +153,13 @@ + - + @@ -238,7 +240,7 @@ - + diff --git a/builds/msvc/vs2022/libbitcoin-network/libbitcoin-network.vcxproj.filters b/builds/msvc/vs2022/libbitcoin-network/libbitcoin-network.vcxproj.filters index 3b65c242a..10498c0e9 100644 --- a/builds/msvc/vs2022/libbitcoin-network/libbitcoin-network.vcxproj.filters +++ b/builds/msvc/vs2022/libbitcoin-network/libbitcoin-network.vcxproj.filters @@ -75,6 +75,9 @@ src\async + + src\async + src\async @@ -302,6 +305,9 @@ include\bitcoin\network\async + + include\bitcoin\network\async + include\bitcoin\network\async @@ -317,7 +323,7 @@ include\bitcoin\network\async - + include\bitcoin\network\async @@ -556,7 +562,7 @@ include\bitcoin\network\impl\async - + include\bitcoin\network\impl\async diff --git a/include/bitcoin/network.hpp b/include/bitcoin/network.hpp index 65ccd8556..1a6b57e39 100644 --- a/include/bitcoin/network.hpp +++ b/include/bitcoin/network.hpp @@ -27,12 +27,13 @@ #include #include #include +#include #include #include #include #include #include -#include +#include #include #include #include diff --git a/include/bitcoin/network/async/async.hpp b/include/bitcoin/network/async/async.hpp index e336b32c2..1fd0a2478 100644 --- a/include/bitcoin/network/async/async.hpp +++ b/include/bitcoin/network/async/async.hpp @@ -24,11 +24,12 @@ #include #include #include +#include #include #include #include #include #include -#include +#include #endif diff --git a/include/bitcoin/network/async/deadline.hpp b/include/bitcoin/network/async/deadline.hpp index 16cf1add7..83d6c53a4 100644 --- a/include/bitcoin/network/async/deadline.hpp +++ b/include/bitcoin/network/async/deadline.hpp @@ -25,7 +25,7 @@ #include #include #include -#include +#include #include #include @@ -37,7 +37,7 @@ namespace network { /// This simplifies invocation, eliminates boost-specific error handling and /// makes timer firing and cancellation conditions safe for shared objects. class BCT_API deadline final - : public std::enable_shared_from_this, public track + : public std::enable_shared_from_this, protected tracker { public: DELETE_COPY_MOVE(deadline); diff --git a/include/bitcoin/network/async/reporter.hpp b/include/bitcoin/network/async/reporter.hpp new file mode 100644 index 000000000..38b6249df --- /dev/null +++ b/include/bitcoin/network/async/reporter.hpp @@ -0,0 +1,44 @@ +/** + * Copyright (c) 2011-2022 libbitcoin developers (see AUTHORS) + * + * This file is part of libbitcoin. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +#ifndef LIBBITCOIN_NETWORK_ASYNC_REPORTER_HPP +#define LIBBITCOIN_NETWORK_ASYNC_REPORTER_HPP + +#include +#include + +namespace libbitcoin { +namespace network { + +class BCT_API reporter +{ +protected: + reporter(const logger& log) NOEXCEPT; + +public: + const logger& log() const NOEXCEPT; + +private: + // This is thread safe. + const logger& log_; +}; + +} // namespace network +} // namespace libbitcoin + +#endif diff --git a/include/bitcoin/network/async/track.hpp b/include/bitcoin/network/async/tracker.hpp similarity index 72% rename from include/bitcoin/network/async/track.hpp rename to include/bitcoin/network/async/tracker.hpp index 36ec9373a..920344ad1 100644 --- a/include/bitcoin/network/async/track.hpp +++ b/include/bitcoin/network/async/tracker.hpp @@ -16,8 +16,8 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -#ifndef LIBBITCOIN_NETWORK_ASYNC_TRACK_HPP -#define LIBBITCOIN_NETWORK_ASYNC_TRACK_HPP +#ifndef LIBBITCOIN_NETWORK_ASYNC_TRACKER_HPP +#define LIBBITCOIN_NETWORK_ASYNC_TRACKER_HPP #include #include @@ -26,19 +26,14 @@ namespace libbitcoin { namespace network { -/// Thread safe, base class. -/// Class to log changes in the reference count of shared objects. -template -class track +template +class tracker { protected: - DEFAULT_COPY_MOVE(track); + DEFAULT_COPY_MOVE(tracker); - track(const logger& log) NOEXCEPT; - ~track() NOEXCEPT; - -public: - const logger& get_log() const NOEXCEPT; + tracker(const logger& log) NOEXCEPT; + ~tracker() NOEXCEPT; private: // These are thread safe. @@ -49,6 +44,6 @@ class track } // namespace network } // namespace libbitcoin -#include +#include #endif diff --git a/include/bitcoin/network/impl/async/track.ipp b/include/bitcoin/network/impl/async/tracker.ipp similarity index 67% rename from include/bitcoin/network/impl/async/track.ipp rename to include/bitcoin/network/impl/async/tracker.ipp index fb68b9fc4..53e661743 100644 --- a/include/bitcoin/network/impl/async/track.ipp +++ b/include/bitcoin/network/impl/async/tracker.ipp @@ -16,8 +16,8 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -#ifndef LIBBITCOIN_NETWORK_ASYNC_TRACK_IPP -#define LIBBITCOIN_NETWORK_ASYNC_TRACK_IPP +#ifndef LIBBITCOIN_NETWORK_ASYNC_TRACKER_IPP +#define LIBBITCOIN_NETWORK_ASYNC_TRACKER_IPP #include #include @@ -28,40 +28,34 @@ namespace libbitcoin { namespace network { -template -std::atomic track::instances_(0); +template +std::atomic tracker::instances_(zero); -template -track::track(const logger& log) NOEXCEPT +template +tracker::tracker(const logger& log) NOEXCEPT : log_(log) { - if constexpr (Track && bc::build_checked) + if constexpr (build_checked) { BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT) - log_.write() << typeid(Shared).name() + log_.write() << typeid(Class).name() << "(" << ++instances_ << ")" << std::endl; BC_POP_WARNING() } } -template -track::~track() NOEXCEPT +template +tracker::~tracker() NOEXCEPT { - if constexpr (Track && bc::build_checked) + if constexpr (build_checked) { BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT) - log_.write() << typeid(Shared).name() + log_.write() << typeid(Class).name() << "(" << --instances_ << ")~" << std::endl; BC_POP_WARNING() } } -template -const logger& track::get_log() const NOEXCEPT -{ - return log_; -} - } // namespace network } // namespace libbitcoin diff --git a/include/bitcoin/network/net/acceptor.hpp b/include/bitcoin/network/net/acceptor.hpp index 33b124b96..5596299a6 100644 --- a/include/bitcoin/network/net/acceptor.hpp +++ b/include/bitcoin/network/net/acceptor.hpp @@ -34,7 +34,8 @@ namespace network { /// Create inbound socket connections. /// Stop is thread safe and idempotent, may be called multiple times. class BCT_API acceptor - : public std::enable_shared_from_this, public track + : public std::enable_shared_from_this, public reporter, + protected tracker { public: DELETE_COPY_MOVE(acceptor); diff --git a/include/bitcoin/network/net/channel.hpp b/include/bitcoin/network/net/channel.hpp index 1f37a0b79..12cf10454 100644 --- a/include/bitcoin/network/net/channel.hpp +++ b/include/bitcoin/network/net/channel.hpp @@ -37,7 +37,7 @@ class session; /// * See proxy for its thread safety constraints. /// A channel is a proxy with logged timers and state. class BCT_API channel - : public proxy, public track + : public proxy, protected tracker { public: DELETE_COPY_MOVE(channel); diff --git a/include/bitcoin/network/net/connector.hpp b/include/bitcoin/network/net/connector.hpp index e5bacab8e..ac0e60a44 100644 --- a/include/bitcoin/network/net/connector.hpp +++ b/include/bitcoin/network/net/connector.hpp @@ -36,7 +36,8 @@ namespace network { /// Create outbound socket connections. /// Stop is thread safe and idempotent, may be called multiple times. class BCT_API connector - : public std::enable_shared_from_this, public track + : public std::enable_shared_from_this, public reporter, + protected tracker { public: DELETE_COPY_MOVE(connector); diff --git a/include/bitcoin/network/net/hosts.hpp b/include/bitcoin/network/net/hosts.hpp index 3732ad75f..49fa781a3 100644 --- a/include/bitcoin/network/net/hosts.hpp +++ b/include/bitcoin/network/net/hosts.hpp @@ -38,7 +38,7 @@ namespace network { /// The file is a line-oriented set of config::authority serializations. /// Duplicate addresses and those with zero-valued ports are disacarded. class BCT_API hosts - : public track + : public reporter, protected tracker { public: DELETE_COPY_MOVE(hosts); diff --git a/include/bitcoin/network/net/proxy.hpp b/include/bitcoin/network/net/proxy.hpp index 5b7ab94d8..460a7c390 100644 --- a/include/bitcoin/network/net/proxy.hpp +++ b/include/bitcoin/network/net/proxy.hpp @@ -38,7 +38,7 @@ namespace network { /// notify/send_bytes are protected/virtual for test access only. /// Handles all channel communication, error handling, and logging. class BCT_API proxy - : public enable_shared_from_base, public track + : public enable_shared_from_base, public reporter { public: DELETE_COPY_MOVE(proxy); diff --git a/include/bitcoin/network/net/socket.hpp b/include/bitcoin/network/net/socket.hpp index 1f5bbde35..7657afd25 100644 --- a/include/bitcoin/network/net/socket.hpp +++ b/include/bitcoin/network/net/socket.hpp @@ -34,7 +34,8 @@ namespace network { /// Stop is thread safe and idempotent, may be called multiple times. /// All handlers (except accept) are posted to the internal strand. class BCT_API socket - : public std::enable_shared_from_this, public track + : public std::enable_shared_from_this, public reporter, + protected tracker { public: DELETE_COPY_MOVE(socket); diff --git a/include/bitcoin/network/p2p.hpp b/include/bitcoin/network/p2p.hpp index c660155a2..64819b05a 100644 --- a/include/bitcoin/network/p2p.hpp +++ b/include/bitcoin/network/p2p.hpp @@ -41,7 +41,7 @@ namespace network { /// * attach must be called from channel strand. /// * close must not be called concurrently or from any threadpool thread. class BCT_API p2p - : public enable_shared_from_base + : public enable_shared_from_base, public reporter, protected tracker { public: typedef std::shared_ptr ptr; @@ -139,9 +139,6 @@ class BCT_API p2p /// Network configuration settings. const settings& network_settings() const NOEXCEPT; - /// Return a logging instance. - const logger& log() const NOEXCEPT; - /// Return a reference to the network io_context (thread safe). asio::io_context& service() NOEXCEPT; diff --git a/include/bitcoin/network/protocols/protocol.hpp b/include/bitcoin/network/protocols/protocol.hpp index 39d81ff8d..5372b5fc3 100644 --- a/include/bitcoin/network/protocols/protocol.hpp +++ b/include/bitcoin/network/protocols/protocol.hpp @@ -55,7 +55,7 @@ class session; /// Abstract base class for protocols. /// handle_ methods are always invoked on the strand. class BCT_API protocol - : public enable_shared_from_base + : public enable_shared_from_base, public reporter { public: DELETE_COPY_MOVE(protocol); @@ -154,9 +154,6 @@ class BCT_API protocol /// Network settings. const network::settings& settings() const NOEXCEPT; - /// Return a logging instance. - const logger& log() const NOEXCEPT; - /// Addresses. /// ----------------------------------------------------------------------- diff --git a/include/bitcoin/network/protocols/protocol_address_31402.hpp b/include/bitcoin/network/protocols/protocol_address_31402.hpp index 2798568e1..7001cbb27 100644 --- a/include/bitcoin/network/protocols/protocol_address_31402.hpp +++ b/include/bitcoin/network/protocols/protocol_address_31402.hpp @@ -34,7 +34,7 @@ namespace network { class session; class BCT_API protocol_address_31402 - : public protocol, public track + : public protocol, protected tracker { public: typedef std::shared_ptr ptr; diff --git a/include/bitcoin/network/protocols/protocol_ping_31402.hpp b/include/bitcoin/network/protocols/protocol_ping_31402.hpp index b77f102d6..25b5fe573 100644 --- a/include/bitcoin/network/protocols/protocol_ping_31402.hpp +++ b/include/bitcoin/network/protocols/protocol_ping_31402.hpp @@ -34,7 +34,7 @@ namespace network { class session; class BCT_API protocol_ping_31402 - : public protocol, public track + : public protocol, protected tracker { public: typedef std::shared_ptr ptr; diff --git a/include/bitcoin/network/protocols/protocol_ping_60001.hpp b/include/bitcoin/network/protocols/protocol_ping_60001.hpp index 6abae3bb7..a1d3bdc3b 100644 --- a/include/bitcoin/network/protocols/protocol_ping_60001.hpp +++ b/include/bitcoin/network/protocols/protocol_ping_60001.hpp @@ -35,7 +35,7 @@ namespace network { class session; class BCT_API protocol_ping_60001 - : public protocol_ping_31402, public track + : public protocol_ping_31402, protected tracker { public: typedef std::shared_ptr ptr; diff --git a/include/bitcoin/network/protocols/protocol_reject_70002.hpp b/include/bitcoin/network/protocols/protocol_reject_70002.hpp index c052382f3..f8677119c 100644 --- a/include/bitcoin/network/protocols/protocol_reject_70002.hpp +++ b/include/bitcoin/network/protocols/protocol_reject_70002.hpp @@ -34,7 +34,7 @@ namespace network { class session; class BCT_API protocol_reject_70002 - : public protocol, public track + : public protocol, protected tracker { public: typedef std::shared_ptr ptr; diff --git a/include/bitcoin/network/protocols/protocol_seed_31402.hpp b/include/bitcoin/network/protocols/protocol_seed_31402.hpp index 67ae3a85b..08b6f227f 100644 --- a/include/bitcoin/network/protocols/protocol_seed_31402.hpp +++ b/include/bitcoin/network/protocols/protocol_seed_31402.hpp @@ -34,7 +34,7 @@ namespace network { class session; class BCT_API protocol_seed_31402 - : public protocol, public track + : public protocol, protected tracker { public: typedef std::shared_ptr ptr; diff --git a/include/bitcoin/network/protocols/protocol_version_31402.hpp b/include/bitcoin/network/protocols/protocol_version_31402.hpp index 3d314ab9e..f572bd719 100644 --- a/include/bitcoin/network/protocols/protocol_version_31402.hpp +++ b/include/bitcoin/network/protocols/protocol_version_31402.hpp @@ -36,7 +36,7 @@ namespace network { class session; class BCT_API protocol_version_31402 - : public protocol, public track + : public protocol, protected tracker { public: typedef std::shared_ptr ptr; diff --git a/include/bitcoin/network/protocols/protocol_version_70001.hpp b/include/bitcoin/network/protocols/protocol_version_70001.hpp index 95a16efde..96c166a52 100644 --- a/include/bitcoin/network/protocols/protocol_version_70001.hpp +++ b/include/bitcoin/network/protocols/protocol_version_70001.hpp @@ -35,7 +35,7 @@ namespace network { class session; class BCT_API protocol_version_70001 - : public protocol_version_31402, public track + : public protocol_version_31402, protected tracker { public: typedef std::shared_ptr ptr; diff --git a/include/bitcoin/network/protocols/protocol_version_70002.hpp b/include/bitcoin/network/protocols/protocol_version_70002.hpp index dd7a7fc0b..49a6bc5a4 100644 --- a/include/bitcoin/network/protocols/protocol_version_70002.hpp +++ b/include/bitcoin/network/protocols/protocol_version_70002.hpp @@ -35,7 +35,7 @@ namespace network { class session; class BCT_API protocol_version_70002 - : public protocol_version_70001, public track + : public protocol_version_70001, protected tracker { public: typedef std::shared_ptr ptr; diff --git a/include/bitcoin/network/sessions/session.hpp b/include/bitcoin/network/sessions/session.hpp index d9014559c..3f8007598 100644 --- a/include/bitcoin/network/sessions/session.hpp +++ b/include/bitcoin/network/sessions/session.hpp @@ -54,7 +54,7 @@ class p2p; /// Abstract base class for maintaining a channel set, thread safe. class BCT_API session - : public enable_shared_from_base + : public enable_shared_from_base, public reporter { public: DELETE_COPY_MOVE(session); @@ -92,9 +92,6 @@ class BCT_API session /// Access network configuration settings. const network::settings& settings() const NOEXCEPT; - /// Return a logging instance. - const logger& log() const NOEXCEPT; - protected: typedef subscriber stop_subscriber; diff --git a/include/bitcoin/network/sessions/session_inbound.hpp b/include/bitcoin/network/sessions/session_inbound.hpp index cd81027d9..e8722b9a1 100644 --- a/include/bitcoin/network/sessions/session_inbound.hpp +++ b/include/bitcoin/network/sessions/session_inbound.hpp @@ -35,7 +35,7 @@ class p2p; /// Inbound connections session, thread safe. class BCT_API session_inbound - : public session, public track + : public session, protected tracker { public: typedef std::shared_ptr ptr; diff --git a/include/bitcoin/network/sessions/session_manual.hpp b/include/bitcoin/network/sessions/session_manual.hpp index bae35956e..42cef8e87 100644 --- a/include/bitcoin/network/sessions/session_manual.hpp +++ b/include/bitcoin/network/sessions/session_manual.hpp @@ -37,7 +37,7 @@ class p2p; /// Manual connections session, thread safe. class BCT_API session_manual - : public session, public track + : public session, protected tracker { public: typedef std::shared_ptr ptr; diff --git a/include/bitcoin/network/sessions/session_outbound.hpp b/include/bitcoin/network/sessions/session_outbound.hpp index 466f3bf58..29d30e174 100644 --- a/include/bitcoin/network/sessions/session_outbound.hpp +++ b/include/bitcoin/network/sessions/session_outbound.hpp @@ -34,7 +34,7 @@ class p2p; /// Outbound connections session, thread safe. class BCT_API session_outbound - : public session, public track + : public session, protected tracker { public: typedef std::shared_ptr ptr; diff --git a/include/bitcoin/network/sessions/session_seed.hpp b/include/bitcoin/network/sessions/session_seed.hpp index 18926d1b9..7dbc5cffd 100644 --- a/include/bitcoin/network/sessions/session_seed.hpp +++ b/include/bitcoin/network/sessions/session_seed.hpp @@ -36,7 +36,7 @@ class p2p; /// Seed connections session, thread safe. class BCT_API session_seed - : public session, public track + : public session, protected tracker { public: typedef std::shared_ptr ptr; diff --git a/src/async/deadline.cpp b/src/async/deadline.cpp index 500066c58..985be5455 100644 --- a/src/async/deadline.cpp +++ b/src/async/deadline.cpp @@ -39,7 +39,7 @@ deadline::deadline(const logger& log, asio::strand& strand, const duration& timeout) NOEXCEPT : duration_(timeout), timer_(strand), - track(log) + tracker(log) { } diff --git a/src/async/logger.cpp b/src/async/logger.cpp index 9eff106ba..8c6323c29 100644 --- a/src/async/logger.cpp +++ b/src/async/logger.cpp @@ -35,24 +35,16 @@ logger::logger() NOEXCEPT std::ostream& logger::write() const NOEXCEPT { -#ifdef HAVE_MSC BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT) return system::cerr_stream(); BC_POP_WARNING() -#else - return std::cout; -#endif } std::ostream& logger::error() const NOEXCEPT { -#ifdef HAVE_MSC BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT) return system::cerr_stream(); BC_POP_WARNING() -#else - return std::cerr; -#endif } } // namespace network diff --git a/src/async/reporter.cpp b/src/async/reporter.cpp new file mode 100644 index 000000000..d3f2a4824 --- /dev/null +++ b/src/async/reporter.cpp @@ -0,0 +1,38 @@ +/** + * Copyright (c) 2011-2019 libbitcoin developers (see AUTHORS) + * + * This file is part of libbitcoin. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +#include + +#include +#include + +namespace libbitcoin { +namespace network { + +reporter::reporter(const logger& log) NOEXCEPT + : log_(log) +{ +} + +const logger& reporter::log() const NOEXCEPT +{ + return log_; +} + +} // namespace network +} // namespace libbitcoin diff --git a/src/net/acceptor.cpp b/src/net/acceptor.cpp index 5d011b99e..6d37c032a 100644 --- a/src/net/acceptor.cpp +++ b/src/net/acceptor.cpp @@ -49,7 +49,8 @@ acceptor::acceptor(const logger& log, asio::strand& strand, strand_(strand), acceptor_(strand_), stopped_(true), - track(log) + reporter(log), + tracker(log) { } @@ -109,7 +110,7 @@ void acceptor::accept(accept_handler&& handler) NOEXCEPT return; } - const auto socket = std::make_shared(get_log(), service_); + const auto socket = std::make_shared(log(), service_); // Posts handle_accept to strand. // This does not post to the socket strand, unlike other socket calls. @@ -144,7 +145,7 @@ void acceptor::handle_accept(const code& ec, const socket::ptr& socket, return; } - const auto channel = std::make_shared(get_log(), socket, + const auto channel = std::make_shared(log(), socket, settings_); // Successful accept. diff --git a/src/net/channel.cpp b/src/net/channel.cpp index 24969d1a5..1ca37d393 100644 --- a/src/net/channel.cpp +++ b/src/net/channel.cpp @@ -112,7 +112,7 @@ channel::channel(const logger& log, const socket::ptr& socket, peer_version_(to_shared()), expiration_(expiration(log, socket->strand(), settings.channel_expiration())), inactivity_(timeout(log, socket->strand(), settings.channel_inactivity())), - track(log) + tracker(log) { } @@ -249,7 +249,7 @@ void channel::handle_expiration(const code& ec) NOEXCEPT if (ec) { - track::get_log().write() + log().write() << "Channel lifetime timer failure [" << authority() << "] " << ec.message() << std::endl; @@ -257,7 +257,7 @@ void channel::handle_expiration(const code& ec) NOEXCEPT return; } - track::get_log().write() + log().write() << "Channel lifetime expired [" << authority() << "]" << std::endl; stop(ec); @@ -286,7 +286,7 @@ void channel::handle_inactivity(const code& ec) NOEXCEPT if (ec) { - track::get_log().write() + log().write() << "Channel inactivity timer failure [" << authority() << "] " << ec.message() << std::endl; @@ -294,7 +294,7 @@ void channel::handle_inactivity(const code& ec) NOEXCEPT return; } - track::get_log().write() + log().write() << "Channel inactivity timeout [" << authority() << "]" << std::endl; stop(ec); diff --git a/src/net/connector.cpp b/src/net/connector.cpp index c191527e9..de0e00bee 100644 --- a/src/net/connector.cpp +++ b/src/net/connector.cpp @@ -48,7 +48,8 @@ connector::connector(const logger& log, asio::strand& strand, timer_(std::make_shared(log, strand_, settings_.connect_timeout())), resolver_(strand_), stopped_(true), - track(log) + reporter(log), + tracker(log) { } @@ -90,7 +91,7 @@ void connector::connect(const std::string& hostname, uint16_t port, // This allows connect after stop (restartable). stopped_ = false; - const auto socket = std::make_shared(get_log(), service_); + const auto socket = std::make_shared(log(), service_); // Posts timer handler to strand. // The handler is copied by std::bind. @@ -178,7 +179,7 @@ void connector::do_handle_connect(const code& ec, socket::ptr socket, return; } - const auto channel = std::make_shared(get_log(), socket, + const auto channel = std::make_shared(log(), socket, settings_); // Successful connect. diff --git a/src/net/hosts.cpp b/src/net/hosts.cpp index 4a95fdfb8..d980017aa 100644 --- a/src/net/hosts.cpp +++ b/src/net/hosts.cpp @@ -53,7 +53,8 @@ hosts::hosts(const logger& log, const settings& settings) NOEXCEPT file_path_(settings.hosts_file), buffer_(std::max(capacity_, one)), stopped_(true), - track(log) + reporter(log), + tracker(log) { } @@ -82,7 +83,7 @@ code hosts::start() NOEXCEPT // An invalid path/non-existent file will not cause an error on open. if (!file.good()) { - get_log().write() << "Failed to load hosts file." << std::endl; + log().write() << "Failed to load hosts file." << std::endl; return error::file_load; } @@ -115,7 +116,7 @@ code hosts::stop() NOEXCEPT if (!file.good()) { - get_log().write() << "Failed to store hosts file." << std::endl; + log().write() << "Failed to store hosts file." << std::endl; return error::file_load; } @@ -129,7 +130,7 @@ code hosts::stop() NOEXCEPT // An invalid path or non-existent file will cause an error on write. if (file.bad()) { - get_log().write() << "Failed to store hosts." << std::endl; + log().write() << "Failed to store hosts." << std::endl; return error::file_load; } @@ -146,7 +147,7 @@ void hosts::store(const address_item& host) NOEXCEPT // Do not treat invalid address as an error, just log it. if (is_invalid(host)) { - get_log().write() << "Invalid host address from peer." << std::endl; + log().write() << "Invalid host address from peer." << std::endl; return; } @@ -183,7 +184,7 @@ void hosts::store(const address_items& hosts) NOEXCEPT // Do not treat invalid address as an error, just log it. if (is_invalid(host)) { - get_log().write() << "Invalid host addresses from peer." << std::endl; + log().write() << "Invalid host addresses from peer." << std::endl; continue; } @@ -197,7 +198,7 @@ void hosts::store(const address_items& hosts) NOEXCEPT } } - get_log().write() + log().write() << "Accepted (" << accepted << " of " << hosts.size() << ") host addresses from peer." << std::endl; } @@ -210,7 +211,7 @@ void hosts::remove(const address_item& host) NOEXCEPT const auto it = find(host); if (it == buffer_.end()) { - get_log().write() << "Address to remove not found." << std::endl; + log().write() << "Address to remove not found." << std::endl; return; } diff --git a/src/net/proxy.cpp b/src/net/proxy.cpp index 325aa2763..4bb4d4774 100644 --- a/src/net/proxy.cpp +++ b/src/net/proxy.cpp @@ -49,7 +49,7 @@ proxy::proxy(const socket::ptr& socket) NOEXCEPT stop_subscriber_(std::make_shared(socket->strand())), payload_buffer_(), heading_reader_(heading_buffer_), - track(socket->get_log()) + reporter(socket->log()) { } @@ -175,7 +175,7 @@ void proxy::handle_read_heading(const code& ec, size_t) NOEXCEPT if (ec == error::channel_stopped) { - get_log().write() + log().write() << "Heading read abort [" << authority() << "]" << std::endl; stop(error::success); @@ -184,7 +184,7 @@ void proxy::handle_read_heading(const code& ec, size_t) NOEXCEPT if (ec) { - get_log().write() + log().write() << "Heading read failure [" << authority() << "] " << ec.message() << std::endl; @@ -197,7 +197,7 @@ void proxy::handle_read_heading(const code& ec, size_t) NOEXCEPT if (!heading_reader_) { - get_log().write() + log().write() << "Invalid heading from [" << authority() << "]" << std::endl; stop(error::invalid_heading); @@ -207,7 +207,7 @@ void proxy::handle_read_heading(const code& ec, size_t) NOEXCEPT if (head->magic != protocol_magic()) { // These are common, with magic 542393671 coming from http requests. - get_log().write() + log().write() << "Invalid heading magic (" << head->magic << ") from [" << authority() << "]" << std::endl; @@ -217,7 +217,7 @@ void proxy::handle_read_heading(const code& ec, size_t) NOEXCEPT if (head->payload_size > maximum_payload()) { - get_log().write() + log().write() << "Oversized payload indicated by " << head->command << " heading from [" << authority() << "] (" << head->payload_size << " bytes)" << std::endl; @@ -244,7 +244,7 @@ void proxy::handle_read_payload(const code& ec, size_t payload_size, if (ec == error::channel_stopped) { - get_log().write() + log().write() << "Payload read abort [" << authority() << "]" << std::endl; stop(error::success); @@ -253,7 +253,7 @@ void proxy::handle_read_payload(const code& ec, size_t payload_size, if (ec) { - get_log().write() + log().write() << "Payload read failure [" << authority() << "] " << ec.message() << std::endl; @@ -264,7 +264,7 @@ void proxy::handle_read_payload(const code& ec, size_t payload_size, // This is a pointless test but we allow it as an option for completeness. if (validate_checksum() && !head->verify_checksum(payload_buffer_)) { - get_log().write() + log().write() << "Invalid " << head->command << " payload from [" << authority() << "] bad checksum." << std::endl; @@ -286,14 +286,14 @@ void proxy::handle_read_payload(const code& ec, size_t payload_size, const auto size = std::min(payload_size, invalid_payload_dump_size); const auto begin = payload_buffer_.begin(); - get_log().write() + log().write() << "Invalid payload from [" << authority() << "] " << encode_base16({ begin, std::next(begin, size) }) << std::endl; } else { - get_log().write() + log().write() << "Invalid " << head->command << " payload from [" << authority() << "] " << code.message() << std::endl; } @@ -302,7 +302,7 @@ void proxy::handle_read_payload(const code& ec, size_t payload_size, return; } - get_log().write() + log().write() << "Received " << head->command << " from [" << authority() << "] (" << payload_size << " bytes)" << std::endl; @@ -345,7 +345,7 @@ void proxy::handle_send(const code& ec, size_t, if (ec == error::channel_stopped) { - get_log().write() + log().write() << "Send abort [" << authority() << "]" << std::endl; stop(error::success); @@ -354,7 +354,7 @@ void proxy::handle_send(const code& ec, size_t, if (ec) { - get_log().write() + log().write() << "Failure sending " << extract_command(*payload) << " to [" << authority() << "] (" << payload->size() << " bytes) " << ec.message() << std::endl; @@ -364,7 +364,7 @@ void proxy::handle_send(const code& ec, size_t, return; } - get_log().write() + log().write() << "Sent " << extract_command(*payload) << " to [" << authority() << "] (" << payload->size() << " bytes)" << std::endl; diff --git a/src/net/socket.cpp b/src/net/socket.cpp index f6fa1be82..32ddd75e3 100644 --- a/src/net/socket.cpp +++ b/src/net/socket.cpp @@ -45,7 +45,8 @@ socket::socket(const logger& log, asio::io_context& service) NOEXCEPT : stopped_(false), strand_(service.get_executor()), socket_(strand_), - track(log) + reporter(log), + tracker(log) { } diff --git a/src/p2p.cpp b/src/p2p.cpp index 726585436..45724b0c9 100644 --- a/src/p2p.cpp +++ b/src/p2p.cpp @@ -55,7 +55,9 @@ p2p::p2p(const settings& settings) NOEXCEPT threadpool_(settings_.threads), strand_(threadpool_.service().get_executor()), stop_subscriber_(std::make_shared(strand_)), - channel_subscriber_(std::make_shared(strand_)) + channel_subscriber_(std::make_shared(strand_)), + reporter(log_), + tracker(log_) { BC_ASSERT_MSG(!is_zero(settings.threads), "empty threadpool"); } @@ -339,11 +341,6 @@ const settings& p2p::network_settings() const NOEXCEPT return settings_; } -const logger& p2p::log() const NOEXCEPT -{ - return log_; -} - asio::io_context& p2p::service() NOEXCEPT { return threadpool_.service(); diff --git a/src/protocols/protocol.cpp b/src/protocols/protocol.cpp index 2c51ec1e4..46aebd7ff 100644 --- a/src/protocols/protocol.cpp +++ b/src/protocols/protocol.cpp @@ -41,7 +41,8 @@ using namespace std::placeholders; protocol::protocol(const session& session, const channel::ptr& channel) NOEXCEPT - : channel_(channel), session_(session), started_(false) + : channel_(channel), session_(session), started_(false), + reporter(session.log()) { } @@ -112,11 +113,6 @@ const network::settings& protocol::settings() const NOEXCEPT return session_.settings(); } -const logger& protocol::log() const NOEXCEPT -{ - return session_.log(); -} - bool protocol::stranded() const NOEXCEPT { return channel_->stranded(); diff --git a/src/protocols/protocol_address_31402.cpp b/src/protocols/protocol_address_31402.cpp index a891a31f6..659a1601f 100644 --- a/src/protocols/protocol_address_31402.cpp +++ b/src/protocols/protocol_address_31402.cpp @@ -39,7 +39,7 @@ protocol_address_31402::protocol_address_31402(const session& session, const channel::ptr& channel) NOEXCEPT : protocol(session, channel), sent_(false), - track(session.log()) + tracker(session.log()) { } diff --git a/src/protocols/protocol_ping_31402.cpp b/src/protocols/protocol_ping_31402.cpp index f1521073c..6fdd25492 100644 --- a/src/protocols/protocol_ping_31402.cpp +++ b/src/protocols/protocol_ping_31402.cpp @@ -41,7 +41,7 @@ protocol_ping_31402::protocol_ping_31402(const session& session, : protocol(session, channel), timer_(std::make_shared(session.log(), channel->strand(), session.settings().channel_heartbeat())), - track(session.log()) + tracker(session.log()) { } diff --git a/src/protocols/protocol_ping_60001.cpp b/src/protocols/protocol_ping_60001.cpp index f01b24ce6..8a60d8988 100644 --- a/src/protocols/protocol_ping_60001.cpp +++ b/src/protocols/protocol_ping_60001.cpp @@ -44,7 +44,7 @@ protocol_ping_60001::protocol_ping_60001(const session& session, const channel::ptr& channel) NOEXCEPT : protocol_ping_31402(session, channel), nonce_(received), - track(session.log()) + tracker(session.log()) { } diff --git a/src/protocols/protocol_reject_70002.cpp b/src/protocols/protocol_reject_70002.cpp index 4e93d73fa..bbb474862 100644 --- a/src/protocols/protocol_reject_70002.cpp +++ b/src/protocols/protocol_reject_70002.cpp @@ -41,7 +41,7 @@ using namespace std::placeholders; protocol_reject_70002::protocol_reject_70002(const session& session, const channel::ptr& channel) NOEXCEPT : protocol(session, channel), - track(session.log()) + tracker(session.log()) { } diff --git a/src/protocols/protocol_seed_31402.cpp b/src/protocols/protocol_seed_31402.cpp index 1160b8580..e3bcc7843 100644 --- a/src/protocols/protocol_seed_31402.cpp +++ b/src/protocols/protocol_seed_31402.cpp @@ -46,7 +46,7 @@ protocol_seed_31402::protocol_seed_31402(const session& session, received_address_(false), timer_(std::make_shared(session.log(), channel->strand(), session.settings().channel_germination())), - track(session.log()) + tracker(session.log()) { } diff --git a/src/protocols/protocol_version_31402.cpp b/src/protocols/protocol_version_31402.cpp index 5b91cc019..40b822192 100644 --- a/src/protocols/protocol_version_31402.cpp +++ b/src/protocols/protocol_version_31402.cpp @@ -65,7 +65,7 @@ protocol_version_31402::protocol_version_31402(const session& session, received_acknowledge_(false), timer_(std::make_shared(session.log(), channel->strand(), session.settings().channel_handshake())), - track(session.log()) + tracker(session.log()) { } diff --git a/src/protocols/protocol_version_70001.cpp b/src/protocols/protocol_version_70001.cpp index a908851d7..eb1d30318 100644 --- a/src/protocols/protocol_version_70001.cpp +++ b/src/protocols/protocol_version_70001.cpp @@ -53,7 +53,7 @@ protocol_version_70001::protocol_version_70001(const session& session, : protocol_version_31402(session, channel, minimum_services, maximum_services), relay_(relay), - track(session.log()) + tracker(session.log()) { } diff --git a/src/protocols/protocol_version_70002.cpp b/src/protocols/protocol_version_70002.cpp index 857ffa6c2..08c837e92 100644 --- a/src/protocols/protocol_version_70002.cpp +++ b/src/protocols/protocol_version_70002.cpp @@ -55,7 +55,7 @@ protocol_version_70002::protocol_version_70002(const session& session, uint64_t maximum_services, bool relay) NOEXCEPT : protocol_version_70001(session, channel, minimum_services, maximum_services, relay), - track(session.log()) + tracker(session.log()) { } diff --git a/src/sessions/session.cpp b/src/sessions/session.cpp index 2bc94f5e6..0d42517d9 100644 --- a/src/sessions/session.cpp +++ b/src/sessions/session.cpp @@ -44,7 +44,8 @@ session::session(p2p& network) NOEXCEPT : network_(network), stopped_(true), timer_(std::make_shared(network.log(), network.strand())), - stop_subscriber_(std::make_shared(network.strand())) + stop_subscriber_(std::make_shared(network.strand())), + reporter(network.log()) { } @@ -343,11 +344,6 @@ const network::settings& session::settings() const NOEXCEPT return network_.network_settings(); } -const logger& session::log() const NOEXCEPT -{ - return network_.log(); -} - // Utilities. // ---------------------------------------------------------------------------- diff --git a/src/sessions/session_inbound.cpp b/src/sessions/session_inbound.cpp index b8f3a48c9..07d697740 100644 --- a/src/sessions/session_inbound.cpp +++ b/src/sessions/session_inbound.cpp @@ -34,7 +34,7 @@ using namespace bc::system; using namespace std::placeholders; session_inbound::session_inbound(p2p& network) NOEXCEPT - : session(network), track(network.log()) + : session(network), tracker(network.log()) { } diff --git a/src/sessions/session_manual.cpp b/src/sessions/session_manual.cpp index e33f75eba..157d163b8 100644 --- a/src/sessions/session_manual.cpp +++ b/src/sessions/session_manual.cpp @@ -39,7 +39,7 @@ using namespace config; using namespace std::placeholders; session_manual::session_manual(p2p& network) NOEXCEPT - : session(network), track(network.log()) + : session(network), tracker(network.log()) { } diff --git a/src/sessions/session_outbound.cpp b/src/sessions/session_outbound.cpp index 5e8039492..7cca9d958 100644 --- a/src/sessions/session_outbound.cpp +++ b/src/sessions/session_outbound.cpp @@ -37,7 +37,7 @@ using namespace config; using namespace std::placeholders; session_outbound::session_outbound(p2p& network) NOEXCEPT - : session(network), track(network.log()) + : session(network), tracker(network.log()) { } diff --git a/src/sessions/session_seed.cpp b/src/sessions/session_seed.cpp index a1a5eee32..15db0dbba 100644 --- a/src/sessions/session_seed.cpp +++ b/src/sessions/session_seed.cpp @@ -37,7 +37,7 @@ using namespace bc::system; using namespace std::placeholders; session_seed::session_seed(p2p& network) NOEXCEPT - : session(network), track(network.log()) + : session(network), tracker(network.log()) { } diff --git a/test/async/track.cpp b/test/async/track.cpp index d9403e2e4..d9623ac3c 100644 --- a/test/async/track.cpp +++ b/test/async/track.cpp @@ -20,12 +20,12 @@ BOOST_AUTO_TEST_SUITE(track_tests) -class simple - : track +class tracked + : tracker { public: - simple(const logger& log) NOEXCEPT - : track(log) + tracked(const logger& log) NOEXCEPT + : tracker(log) { } @@ -35,11 +35,33 @@ class simple }; }; +class reported + : protected reporter +{ +public: + reported(const logger& log) NOEXCEPT + : reporter(log) + { + } + + std::ostream& method() const NOEXCEPT + { + return log().write(); + }; +}; + BOOST_AUTO_TEST_CASE(track__construct__always__compiles) { - logger log{}; - simple foo{ log }; + const logger log{}; + tracked foo{ log }; BOOST_REQUIRE(foo.method()); } +BOOST_AUTO_TEST_CASE(report__log__always__good) +{ + const logger log{}; + reported foo{ log }; + BOOST_REQUIRE(foo.method().good()); +} + BOOST_AUTO_TEST_SUITE_END() diff --git a/test/protocols/protocol.cpp b/test/protocols/protocol.cpp index 630b23b07..1af41b64c 100644 --- a/test/protocols/protocol.cpp +++ b/test/protocols/protocol.cpp @@ -113,8 +113,8 @@ class mock_acceptor // Inject mock channel. void accept(accept_handler&& handler) NOEXCEPT override { - const auto socket = std::make_shared(get_log(), service_); - const auto created = std::make_shared(get_log(), socket, settings_); + const auto socket = std::make_shared(log(), service_); + const auto created = std::make_shared(log(), socket, settings_); // Must be asynchronous or is an infinite recursion. // This error code will set the re-listener timer and channel pointer is ignored. @@ -156,8 +156,8 @@ class mock_connector void connect(const std::string&, uint16_t, connect_handler&& handler) NOEXCEPT override { - const auto socket = std::make_shared(get_log(), service_); - const auto created = std::make_shared(get_log(), socket, settings_); + const auto socket = std::make_shared(log(), service_); + const auto created = std::make_shared(log(), socket, settings_); handler(error::success, created); } diff --git a/test/sessions/session_inbound.cpp b/test/sessions/session_inbound.cpp index 62af400c5..2a66ac31d 100644 --- a/test/sessions/session_inbound.cpp +++ b/test/sessions/session_inbound.cpp @@ -103,8 +103,8 @@ class mock_acceptor_start_success_accept_success void accept(accept_handler&& handler) NOEXCEPT override { ++accepts_; - const auto socket = std::make_shared(get_log(), service_); - const auto channel = std::make_shared(get_log(), set_, + const auto socket = std::make_shared(log(), service_); + const auto channel = std::make_shared(log(), set_, coded_, ChannelStopCode, socket, settings_); // Must be asynchronous or is an infinite recursion. diff --git a/test/sessions/session_manual.cpp b/test/sessions/session_manual.cpp index bd09a8ad6..d6b8a51aa 100644 --- a/test/sessions/session_manual.cpp +++ b/test/sessions/session_manual.cpp @@ -63,8 +63,8 @@ class mock_connector_connect_success if (is_zero(connects_++)) peer_ = peer; - const auto socket = std::make_shared(get_log(), service_); - const auto channel = std::make_shared(get_log(), socket, + const auto socket = std::make_shared(log(), service_); + const auto channel = std::make_shared(log(), socket, settings_); // Must be asynchronous or is an infinite recursion. diff --git a/test/sessions/session_outbound.cpp b/test/sessions/session_outbound.cpp index 8329eeaf2..bd77d9669 100644 --- a/test/sessions/session_outbound.cpp +++ b/test/sessions/session_outbound.cpp @@ -122,8 +122,8 @@ class mock_connector_connect_success port_ = port; } - const auto socket = std::make_shared(get_log(), service_); - const auto channel = std::make_shared(get_log(), set_, + const auto socket = std::make_shared(log(), service_); + const auto channel = std::make_shared(log(), set_, coded_, ChannelStopCode, socket, settings_); // Must be asynchronous or is an infinite recursion. diff --git a/test/sessions/session_seed.cpp b/test/sessions/session_seed.cpp index 26f6ef662..602899348 100644 --- a/test/sessions/session_seed.cpp +++ b/test/sessions/session_seed.cpp @@ -115,8 +115,8 @@ class mock_connector_connect_success port_ = port; } - const auto socket = std::make_shared(get_log(), service_); - const auto channel = std::make_shared(get_log(), set_, coded_, + const auto socket = std::make_shared(log(), service_); + const auto channel = std::make_shared(log(), set_, coded_, ChannelStopCode, socket, settings_); // Must be asynchronous or is an infinite recursion.