From ad118aabaed5dc0faccf41df02427e5dc9859ddf Mon Sep 17 00:00:00 2001 From: Marcos Bento Date: Fri, 13 Dec 2024 12:56:35 +0000 Subject: [PATCH] Clean up debug connection information --- libs/base/src/ecflow/base/Connection.cpp | 19 ++++----- libs/base/src/ecflow/base/Connection.hpp | 43 ++++++++----------- libs/base/src/ecflow/base/ssl_connection.cpp | 22 +++++----- libs/base/src/ecflow/base/ssl_connection.hpp | 44 ++++++++------------ 4 files changed, 54 insertions(+), 74 deletions(-) diff --git a/libs/base/src/ecflow/base/Connection.cpp b/libs/base/src/ecflow/base/Connection.cpp index 6a15b07c6..1649617af 100644 --- a/libs/base/src/ecflow/base/Connection.cpp +++ b/libs/base/src/ecflow/base/Connection.cpp @@ -15,34 +15,33 @@ connection::~connection() { #ifdef DEBUG_CONNECTION - if (Ecf::server()) - std::cout << "SERVER: Connection::~connection socket_.is_open() = " << socket_.is_open() << "\n\n"; - else - std::cout << "CLIENT: Connection::~connection socket_.is_open() = " << socket_.is_open() << "\n\n"; + auto is_socket_open = socket_.is_open(); + auto location = Ecf::server() ? "SERVER" : "CLIENT"; + std::cout << location << ": Connection::~connection socket_.is_open() = " << is_socket_open << "\n\n"; #endif } connection::connection(boost::asio::io_context& io) : socket_(io) { #ifdef DEBUG_CONNECTION - if (Ecf::server()) - std::cout << "SERVER: Connection::connection\n"; - else - std::cout << "CLIENT: Connection::connection\n"; + auto location = Ecf::server() ? "SERVER" : "CLIENT"; + std::cout << location << ": Connection::connection\n"; #endif } void connection::log_error(const char* msg) { const char* in_context = ", in client"; - if (Ecf::server()) + if (Ecf::server()) { in_context = ", in server"; + } ecf::LogToCout logToCout; LOG(ecf::Log::ERR, msg << in_context); } void connection::log_archive_error(const char* msg, const std::exception& ae, const std::string& data) { const char* in_context = ", in client"; - if (Ecf::server()) + if (Ecf::server()) { in_context = ", in server"; + } ecf::LogToCout logToCout; LOG(ecf::Log::ERR, msg << ae.what() << in_context << " data:\n" << data); } diff --git a/libs/base/src/ecflow/base/Connection.hpp b/libs/base/src/ecflow/base/Connection.hpp index 4d5043d57..ee0cec743 100644 --- a/libs/base/src/ecflow/base/Connection.hpp +++ b/libs/base/src/ecflow/base/Connection.hpp @@ -54,11 +54,9 @@ class connection { void async_write(const T& t, Handler handler) { #ifdef DEBUG_CONNECTION - if (Ecf::server()) - std::cout << "SERVER: Connection::async_write\n"; - else - std::cout << "CLIENT: Connection::async_write\n"; - std::cout << " Serialise the data first so we know how large it is\n"; + auto location = Ecf::server() ? "SERVER" : "CLIENT"; + std::cout << location << ": Connection::async_write\n" + << " Serialise the data first so we know how large it is\n"; #endif // Serialise the data first so we know how large it is. try { @@ -86,11 +84,11 @@ class connection { outbound_header_ = header_stream.str(); #ifdef DEBUG_CONNECTION - std::cout << " Write the HEADER and serialised DATA to the socket\n"; - std::cout << " outbound_header_.size(" << outbound_header_.size() << ")\n"; - std::cout << " outbound_header_:'" << outbound_header_ << "' # this is the size in hex\n"; - std::cout << " outbound_data_.size(" << outbound_data_.size() << ")\n"; - std::cout << " hdr+data:'" << outbound_header_ << outbound_data_ << "'\n"; + std::cout << " Write the HEADER and serialised DATA to the socket\n" + << " outbound_header_.size(" << outbound_header_.size() << ")\n" + << " outbound_header_:'" << outbound_header_ << "' # this is the size in hex\n" + << " outbound_data_.size(" << outbound_data_.size() << ")\n" + << " hdr+data:'" << outbound_header_ << outbound_data_ << "'\n"; #endif // Write the serialized data to the socket. We use "gather-write" to send // both the header and the data in a single write operation. @@ -113,10 +111,8 @@ class connection { void async_read(T& t, Handler handler) { #ifdef DEBUG_CONNECTION - if (Ecf::server()) - std::cout << "SERVER: Connection::async_read\n"; - else - std::cout << "CLIENT: Connection::async_read\n"; + auto location = Ecf::server() ? "SERVER" : "CLIENT"; + std::cout << location << ": Connection::async_read\n"; #endif // Issue a read operation to read exactly the number of bytes in a header @@ -133,11 +129,9 @@ class connection { template void handle_read_header(const boost::system::error_code& e, T& t, Handler handler) { #ifdef DEBUG_CONNECTION - if (Ecf::server()) - std::cout << "SERVER: Connection::handle_read_header\n"; - else - std::cout << "CLIENT: Connection::handle_read_header\n"; - std::cout << " header:'" << std::string(inbound_header_, header_length) + auto location = Ecf::server() ? "SERVER" : "CLIENT"; + std::cout << location << ": Connection::handle_read_header\n" + << " header:'" << std::string(inbound_header_, header_length) << "' # this size of payload in hex\n"; #endif if (e) { @@ -172,10 +166,8 @@ class connection { template void handle_read_data(const boost::system::error_code& e, T& t, Handler handler) { #ifdef DEBUG_CONNECTION - if (Ecf::server()) - std::cout << "SERVER: Connection::handle_read_data\n"; - else - std::cout << "CLIENT: Connection::handle_read_data\n"; + auto location = Ecf::server() ? "SERVER" : "CLIENT"; + std::cout << location << ": Connection::handle_read_data\n"; #endif if (e) { @@ -186,9 +178,8 @@ class connection { std::string archive_data(&inbound_data_[0], inbound_data_.size()); try { #ifdef DEBUG_CONNECTION - std::cout << " inbound_data_.size(" << inbound_data_.size() << ") typeid(" << typeid(t).name() - << ")\n"; - std::cout << " '" << archive_data << "'\n"; + std::cout << " inbound_data_.size(" << inbound_data_.size() << ") typeid=" << typeid(t).name() << "\n" + << " '" << archive_data << "'\n"; #endif ecf::restore_from_string(archive_data, t); } diff --git a/libs/base/src/ecflow/base/ssl_connection.cpp b/libs/base/src/ecflow/base/ssl_connection.cpp index 25e5583fa..d1ec1ae18 100644 --- a/libs/base/src/ecflow/base/ssl_connection.cpp +++ b/libs/base/src/ecflow/base/ssl_connection.cpp @@ -15,20 +15,16 @@ ssl_connection::~ssl_connection() { #ifdef DEBUG_CONNECTION - if (Ecf::server()) - std::cout << "SERVER: ssl_connection::~ssl_connection socket_.is_open() = " << socket_.is_open() << "\n\n"; - else - std::cout << "CLIENT: ssl_connection::~ssl_connection socket_.is_open() = " << socket_.is_open() << "\n\n"; + auto is_socket_open = socket_.lowest_layer().is_open(); + auto location = Ecf::server() ? "SERVER" : "CLIENT"; + std::cout << location << ": ssl_connection::~ssl_connection socket_.is_open() = " << is_socket_open << "\n\n"; #endif } -ssl_connection::ssl_connection(boost::asio::io_context& io, boost::asio::ssl::context& context) - : socket_(io, context) { +ssl_connection::ssl_connection(boost::asio::io_context& io, boost::asio::ssl::context& context) : socket_(io, context) { #ifdef DEBUG_CONNECTION - if (Ecf::server()) - std::cout << "SERVER: ssl_connection::ssl_connection\n"; - else - std::cout << "CLIENT: ssl_connection::ssl_connection\n"; + auto location = Ecf::server() ? "SERVER" : "CLIENT"; + std::cout << location << ": ssl_connection::ssl_connection\n"; #endif socket_.set_verify_mode(boost::asio::ssl::verify_peer); @@ -61,16 +57,18 @@ bool ssl_connection::verify_certificate(bool preverified, boost::asio::ssl::veri void ssl_connection::log_error(const char* msg) { const char* in_context = ", in client"; - if (Ecf::server()) + if (Ecf::server()) { in_context = ", in server"; + } ecf::LogToCout logToCout; LOG(ecf::Log::ERR, msg << in_context); } void ssl_connection::log_archive_error(const char* msg, const std::exception& ae, const std::string& data) { const char* in_context = ", in client"; - if (Ecf::server()) + if (Ecf::server()) { in_context = ", in server"; + } ecf::LogToCout logToCout; LOG(ecf::Log::ERR, msg << ae.what() << in_context << " data:\n" << data); } diff --git a/libs/base/src/ecflow/base/ssl_connection.hpp b/libs/base/src/ecflow/base/ssl_connection.hpp index f9d47c82c..251655822 100644 --- a/libs/base/src/ecflow/base/ssl_connection.hpp +++ b/libs/base/src/ecflow/base/ssl_connection.hpp @@ -62,11 +62,9 @@ class ssl_connection { void async_write(const T& t, Handler handler) { #ifdef DEBUG_CONNECTION - if (Ecf::server()) - std::cout << "SERVER: ssl_connection::async_write\n"; - else - std::cout << "CLIENT: ssl_connection::async_write\n"; - std::cout << " Serialise the data first so we know how large it is\n"; + auto location = Ecf::server() ? "SERVER" : "CLIENT"; + std::cout << location << ": ssl_connection::async_write\n" + << " Serialise the data first so we know how large it is\n"; #endif // Serialise the data first so we know how large it is. try { @@ -96,11 +94,11 @@ class ssl_connection { outbound_header_ = header_stream.str(); #ifdef DEBUG_CONNECTION - std::cout << " Write the HEADER and serialised DATA to the socket\n"; - std::cout << " outbound_header_.size(" << outbound_header_.size() << ")\n"; - std::cout << " outbound_header_:'" << outbound_header_ << "' # this is the size in hex\n"; - std::cout << " outbound_data_.size(" << outbound_data_.size() << ")\n"; - std::cout << " hdr+data:'" << outbound_header_ << outbound_data_ << "'\n"; + std::cout << " Write the HEADER and serialised DATA to the socket\n" + << " outbound_header_.size(" << outbound_header_.size() << ")\n" + << " outbound_header_:'" << outbound_header_ << "' # this is the size in hex\n" + << " outbound_data_.size(" << outbound_data_.size() << ")\n" + << " hdr+data:'" << outbound_header_ << outbound_data_ << "'\n"; #endif // Write the serialized data to the socket. We use "gather-write" to send @@ -124,10 +122,8 @@ class ssl_connection { void async_read(T& t, Handler handler) { #ifdef DEBUG_CONNECTION - if (Ecf::server()) - std::cout << "SERVER: ssl_connection::async_read\n"; - else - std::cout << "CLIENT: ssl_connection::async_read\n"; + auto location = Ecf::server() ? "SERVER" : "CLIENT"; + std::cout << location << ": ssl_connection::async_read\n"; #endif // Issue a read operation to read exactly the number of bytes in a header. @@ -144,11 +140,9 @@ class ssl_connection { template void handle_read_header(const boost::system::error_code& e, T& t, Handler handler) { #ifdef DEBUG_CONNECTION - if (Ecf::server()) - std::cout << "SERVER: ssl_onnection::handle_read_header\n"; - else - std::cout << "CLIENT: ssl_connection::handle_read_header\n"; - std::cout << " header:'" << std::string(inbound_header_, header_length) + auto location = Ecf::server() ? "SERVER" : "CLIENT"; + std::cout << location << ": ssl_connection::handle_read_header\n" + << " header:'" << std::string(inbound_header_, header_length) << "' # this size of payload in hex\n"; #endif if (e) { @@ -183,10 +177,8 @@ class ssl_connection { template void handle_read_data(const boost::system::error_code& e, T& t, Handler handler) { #ifdef DEBUG_CONNECTION - if (Ecf::server()) - std::cout << "SERVER: ssl_connection::handle_read_data\n"; - else - std::cout << "CLIENT: ssl_connection::handle_read_data\n"; + auto location = Ecf::server() ? "SERVER" : "CLIENT"; + std::cout << location << ": ssl_connection::handle_read_data\n"; #endif if (e) { @@ -197,9 +189,9 @@ class ssl_connection { std::string archive_data(&inbound_data_[0], inbound_data_.size()); try { #ifdef DEBUG_CONNECTION - std::cout << " inbound_data_.size(" << inbound_data_.size() << ") typeid(" << typeid(t).name() - << ")\n"; - std::cout << " '" << archive_data << "'\n"; + auto tname = typeid(t).name(); + std::cout << " inbound_data_.size(" << inbound_data_.size() << ") typeid=" << tname << "\n" + << " '" << archive_data << "'\n"; #endif ecf::restore_from_string(archive_data, t); }