diff --git a/src/client.cpp b/src/client.cpp index e02e6ed..24ecefe 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -3,6 +3,7 @@ #include #include #include + #include #include #include diff --git a/src/server.cpp b/src/server.cpp index 1da650b..14044a5 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -7,6 +7,15 @@ #include #include #include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include "logging.h" #include "ISerialPort.h" @@ -19,8 +28,25 @@ using ip::tcp; using std::string; using std::endl; +void init_logging() { + 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_server_%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"); +} + SerialServer::SerialServer(io_service& io, ISerialPort& serial, tcp::acceptor& acceptor) : io_service_(io), serial_(serial), acceptor_(acceptor), socket_(io) { + BOOST_LOG_TRIVIAL(info) << "Starting server and waiting for connection..."; start_accept(); } @@ -33,7 +59,7 @@ void SerialServer::run() { void SerialServer::start_accept() { acceptor_.async_accept(socket_, [this](boost::system::error_code ec) { if (!ec) { - BOOST_LOG_TRIVIAL(info) << "Client connected."; + BOOST_LOG_TRIVIAL(info) << "Client connected. Starting to handle read/write operations."; do_read_write(); } else { BOOST_LOG_TRIVIAL(error) << "Error accepting connection: " << ec.message(); @@ -47,7 +73,8 @@ void SerialServer::do_read_write() { if (!ec) { async_write(socket_, boost::asio::buffer(buf, length), [this](boost::system::error_code ec, std::size_t) { if (!ec) { - do_read_write(); // Loop back to continue reading/writing + BOOST_LOG_TRIVIAL(info) << "Data successfully written to client. Continuing read/write loop."; + do_read_write(); } else { BOOST_LOG_TRIVIAL(error) << "Error writing to client: " << ec.message(); } @@ -93,7 +120,7 @@ int main(int argc, char* argv[]) { server.run(); } catch (const std::exception& e) { BOOST_LOG_TRIVIAL(error) << "Exception: " << e.what(); - return 1; + return 1; // Ensure returning a non-zero value on error } return 0; }