Skip to content

Commit

Permalink
Update logs in server and client
Browse files Browse the repository at this point in the history
  • Loading branch information
janekbaraniewski committed Apr 28, 2024
1 parent 8bf5c76 commit 0c00592
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>

#include <boost/asio.hpp>
#include <boost/program_options.hpp>
#include <boost/log/core.hpp>
Expand Down
33 changes: 30 additions & 3 deletions src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
#include <boost/asio.hpp>
#include <boost/array.hpp>
#include <boost/program_options.hpp>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/support/date_time.hpp>

#include "logging.h"
#include "ISerialPort.h"
Expand All @@ -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<boost::log::trivial::severity_level, char>("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();
}

Expand All @@ -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();
Expand All @@ -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();
}
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 0c00592

Please sign in to comment.