From a768b1abad4dd31d3ddfb26573ff8468c8d0d95e Mon Sep 17 00:00:00 2001 From: Jan Baraniewski Date: Sun, 28 Apr 2024 19:44:33 +0200 Subject: [PATCH] Add more logs to client --- src/client.cpp | 55 +++++++++++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/src/client.cpp b/src/client.cpp index fbb4b12..cb1b985 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -13,8 +13,7 @@ #include #include #include -#include // Ensure this is included for date-time support - +#include #include "logging.h" #include "VirtualSerialPort.h" @@ -28,17 +27,21 @@ using std::cerr; using std::endl; void init_logging() { - boost::log::register_simple_formatter_factory("Severity"); - boost::log::add_console_log(std::cout, boost::log::keywords::format = "[%TimeStamp%] [%ThreadID%] [%Severity%] %Message%"); - boost::log::add_file_log(boost::log::keywords::file_name = "serial_client_%N.log", - boost::log::keywords::rotation_size = 10 * 1024 * 1024, - boost::log::keywords::time_based_rotation = boost::log::sinks::file::rotation_at_time_point(0, 0, 0), - boost::log::keywords::format = "[%TimeStamp%] [%Severity%] %Message%"); + boost::log::add_console_log( + std::cout, + boost::log::keywords::format = "[%TimeStamp%] [%ThreadID%] [%Severity%] %Message%", + boost::log::keywords::auto_flush = true + ); + boost::log::add_file_log( + boost::log::keywords::file_name = "serial_client_%N.log", + boost::log::keywords::rotation_size = 10 * 1024 * 1024, + boost::log::keywords::time_based_rotation = boost::log::sinks::file::rotation_at_time_point(0, 0, 0), + boost::log::keywords::format = "[%TimeStamp%] [%Severity%] %Message%" + ); boost::log::add_common_attributes(); + boost::log::register_simple_formatter_factory("Severity"); } - - class SerialClient { private: io_service io_service_; @@ -47,16 +50,20 @@ class SerialClient { VirtualSerialPort vsp_; public: - SerialClient(const std::string& server_ip, unsigned short server_port, const std::string& vsp_name) + SerialClient(const string& server_ip, unsigned short server_port, const string& vsp_name) : socket_(io_service_), vsp_(vsp_name) { + BOOST_LOG_TRIVIAL(info) << "Initializing client..."; tcp::resolver resolver(io_service_); auto endpoint_iterator = resolver.resolve({server_ip, std::to_string(server_port)}); + BOOST_LOG_TRIVIAL(info) << "Connecting to server at " << server_ip << ":" << server_port; connect(socket_, endpoint_iterator); - - // vsp_.open(); // Ensure the virtual port is ready for use + BOOST_LOG_TRIVIAL(info) << "Connected to server."; + BOOST_LOG_TRIVIAL(info) << "Opening virtual serial port: " << vsp_name; + vsp_.open(); } void run() { + BOOST_LOG_TRIVIAL(info) << "Starting client I/O operations."; do_read_write(); io_service_.run(); } @@ -65,14 +72,16 @@ class SerialClient { void do_read_write() { socket_.async_read_some(boost::asio::buffer(buffer_), [this](boost::system::error_code ec, std::size_t length) { if (!ec) { - std::string data(buffer_.begin(), buffer_.begin() + length); - if (!vsp_.write(data)) { - cerr << "Write to virtual serial port failed" << endl; - return; + string data(buffer_.data(), length); + BOOST_LOG_TRIVIAL(info) << "Received data: " << data; + if (vsp_.write(data)) { + BOOST_LOG_TRIVIAL(info) << "Data written to virtual serial port."; + } else { + BOOST_LOG_TRIVIAL(error) << "Failed to write to virtual serial port."; } - do_read_write(); // Continue reading + do_read_write(); } else { - cerr << "Read error: " << ec.message() << endl; + BOOST_LOG_TRIVIAL(error) << "Read error: " << ec.message(); } }); } @@ -85,9 +94,9 @@ int main(int argc, char* argv[]) { options_description desc{"Options"}; desc.add_options() ("help,h", "Help screen") - ("server,s", value()->default_value("127.0.0.1"), "Server IP address") + ("server,s", value()->default_value("127.0.0.1"), "Server IP address") ("port,p", value()->default_value(12345), "Server port") - ("vsp,v", value()->required(), "Virtual serial port name"); + ("vsp,v", value()->required(), "Virtual serial port name"); variables_map vm; store(parse_command_line(argc, argv, desc), vm); @@ -98,9 +107,9 @@ int main(int argc, char* argv[]) { return 0; } - std::string server_ip = vm["server"].as(); + string server_ip = vm["server"].as(); unsigned short server_port = vm["port"].as(); - std::string vsp_name = vm["vsp"].as(); + string vsp_name = vm["vsp"].as(); SerialClient client(server_ip, server_port, vsp_name); client.run();