From c07d63bf5a3f128503c8a02ddd6fb0f717d89b54 Mon Sep 17 00:00:00 2001 From: Kilemonn Date: Mon, 14 Oct 2024 14:57:44 +0900 Subject: [PATCH 01/13] Add log4cxx, include and make sure we can build with the new dependency. --- CMakeLists.txt | 13 +++++++++++++ Dockerfile | 2 +- socket-forwarder/main.cpp | 7 ++++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3179414..2b87c15 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,17 @@ ExternalProject_Add(CppSocketLibrary ExternalProject_Get_Property(CppSocketLibrary SOURCE_DIR) set(SOCKET_LIB_SOURCE ${SOURCE_DIR}) +ExternalProject_Add(log4cxx + GIT_REPOSITORY https://github.com/apache/logging-log4cxx.git + GIT_TAG rel/v1.2.0 + INSTALL_COMMAND "" + UPDATE_DISCONNECTED FALSE + BUILD_IN_SOURCE TRUE +) + +ExternalProject_Get_Property(log4cxx SOURCE_DIR) +set(LOG_LIB_SOURCE ${SOURCE_DIR}) + set(FORWARDER_SOURCE socket-forwarder/main.cpp socket-forwarder/environment/Environment.cpp @@ -30,10 +41,12 @@ add_executable(SocketForwarder ${FORWARDER_SOURCE}) target_include_directories(SocketForwarder PUBLIC ${SOCKET_LIB_SOURCE}/src + PUBLIC ${LOG_LIB_SOURCE}/src/main/include ) target_link_libraries(SocketForwarder PUBLIC ${SOCKET_LIB_SOURCE}/libCppSocketLibrary.a + PUBLIC ${LOG_LIB_SOURCE}/src/main/cpp/liblog4cxx.so pthread bluetooth uuid diff --git a/Dockerfile b/Dockerfile index 5109375..85d1144 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,7 +16,7 @@ WORKDIR /builder # For alpine linux # RUN apk update && apk upgrade && apk add g++ cmake make git bluez-dev glib-dev bluez gdb -RUN apt update && apt install g++ cmake make git libbluetooth-dev libglib2.0-dev bluez gdb uuid-dev -y +RUN apt update && apt install g++ cmake make git libbluetooth-dev libglib2.0-dev bluez gdb uuid-dev libapr1-dev libaprutil1-dev zip -y COPY ./socket-forwarder ./socket-forwarder COPY ./tests ./tests diff --git a/socket-forwarder/main.cpp b/socket-forwarder/main.cpp index 5195b96..25b9bf0 100644 --- a/socket-forwarder/main.cpp +++ b/socket-forwarder/main.cpp @@ -9,12 +9,17 @@ #include "environment/Environment.h" #include "forwarder/Forwarder.h" +#include +#include + // Make sure version of built image matches const std::string VERSION = "0.3.0"; int main(int argc, char** argv) { - std::cout << "Running SocketForwarder v" << VERSION << std::endl; + auto logger = log4cxx::Logger::getLogger("SocketForwarder"); + + LOG4CXX_INFO(logger, "Running SocketForwarder v" + VERSION); const std::string newClientPrefix = forwarder::getEnvironmentVariableValueOrDefault(forwarder::NEW_CLIENT_PREFIX, forwarder::NEW_CLIENT_PREFIX_DEFAULT); const unsigned short maxReadInSize = std::atoi(forwarder::getEnvironmentVariableValueOrDefault(forwarder::MAX_READ_IN_SIZE, std::to_string(forwarder::MAX_READ_IN_DEFAULT)).c_str()); From c2b1c7f9ceb7cc15fb7d4f1411253e7c2ebdb87e Mon Sep 17 00:00:00 2001 From: Kilemonn Date: Mon, 14 Oct 2024 15:21:43 +0900 Subject: [PATCH 02/13] Change all logging to use the new logger. --- socket-forwarder/forwarder/Forwarder.cpp | 104 ++++++------------ socket-forwarder/forwarder/Forwarder.h | 4 + socket-forwarder/main.cpp | 18 +-- socket-forwarder/sockets/Sockets.cpp | 34 +++--- tests/CMakeLists.txt | 2 + .../forwarder/UDPSocketForwarderTest.cpp | 4 +- 6 files changed, 73 insertions(+), 93 deletions(-) diff --git a/socket-forwarder/forwarder/Forwarder.cpp b/socket-forwarder/forwarder/Forwarder.cpp index b574330..5ff2709 100644 --- a/socket-forwarder/forwarder/Forwarder.cpp +++ b/socket-forwarder/forwarder/Forwarder.cpp @@ -22,17 +22,14 @@ namespace forwarder if (tcpSessions.find(groupId) == tcpSessions.end()) { - std::cout << "[TCP] - Creating new group with ID [" << groupId << "], adding address [" << addressString << "] to group.\n"; + LOG4CXX_INFO(logger, "[TCP] - Creating new group with ID [" << groupId << "], adding address [" << addressString << "] to group."); // No existing groups with this ID, creating new std::vector sockets = { socket }; tcpSessions.insert(std::make_pair(groupId, sockets)); } else { - if (debug) - { - std::cout << "[TCP] - Adding new connection [" << addressString << "] to group [" << groupId << "].\n"; - } + LOG4CXX_DEBUG(logger, "[TCP] - Adding new connection [" << addressString << "] to group [" << groupId << "]."); tcpSessions[groupId].push_back(socket); } } @@ -41,11 +38,11 @@ namespace forwarder { if (tcpPreconfigured.find(address) != tcpPreconfigured.end()) { - std::cout << "[TCP] - Address [" << kt::getAddress(address).value_or("") + ":" + std::to_string(kt::getPortNumber(address)) << "] is already preconfigured, skipping..." << std::endl; + LOG4CXX_INFO(logger, "[TCP] - Address [" << kt::getAddress(address).value_or("") + ":" + std::to_string(kt::getPortNumber(address)) << "] is already preconfigured, skipping..."); } else { - std::cout << "[TCP] - Adding address [" << kt::getAddress(address).value_or("") + ":" + std::to_string(kt::getPortNumber(address)) << "] to TCP preconfiguration list for group [" << groupId << "]." << std::endl; + LOG4CXX_INFO(logger, "[TCP] - Adding address [" << kt::getAddress(address).value_or("") + ":" + std::to_string(kt::getPortNumber(address)) << "] to TCP preconfiguration list for group [" << groupId << "]."); tcpPreconfigured.insert(std::make_pair(address, groupId)); } } @@ -59,29 +56,29 @@ namespace forwarder { if (tcpServerSocket.has_value()) { - std::cout << "[TCP] - Running TCP forwarder on port [" << tcpServerSocket->getPort() << "]" << std::endl; + LOG4CXX_INFO(logger, "[TCP] - Running TCP forwarder on port [" << tcpServerSocket->getPort() << "]"); startTCPForwarder(); } else { - std::cout << "[TCP] - No TCP socket was provided, TCP forwarding is disabled." << std::endl; + LOG4CXX_INFO(logger, "[TCP] - No TCP socket was provided, TCP forwarding is disabled."); } if (udpRecieveSocket.has_value()) { if (udpRecieveSocket->isUdpBound()) { - std::cout << "[UDP] - Running UDP forwarder on port [" << udpRecieveSocket->getListeningPort().value() << "]" << std::endl; + LOG4CXX_INFO(logger, "[UDP] - Running UDP forwarder on port [" << udpRecieveSocket->getListeningPort().value() << "]"); startUDPForwarder(); } else { - std::cout << "[UDP] - Provided UDP socket needs to be bound before it is passed into the forwarder. UDP forwarding will be disabled." << std::endl; + LOG4CXX_INFO(logger, "[UDP] - Provided UDP socket needs to be bound before it is passed into the forwarder. UDP forwarding will be disabled."); } } else { - std::cout << "[UDP] - No UDP socket was provided, UDP forwarding is disabled." << std::endl; + LOG4CXX_INFO(logger, "[UDP] - No UDP socket was provided, UDP forwarding is disabled."); } } @@ -103,7 +100,7 @@ namespace forwarder { kt::ServerSocket& serverSocket = tcpServerSocket.value(); - std::cout << "[TCP] - Starting TCP connection listener..." << std::endl; + LOG4CXX_INFO(logger, "[TCP] - Starting TCP connection listener..."); while(forwarderIsRunning) { try @@ -114,18 +111,14 @@ namespace forwarder auto preConfiguredAddress = tcpPreconfigured.find(socket.getSocketAddress()); if (preConfiguredAddress != tcpPreconfigured.end()) { - std::cout << "[TCP] - Accepted connection to pre-configured address [" << addressString << "] adding to group [" << preConfiguredAddress->second << "]." << std::endl; + LOG4CXX_INFO(logger, "[TCP] - Accepted connection to pre-configured address [" << addressString << "] adding to group [" << preConfiguredAddress->second << "]."); addSocketToTCPGroup(preConfiguredAddress->second, socket); } else { std::string firstMessage = socket.receiveAmount(maxReadInSize); - std::cout << "[TCP] - Accepted new connection from [" << addressString << "] and read message of size [" << firstMessage.size() << "].\n"; - - if (debug) - { - std::cout << "[TCP] - Accepted connection message: [" << firstMessage << "]\n"; - } + LOG4CXX_INFO(logger, "[TCP] - Accepted new connection from [" << addressString << "] and read message of size [" << firstMessage.size() << "]."); + LOG4CXX_DEBUG(logger, "[TCP] - Accepted connection message: [" << firstMessage << "]"); if (firstMessage.rfind(newClientPrefix, 0) == 0) { @@ -135,7 +128,7 @@ namespace forwarder else { // First message does not start with prefix, just close connection - std::cout << "[TCP] - First message from address [" << addressString << "] did not start with prefix: [" << newClientPrefix << "]. Closing connection.\n"; + LOG4CXX_INFO(logger, "[TCP] - First message from address [" << addressString << "] did not start with prefix: [" << newClientPrefix << "]. Closing connection."); socket.close(); } } @@ -146,10 +139,8 @@ namespace forwarder } catch(kt::SocketException e) { - std::cout << "[TCP] - Failed to accept incoming client: " << e.what() << std::endl; + LOG4CXX_INFO(logger, "[TCP] - Failed to accept incoming client: " << e.what()); } - - std::cout << std::flush; } // If we exit the loop, close the server socket @@ -158,7 +149,7 @@ namespace forwarder void Forwarder::startTCPDataForwarder() { - std::cout << "[TCP] - Starting TCP forwarder listener..." << std::endl; + LOG4CXX_INFO(logger, "[TCP] - Starting TCP forwarder listener..."); while (forwarderIsRunning) { std::vector toRemove; @@ -177,10 +168,8 @@ namespace forwarder } std::string uuidString = getNewUUID(); - if (debug) - { - std::cout << "[TCP - " + uuidString + "] - Group [" << groupID << "] with [" << it->second.size() << "] nodes. Received content [" << received << "] from peer [" << i << "] forwarding to other peers...\n"; - } + LOG4CXX_DEBUG(logger, "[TCP - " + uuidString + "] - Group [" << groupID << "] with [" << it->second.size() << "] nodes. Received content [" << received << "] from peer [" << i << "] forwarding to other peers..."); + std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); for (size_t j = 0; j < it->second.size(); j++) { @@ -189,42 +178,32 @@ namespace forwarder const kt::TCPSocket& forwardToSocket = it->second[j]; if (!forwardToSocket.connected()) { - if (debug) - { - std::cout << "[TCP - " + uuidString + "] - Group [" << groupID << "], peer [" << j << "] is no longer connected, marking for removal from group.\n"; - } + LOG4CXX_DEBUG(logger, "[TCP - " + uuidString + "] - Group [" << groupID << "], peer [" << j << "] is no longer connected, marking for removal from group."); toRemove.push_back(j); } else { if (forwardToSocket.send(received, MSG_NOSIGNAL).first) { - if (debug) - { - std::cout << "[TCP - " + uuidString + "] - Group [" << groupID << "], successfully forwarded to peer [" << j << "]\n"; - } + LOG4CXX_DEBUG(logger, "[TCP - " + uuidString + "] - Group [" << groupID << "], successfully forwarded to peer [" << j << "]"); + } else { - if (debug) - { - std::cout << "[TCP - " + uuidString + "] - Group [" << groupID << "], failed to send to peer [" << j << "], marking for removal from group.\n"; - } + LOG4CXX_DEBUG(logger, "[TCP - " + uuidString + "] - Group [" << groupID << "], failed to send to peer [" << j << "], marking for removal from group."); toRemove.push_back(j); } } } } - if (debug) - { - std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); - std::cout << "[TCP - " + uuidString + "] - Group [" << groupID << "] took [" << std::chrono::duration_cast(end - start).count() << "ms] to forward message to [" << it->second.size() - 1 << "] peers.\n"; - } + + std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); + LOG4CXX_DEBUG(logger, "[TCP - " + uuidString + "] - Group [" << groupID << "] took [" << std::chrono::duration_cast(end - start).count() << "ms] to forward message to [" << it->second.size() - 1 << "] peers."); for (size_t index : toRemove) { std::vector::iterator socketPosition = std::next(it->second.begin(), index); - std::cout << "[TCP - " + uuidString + "] - Group [" << groupID << "] - Closing and removing socket with address [" << kt::getAddress(socketPosition->getSocketAddress()).value_or("") + ":" + std::to_string(kt::getPortNumber(socketPosition->getSocketAddress())) << "].\n"; + LOG4CXX_INFO(logger, "[TCP - " + uuidString + "] - Group [" << groupID << "] - Closing and removing socket with address [" << kt::getAddress(socketPosition->getSocketAddress()).value_or("") + ":" + std::to_string(kt::getPortNumber(socketPosition->getSocketAddress())) << "]."); socketPosition->close(); it->second.erase(socketPosition); } @@ -273,7 +252,7 @@ namespace forwarder { kt::UDPSocket& udpSocket = udpRecieveSocket.value(); - std::cout << "[UDP] - Starting UDP forwarder connection listener..." << std::endl; + LOG4CXX_INFO(logger, "[UDP] - Starting UDP forwarder connection listener..."); while (forwarderIsRunning) { if (udpSocket.ready()) @@ -285,16 +264,13 @@ namespace forwarder std::string addressString = kt::getAddress(result.second.second).value_or("") + ":" + std::to_string(kt::getPortNumber(result.second.second)); std::string& message = result.first.value(); - if (debug) - { - std::cout << "[UDP] - Received message [" << message << "] from address: [" << addressString << "]\n"; - } + LOG4CXX_DEBUG(logger, "[UDP] - Received message [" << message << "] from address: [" << addressString << "]"); // This is a new client, check their first message content if (message.rfind(newClientPrefix, 0) == 0) { std::string recievingPort = message.substr(newClientPrefix.size()); - std::cout << "[UDP] - New client joined UDP group from address [" << addressString << "] with request reply port [" << recievingPort << "]\n"; + LOG4CXX_INFO(logger, "[UDP] - New client joined UDP group from address [" << addressString << "] with request reply port [" << recievingPort << "]"); kt::SocketAddress address = result.second.second; address.ipv4.sin_port = htons(std::atoi(recievingPort.c_str())); @@ -306,14 +282,13 @@ namespace forwarder } } } - std::cout << std::flush; } udpSocket.close(); } void Forwarder::startUDPDataForwarder() { - std::cout << "[UDP] - Starting UDP data forwarder listener..." << std::endl; + LOG4CXX_INFO(logger, "[UDP] - Starting UDP data forwarder listener..."); kt::UDPSocket sendSocket; while (forwarderIsRunning) { @@ -323,27 +298,18 @@ namespace forwarder std::string message = udpMessageQueue.front(); udpMessageQueue.pop(); - if (debug) - { - std::cout << "[UDP - " + uuidString + "] - Received message [" << message << "] forwarding to peers.\n"; - } + LOG4CXX_DEBUG(logger, "[UDP - " + uuidString + "] - Received message [" << message << "] forwarding to peers."); std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); for (const kt::SocketAddress& addr : udpKnownPeers) { std::pair result = sendSocket.sendTo(message, addr); - if (debug) - { - std::cout << "[UDP - " + uuidString + "] - Forwarded to peer with address: [" << kt::getAddress(addr).value_or("") + ":" + std::to_string(kt::getPortNumber(addr)) << "]. With result [" << result.second << "]\n"; - } + LOG4CXX_DEBUG(logger, "[UDP - " + uuidString + "] - Forwarded to peer with address: [" << kt::getAddress(addr).value_or("") + ":" + std::to_string(kt::getPortNumber(addr)) << "]. With result [" << result.second << "]"); } - if (debug) - { - std::cout << "[UDP - " + uuidString + "] - Forwarded to [" << udpKnownPeers.size() << "] peer(s).\n"; - std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); - std::cout << "[UDP - " + uuidString + "] - Took [" << std::chrono::duration_cast(end - start).count() << "ms] to forward message to [" << udpKnownPeers.size() << "] peers.\n"; - } + LOG4CXX_DEBUG(logger, "[UDP - " + uuidString + "] - Forwarded to [" << udpKnownPeers.size() << "] peer(s)."); + std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); + LOG4CXX_DEBUG(logger, "[UDP - " + uuidString + "] - Took [" << std::chrono::duration_cast(end - start).count() << "ms] to forward message to [" << udpKnownPeers.size() << "] peers."); } else { diff --git a/socket-forwarder/forwarder/Forwarder.h b/socket-forwarder/forwarder/Forwarder.h index 3ba37b3..7806a33 100644 --- a/socket-forwarder/forwarder/Forwarder.h +++ b/socket-forwarder/forwarder/Forwarder.h @@ -11,8 +11,11 @@ #include #include +#include + namespace forwarder { + class Forwarder { protected: @@ -41,6 +44,7 @@ namespace forwarder std::optional> tcpRunningThreads = std::nullopt; std::optional> udpRunningThreads = std::nullopt; + log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger("SocketForwarder"); bool forwarderIsRunning = false; std::string newClientPrefix; unsigned short maxReadInSize; diff --git a/socket-forwarder/main.cpp b/socket-forwarder/main.cpp index 25b9bf0..85c7380 100644 --- a/socket-forwarder/main.cpp +++ b/socket-forwarder/main.cpp @@ -17,18 +17,22 @@ const std::string VERSION = "0.3.0"; int main(int argc, char** argv) { + log4cxx::BasicConfigurator::configure(); + auto logger = log4cxx::Logger::getLogger("SocketForwarder"); - LOG4CXX_INFO(logger, "Running SocketForwarder v" + VERSION); + LOG4CXX_INFO(logger, "Running SocketForwarder v" << VERSION); const std::string newClientPrefix = forwarder::getEnvironmentVariableValueOrDefault(forwarder::NEW_CLIENT_PREFIX, forwarder::NEW_CLIENT_PREFIX_DEFAULT); const unsigned short maxReadInSize = std::atoi(forwarder::getEnvironmentVariableValueOrDefault(forwarder::MAX_READ_IN_SIZE, std::to_string(forwarder::MAX_READ_IN_DEFAULT)).c_str()); const bool debug = forwarder::getEnvironmentVariableValue(forwarder::DEBUG).has_value(); - std::cout << "Using new client prefix: [" << newClientPrefix << "].\n" - << "Using max read in size: [" << maxReadInSize << "].\n" - << "DEBUG flag set to [" << debug << "].\n" - << "Binding to host address [" << forwarder::getEnvironmentVariableValueOrDefault(forwarder::HOST_ADDRESS, forwarder::HOST_ADDRESS_DEFAULT) << "]." << std::endl; + LOG4CXX_INFO(logger, "Using new client prefix: [" << newClientPrefix << "]."); + LOG4CXX_INFO(logger, "Using max read in size: [" << maxReadInSize << "]."); + + // TODO: Remove debug flag, use log4cxx configuration + LOG4CXX_INFO(logger, "DEBUG flag set to [" << debug << "]."); + LOG4CXX_INFO(logger, "Binding to host address [" << forwarder::getEnvironmentVariableValueOrDefault(forwarder::HOST_ADDRESS, forwarder::HOST_ADDRESS_DEFAULT) << "]."); std::optional serverSocket = forwarder::setUpTcpServerSocket(argc > 1 ? std::make_optional(std::string(argv[1])) : std::nullopt); std::optional udpSocket = forwarder::setUpUDPSocket(argc > 2 ? std::make_optional(std::string(argv[2])) : std::nullopt); @@ -38,7 +42,7 @@ int main(int argc, char** argv) std::vector udpPreconfiguredAddresses = forwarder::getPreconfiguredUDPAddresses(); if (!udpPreconfiguredAddresses.empty()) { - std::cout << "UDP preconfigured addresses provided, setting into forwarder..." << std::endl; + LOG4CXX_INFO(logger, "UDP preconfigured addresses provided, setting into forwarder..."); for (const kt::SocketAddress& addr : udpPreconfiguredAddresses) { forwarder.addAddressToUDPGroup(addr); @@ -48,7 +52,7 @@ int main(int argc, char** argv) std::unordered_map> tcpPreconfiguredAddresses = forwarder::getPreconfiguredTCPAddresses(); if (!tcpPreconfiguredAddresses.empty()) { - std::cout << "TCP preconfigured addresses provided, setting into forwarder..." << std::endl; + LOG4CXX_INFO(logger, "TCP preconfigured addresses provided, setting into forwarder..."); for (const auto& it : tcpPreconfiguredAddresses) { for (const kt::SocketAddress& addr : it.second) diff --git a/socket-forwarder/sockets/Sockets.cpp b/socket-forwarder/sockets/Sockets.cpp index a918365..b477d72 100644 --- a/socket-forwarder/sockets/Sockets.cpp +++ b/socket-forwarder/sockets/Sockets.cpp @@ -6,15 +6,19 @@ #include "../environment/Environment.h" #include "Sockets.h" +#include + namespace forwarder { + auto logger = log4cxx::Logger::getLogger("SocketForwarder"); + std::optional setUpTcpServerSocket(std::optional defaultPort) { std::optional tcpPort = forwarder::getEnvironmentVariableValue(forwarder::TCP_PORT); if (!tcpPort.has_value() && !defaultPort.has_value()) { - std::cout << "Skipping TCP socket creation since value for [" + forwarder::TCP_PORT + "] was not provided." << std::endl; + LOG4CXX_INFO(logger, "Skipping TCP socket creation since value for [" + forwarder::TCP_PORT + "] was not provided."); return std::nullopt; } @@ -28,12 +32,12 @@ namespace forwarder } catch(const kt::BindingException e) { - std::cout << "[TCP] - Failed to bind server socket on port [" << portNumber << "]. " << e.what() << std::endl; + LOG4CXX_INFO(logger, "[TCP] - Failed to bind server socket on port [" << portNumber << "]. " << e.what()); return std::nullopt; } catch (const kt::SocketException e) { - std::cout << "[TCP] - Failed to create server socket: " << e.what() << std::endl; + LOG4CXX_INFO(logger, "[TCP] - Failed to create server socket: " << e.what()); return std::nullopt; } } @@ -44,7 +48,7 @@ namespace forwarder if (!udpPort.has_value() && !defaultPort.has_value()) { - std::cout << "Skipping UDP socket creation since value for [" + forwarder::UDP_PORT + "] was not provided." << std::endl; + LOG4CXX_INFO(logger, "Skipping UDP socket creation since value for [" + forwarder::UDP_PORT + "] was not provided."); return std::nullopt; } @@ -56,19 +60,19 @@ namespace forwarder kt::UDPSocket udpSocket; if (!udpSocket.bind(getEnvironmentVariableValueOrDefault(HOST_ADDRESS, HOST_ADDRESS_DEFAULT), portNumber).first) { - std::cout << "[UDP] - Failed to bind to provided port [" << portNumber << "].\n"; + LOG4CXX_INFO(logger, "[UDP] - Failed to bind to provided port [" << portNumber << "]."); return std::nullopt; } return std::make_optional(udpSocket); } catch(const kt::BindingException e) { - std::cout << "[UDP] - Failed to bind UDP socket on port: [" << portNumber << "]. " << e.what() << std::endl; + LOG4CXX_INFO(logger, "[UDP] - Failed to bind UDP socket on port: [" << portNumber << "]. " << e.what()); return std::nullopt; } catch (const kt::SocketException e) { - std::cout << "[UDP] - Failed to create UDP socket: " << e.what() << std::endl; + LOG4CXX_INFO(logger, "[UDP] - Failed to create UDP socket: " << e.what()); return std::nullopt; } } @@ -90,13 +94,13 @@ namespace forwarder } else if (parts.size() < 3) { - std::cout << "[TCP] - Unable to add address [" << s << "], expected format to be \":
:\"." << std::endl; + LOG4CXX_INFO(logger, "[TCP] - Unable to add address [" << s << "], expected format to be \":
:\"."); } else { if (parts.size() > 3) { - std::cout << "[TCP] - Multiple ':' provided in address string [" << s << "]. Attempting to parse and add address to group [" << parts[0] << "] using second and third elements as the address [" << parts[1] << ", " << parts[2] << "]." << std::endl; + LOG4CXX_INFO(logger, "[TCP] - Multiple ':' provided in address string [" << s << "]. Attempting to parse and add address to group [" << parts[0] << "] using second and third elements as the address [" << parts[1] << ", " << parts[2] << "]."); } unsigned short portNumber = static_cast(std::atoi(parts[2].c_str())); @@ -104,12 +108,12 @@ namespace forwarder std::pair, int> resolvedAddresses = kt::resolveToAddresses(parts[1], portNumber, info); if (resolvedAddresses.first.empty()) { - std::cout << "[TCP] - Failed to resolve address [" << parts[1] << ":" << portNumber << "]. Address will not be added to TCP group [" << parts[0] << "]." << std::endl; + LOG4CXX_INFO(logger, "[TCP] - Failed to resolve address [" << parts[1] << ":" << portNumber << "]. Address will not be added to TCP group [" << parts[0] << "]."); } else { kt::SocketAddress addr = resolvedAddresses.first.at(0); - std::cout << "[TCP] - Resolved and added pre-configured address [" << kt::getAddress(addr).value_or("") + ":" + std::to_string(kt::getPortNumber(addr)) << "] to group [" << parts[0] << "]." << std::endl; + LOG4CXX_INFO(logger, "[TCP] - Resolved and added pre-configured address [" << kt::getAddress(addr).value_or("") + ":" + std::to_string(kt::getPortNumber(addr)) << "] to group [" << parts[0] << "]."); if (addresses.find(parts[0]) == addresses.end()) { @@ -144,13 +148,13 @@ namespace forwarder } else if (parts.size() < 2) { - std::cout << "[UDP] - Unable to add address [" << s << "], expected format to be \"
:\"." << std::endl; + LOG4CXX_INFO(logger, "[UDP] - Unable to add address [" << s << "], expected format to be \"
:\"."); } else { if (parts.size() > 2) { - std::cout << "[UDP] - Multiple ':' provided in address string [" << s << "]. Attempting to parse as address using first two elements [" << parts[0] << ", " << parts[1] << "]." << std::endl; + LOG4CXX_INFO(logger, "[UDP] - Multiple ':' provided in address string [" << s << "]. Attempting to parse as address using first two elements [" << parts[0] << ", " << parts[1] << "]."); } unsigned short portNumber = static_cast(std::atoi(parts[1].c_str())); @@ -158,12 +162,12 @@ namespace forwarder std::pair, int> resolvedAddresses = kt::resolveToAddresses(parts[0], portNumber, info); if (resolvedAddresses.first.empty()) { - std::cout << "[UDP] - Failed to resolve address [" << parts[0] << ":" << portNumber << "]. Address will not be added to UDP group." << std::endl; + LOG4CXX_INFO(logger, "[UDP] - Failed to resolve address [" << parts[0] << ":" << portNumber << "]. Address will not be added to UDP group."); } else { kt::SocketAddress addr = resolvedAddresses.first.at(0); - std::cout << "[UDP] - Resolved and added pre-configured address [" << kt::getAddress(addr).value_or("") + ":" + std::to_string(kt::getPortNumber(addr)) << "] to UDP group." << std::endl; + LOG4CXX_INFO(logger, "[UDP] - Resolved and added pre-configured address [" << kt::getAddress(addr).value_or("") + ":" + std::to_string(kt::getPortNumber(addr)) << "] to UDP group."); addresses.push_back(addr); } } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index f38f4c3..8326012 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -37,6 +37,7 @@ add_executable(${PROJECT_NAME} ${FORWARDER_TEST_SOURCE} ${FORWARDER_SOURCE_FOR_T target_include_directories(SocketForwarderTests PUBLIC ${SOCKET_LIB_SOURCE}/src + PUBLIC ${LOG_LIB_SOURCE}/src/main/include ) target_link_libraries(${PROJECT_NAME} PUBLIC @@ -46,6 +47,7 @@ target_link_libraries(${PROJECT_NAME} PUBLIC pthread uuid PUBLIC ${SOCKET_LIB_SOURCE}/libCppSocketLibrary.a + PUBLIC ${LOG_LIB_SOURCE}/src/main/cpp/liblog4cxx.so ) # Enable unit testing diff --git a/tests/socket-forwarder/forwarder/UDPSocketForwarderTest.cpp b/tests/socket-forwarder/forwarder/UDPSocketForwarderTest.cpp index 4456e5d..a35cb53 100644 --- a/tests/socket-forwarder/forwarder/UDPSocketForwarderTest.cpp +++ b/tests/socket-forwarder/forwarder/UDPSocketForwarderTest.cpp @@ -119,7 +119,7 @@ namespace forwarder } } - std::cout << "UDP - Test - Received messages [" << receivedMessageCount << "/" << messagesToReceive * (endIndex - startIndex) << "] [~" << (static_cast(receivedMessageCount) / static_cast((messagesToReceive * (endIndex - startIndex)))) * 100 << "%] from forwarder->\n"; + std::cout << "UDP - Test - Received messages [" << receivedMessageCount << "/" << messagesToReceive * (endIndex - startIndex) << "] [~" << (static_cast(receivedMessageCount) / static_cast((messagesToReceive * (endIndex - startIndex)))) * 100 << "%] from forwarder." << std::endl; } /** @@ -208,7 +208,7 @@ namespace forwarder socket.close(); } - std::cout << "UDP - Test - Received messages [" << receivedMessageCount << "/" << messagesToSend * amountOfClients << "] [~" << (static_cast(receivedMessageCount) / static_cast((messagesToSend * amountOfClients))) * 100 << "%] from forwarder->\n"; + std::cout << "UDP - Test - Received messages [" << receivedMessageCount << "/" << messagesToSend * amountOfClients << "] [~" << (static_cast(receivedMessageCount) / static_cast((messagesToSend * amountOfClients))) * 100 << "%] from forwarder." << std::endl; // We cannot assert this since there can be messages that are lost or dropped because of the use of UDP // ASSERT_EQ(messagesToSend * amountOfClients, receivedMessageCount); From 91f6e98ea0e121eb04691575cf02c07afa64e448 Mon Sep 17 00:00:00 2001 From: Kilemonn Date: Mon, 14 Oct 2024 15:40:02 +0900 Subject: [PATCH 03/13] Fix test logging by setting up basic logger. --- tests/socket-forwarder/forwarder/TCPSocketForwarderTest.cpp | 3 +++ tests/socket-forwarder/forwarder/UDPSocketForwarderTest.cpp | 3 +++ 2 files changed, 6 insertions(+) diff --git a/tests/socket-forwarder/forwarder/TCPSocketForwarderTest.cpp b/tests/socket-forwarder/forwarder/TCPSocketForwarderTest.cpp index 66e33b5..4c07929 100644 --- a/tests/socket-forwarder/forwarder/TCPSocketForwarderTest.cpp +++ b/tests/socket-forwarder/forwarder/TCPSocketForwarderTest.cpp @@ -7,6 +7,8 @@ #include "../../../socket-forwarder/environment/Environment.h" #include "../../../socket-forwarder/forwarder/Forwarder.h" +#include + using namespace std::chrono_literals; namespace forwarder @@ -20,6 +22,7 @@ namespace forwarder TCPSocketForwarderTest() : serverSocket(kt::SocketType::Wifi), forwarder(serverSocket, std::nullopt, NEW_CLIENT_PREFIX_DEFAULT, MAX_READ_IN_DEFAULT, true) {} void SetUp() override { + log4cxx::BasicConfigurator::configure(); forwarder.start(); } diff --git a/tests/socket-forwarder/forwarder/UDPSocketForwarderTest.cpp b/tests/socket-forwarder/forwarder/UDPSocketForwarderTest.cpp index a35cb53..31a2d31 100644 --- a/tests/socket-forwarder/forwarder/UDPSocketForwarderTest.cpp +++ b/tests/socket-forwarder/forwarder/UDPSocketForwarderTest.cpp @@ -6,6 +6,8 @@ #include "../../../socket-forwarder/environment/Environment.h" #include "../../../socket-forwarder/forwarder/Forwarder.h" +#include + using namespace std::chrono_literals; namespace forwarder @@ -25,6 +27,7 @@ namespace forwarder void SetUp() override { + log4cxx::BasicConfigurator::configure(); ASSERT_NE(forwarder, std::nullopt); forwarder->start(); } From 06416ec8eaf7ffe90cc6bb8f953681af502e18d8 Mon Sep 17 00:00:00 2001 From: Kilemonn Date: Mon, 14 Oct 2024 15:47:22 +0900 Subject: [PATCH 04/13] Bump version. --- CMakeLists.txt | 2 +- socket-forwarder/main.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2b87c15..86a83b9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(PROJECT_NAME SocketForwarder) -project(${PROJECT_NAME} VERSION 0.1.1) +project(${PROJECT_NAME} VERSION 0.4.0) ExternalProject_Add(CppSocketLibrary GIT_REPOSITORY https://github.com/Kilemonn/Cpp-SocketLibrary.git diff --git a/socket-forwarder/main.cpp b/socket-forwarder/main.cpp index 85c7380..658012c 100644 --- a/socket-forwarder/main.cpp +++ b/socket-forwarder/main.cpp @@ -13,7 +13,7 @@ #include // Make sure version of built image matches -const std::string VERSION = "0.3.0"; +const std::string VERSION = "0.4.0"; int main(int argc, char** argv) { From f0ac23c7c0ce4be31f882c747135ab577d9150dd Mon Sep 17 00:00:00 2001 From: Kilemonn Date: Mon, 14 Oct 2024 15:49:07 +0900 Subject: [PATCH 05/13] Improve log level for messages. --- socket-forwarder/forwarder/Forwarder.cpp | 6 +++--- socket-forwarder/sockets/Sockets.cpp | 18 +++++++++--------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/socket-forwarder/forwarder/Forwarder.cpp b/socket-forwarder/forwarder/Forwarder.cpp index 5ff2709..8f1cfb1 100644 --- a/socket-forwarder/forwarder/Forwarder.cpp +++ b/socket-forwarder/forwarder/Forwarder.cpp @@ -128,7 +128,7 @@ namespace forwarder else { // First message does not start with prefix, just close connection - LOG4CXX_INFO(logger, "[TCP] - First message from address [" << addressString << "] did not start with prefix: [" << newClientPrefix << "]. Closing connection."); + LOG4CXX_ERROR(logger, "[TCP] - First message from address [" << addressString << "] did not start with prefix: [" << newClientPrefix << "]. Closing connection."); socket.close(); } } @@ -139,7 +139,7 @@ namespace forwarder } catch(kt::SocketException e) { - LOG4CXX_INFO(logger, "[TCP] - Failed to accept incoming client: " << e.what()); + LOG4CXX_ERROR(logger, "[TCP] - Failed to accept incoming client: " << e.what()); } } @@ -190,7 +190,7 @@ namespace forwarder } else { - LOG4CXX_DEBUG(logger, "[TCP - " + uuidString + "] - Group [" << groupID << "], failed to send to peer [" << j << "], marking for removal from group."); + LOG4CXX_WARN(logger, "[TCP - " + uuidString + "] - Group [" << groupID << "], failed to send to peer [" << j << "], marking for removal from group."); toRemove.push_back(j); } } diff --git a/socket-forwarder/sockets/Sockets.cpp b/socket-forwarder/sockets/Sockets.cpp index b477d72..996652f 100644 --- a/socket-forwarder/sockets/Sockets.cpp +++ b/socket-forwarder/sockets/Sockets.cpp @@ -32,12 +32,12 @@ namespace forwarder } catch(const kt::BindingException e) { - LOG4CXX_INFO(logger, "[TCP] - Failed to bind server socket on port [" << portNumber << "]. " << e.what()); + LOG4CXX_ERROR(logger, "[TCP] - Failed to bind server socket on port [" << portNumber << "]. " << e.what()); return std::nullopt; } catch (const kt::SocketException e) { - LOG4CXX_INFO(logger, "[TCP] - Failed to create server socket: " << e.what()); + LOG4CXX_ERROR(logger, "[TCP] - Failed to create server socket: " << e.what()); return std::nullopt; } } @@ -60,19 +60,19 @@ namespace forwarder kt::UDPSocket udpSocket; if (!udpSocket.bind(getEnvironmentVariableValueOrDefault(HOST_ADDRESS, HOST_ADDRESS_DEFAULT), portNumber).first) { - LOG4CXX_INFO(logger, "[UDP] - Failed to bind to provided port [" << portNumber << "]."); + LOG4CXX_ERROR(logger, "[UDP] - Failed to bind to provided port [" << portNumber << "]."); return std::nullopt; } return std::make_optional(udpSocket); } catch(const kt::BindingException e) { - LOG4CXX_INFO(logger, "[UDP] - Failed to bind UDP socket on port: [" << portNumber << "]. " << e.what()); + LOG4CXX_ERROR(logger, "[UDP] - Failed to bind UDP socket on port: [" << portNumber << "]. " << e.what()); return std::nullopt; } catch (const kt::SocketException e) { - LOG4CXX_INFO(logger, "[UDP] - Failed to create UDP socket: " << e.what()); + LOG4CXX_ERROR(logger, "[UDP] - Failed to create UDP socket: " << e.what()); return std::nullopt; } } @@ -90,7 +90,7 @@ namespace forwarder std::vector parts = split(s, ":"); if (parts.size() == 1 && parts[0].empty()) { - // Skip + LOG4CXX_WARN(logger, "[TCP] - Skipping processing address with value [" << s << "], expected format to be \":
:\"."); } else if (parts.size() < 3) { @@ -108,7 +108,7 @@ namespace forwarder std::pair, int> resolvedAddresses = kt::resolveToAddresses(parts[1], portNumber, info); if (resolvedAddresses.first.empty()) { - LOG4CXX_INFO(logger, "[TCP] - Failed to resolve address [" << parts[1] << ":" << portNumber << "]. Address will not be added to TCP group [" << parts[0] << "]."); + LOG4CXX_ERROR(logger, "[TCP] - Failed to resolve address [" << parts[1] << ":" << portNumber << "]. Address will not be added to TCP group [" << parts[0] << "]."); } else { @@ -144,7 +144,7 @@ namespace forwarder std::vector parts = split(s, ":"); if (parts.size() == 1 && parts[0].empty()) { - // Skip + LOG4CXX_WARN(logger, "[UDP] - Skipping processing address with value [" << s << "], expected format to be \"
:\"."); } else if (parts.size() < 2) { @@ -162,7 +162,7 @@ namespace forwarder std::pair, int> resolvedAddresses = kt::resolveToAddresses(parts[0], portNumber, info); if (resolvedAddresses.first.empty()) { - LOG4CXX_INFO(logger, "[UDP] - Failed to resolve address [" << parts[0] << ":" << portNumber << "]. Address will not be added to UDP group."); + LOG4CXX_ERROR(logger, "[UDP] - Failed to resolve address [" << parts[0] << ":" << portNumber << "]. Address will not be added to UDP group."); } else { From 611ed7894505b91fd62aec05be1b7647391ce7ae Mon Sep 17 00:00:00 2001 From: Kilemonn Date: Tue, 15 Oct 2024 12:56:56 +0900 Subject: [PATCH 06/13] Only setup log configuration once, in class setup functions. --- .../socket-forwarder/forwarder/TCPSocketForwarderTest.cpp | 8 +++++++- .../socket-forwarder/forwarder/UDPSocketForwarderTest.cpp | 7 ++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/tests/socket-forwarder/forwarder/TCPSocketForwarderTest.cpp b/tests/socket-forwarder/forwarder/TCPSocketForwarderTest.cpp index 4c07929..65ec616 100644 --- a/tests/socket-forwarder/forwarder/TCPSocketForwarderTest.cpp +++ b/tests/socket-forwarder/forwarder/TCPSocketForwarderTest.cpp @@ -20,9 +20,15 @@ namespace forwarder forwarder::Forwarder forwarder; protected: TCPSocketForwarderTest() : serverSocket(kt::SocketType::Wifi), forwarder(serverSocket, std::nullopt, NEW_CLIENT_PREFIX_DEFAULT, MAX_READ_IN_DEFAULT, true) {} - void SetUp() override + + static void SetUpTestCase() { + log4cxx::BasicConfigurator::resetConfiguration(); log4cxx::BasicConfigurator::configure(); + } + + void SetUp() override + { forwarder.start(); } diff --git a/tests/socket-forwarder/forwarder/UDPSocketForwarderTest.cpp b/tests/socket-forwarder/forwarder/UDPSocketForwarderTest.cpp index 31a2d31..5b5d521 100644 --- a/tests/socket-forwarder/forwarder/UDPSocketForwarderTest.cpp +++ b/tests/socket-forwarder/forwarder/UDPSocketForwarderTest.cpp @@ -25,9 +25,14 @@ namespace forwarder forwarder = forwarder::Forwarder(std::nullopt, udpSocket, NEW_CLIENT_PREFIX_DEFAULT, MAX_READ_IN_DEFAULT, true); } + static void SetUpTestCase() + { + log4cxx::BasicConfigurator::resetConfiguration(); + log4cxx::BasicConfigurator::configure(); + } + void SetUp() override { - log4cxx::BasicConfigurator::configure(); ASSERT_NE(forwarder, std::nullopt); forwarder->start(); } From a88bf5477a21e4ec9b4f873ff9ae426e5483c482 Mon Sep 17 00:00:00 2001 From: Kilemonn Date: Tue, 15 Oct 2024 13:09:01 +0900 Subject: [PATCH 07/13] Set log level explicitly for tests incase I want to change it in future to speed up the tests. --- tests/socket-forwarder/forwarder/TCPSocketForwarderTest.cpp | 1 + tests/socket-forwarder/forwarder/UDPSocketForwarderTest.cpp | 1 + 2 files changed, 2 insertions(+) diff --git a/tests/socket-forwarder/forwarder/TCPSocketForwarderTest.cpp b/tests/socket-forwarder/forwarder/TCPSocketForwarderTest.cpp index 65ec616..47ecc46 100644 --- a/tests/socket-forwarder/forwarder/TCPSocketForwarderTest.cpp +++ b/tests/socket-forwarder/forwarder/TCPSocketForwarderTest.cpp @@ -25,6 +25,7 @@ namespace forwarder { log4cxx::BasicConfigurator::resetConfiguration(); log4cxx::BasicConfigurator::configure(); + log4cxx::Logger::getRootLogger()->setLevel(log4cxx::Level::getDebug()); } void SetUp() override diff --git a/tests/socket-forwarder/forwarder/UDPSocketForwarderTest.cpp b/tests/socket-forwarder/forwarder/UDPSocketForwarderTest.cpp index 5b5d521..3a00b56 100644 --- a/tests/socket-forwarder/forwarder/UDPSocketForwarderTest.cpp +++ b/tests/socket-forwarder/forwarder/UDPSocketForwarderTest.cpp @@ -29,6 +29,7 @@ namespace forwarder { log4cxx::BasicConfigurator::resetConfiguration(); log4cxx::BasicConfigurator::configure(); + log4cxx::Logger::getRootLogger()->setLevel(log4cxx::Level::getDebug()); } void SetUp() override From cbc6ddddb486db29209c2877d3d1663d97794aa7 Mon Sep 17 00:00:00 2001 From: Kilemonn Date: Tue, 15 Oct 2024 14:25:32 +0900 Subject: [PATCH 08/13] Use installed version of log4cxx instead of always building from source since the shared lib is required at runtime anyway. Update dockerfile to have this new dependency at build time and at runner time. --- CMakeLists.txt | 15 +++------------ Dockerfile | 4 +++- tests/CMakeLists.txt | 4 ++-- 3 files changed, 8 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 86a83b9..b44f742 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,16 +19,7 @@ ExternalProject_Add(CppSocketLibrary ExternalProject_Get_Property(CppSocketLibrary SOURCE_DIR) set(SOCKET_LIB_SOURCE ${SOURCE_DIR}) -ExternalProject_Add(log4cxx - GIT_REPOSITORY https://github.com/apache/logging-log4cxx.git - GIT_TAG rel/v1.2.0 - INSTALL_COMMAND "" - UPDATE_DISCONNECTED FALSE - BUILD_IN_SOURCE TRUE -) - -ExternalProject_Get_Property(log4cxx SOURCE_DIR) -set(LOG_LIB_SOURCE ${SOURCE_DIR}) +find_package(log4cxx REQUIRED) set(FORWARDER_SOURCE socket-forwarder/main.cpp @@ -41,12 +32,12 @@ add_executable(SocketForwarder ${FORWARDER_SOURCE}) target_include_directories(SocketForwarder PUBLIC ${SOCKET_LIB_SOURCE}/src - PUBLIC ${LOG_LIB_SOURCE}/src/main/include + ${LOG4CXX_INCLUDE_DIRS} ) target_link_libraries(SocketForwarder PUBLIC ${SOCKET_LIB_SOURCE}/libCppSocketLibrary.a - PUBLIC ${LOG_LIB_SOURCE}/src/main/cpp/liblog4cxx.so + PRIVATE log4cxx pthread bluetooth uuid diff --git a/Dockerfile b/Dockerfile index 85d1144..52a8fe7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,7 +16,7 @@ WORKDIR /builder # For alpine linux # RUN apk update && apk upgrade && apk add g++ cmake make git bluez-dev glib-dev bluez gdb -RUN apt update && apt install g++ cmake make git libbluetooth-dev libglib2.0-dev bluez gdb uuid-dev libapr1-dev libaprutil1-dev zip -y +RUN apt update && apt install g++ cmake make git libbluetooth-dev libglib2.0-dev bluez gdb uuid-dev liblog4cxx-dev -y COPY ./socket-forwarder ./socket-forwarder COPY ./tests ./tests @@ -39,6 +39,8 @@ WORKDIR /socket-forwarder # For alpine linux # RUN apk update && apk upgrade && apk add libstdc++ +RUN apt update && apt install liblog4cxx-dev -y + COPY --from=builder /builder/build/SocketForwarder /socket-forwarder/SocketForwarder ENTRYPOINT ["./SocketForwarder"] diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 8326012..71b6bb0 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -37,7 +37,7 @@ add_executable(${PROJECT_NAME} ${FORWARDER_TEST_SOURCE} ${FORWARDER_SOURCE_FOR_T target_include_directories(SocketForwarderTests PUBLIC ${SOCKET_LIB_SOURCE}/src - PUBLIC ${LOG_LIB_SOURCE}/src/main/include + ${LOG4CXX_INCLUDE_DIRS} ) target_link_libraries(${PROJECT_NAME} PUBLIC @@ -47,7 +47,7 @@ target_link_libraries(${PROJECT_NAME} PUBLIC pthread uuid PUBLIC ${SOCKET_LIB_SOURCE}/libCppSocketLibrary.a - PUBLIC ${LOG_LIB_SOURCE}/src/main/cpp/liblog4cxx.so + PRIVATE log4cxx ) # Enable unit testing From 7c502185f7cb688220e004d4eaa249023827e48a Mon Sep 17 00:00:00 2001 From: Kilemonn Date: Tue, 15 Oct 2024 14:29:04 +0900 Subject: [PATCH 09/13] Remove debug flag as its now controlled by the log4j configuration. --- README.md | 8 -------- socket-forwarder/environment/Environment.h | 1 - socket-forwarder/forwarder/Forwarder.cpp | 4 ++-- socket-forwarder/forwarder/Forwarder.h | 1 - socket-forwarder/main.cpp | 6 +----- 5 files changed, 3 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index debd329..43c856d 100644 --- a/README.md +++ b/README.md @@ -75,14 +75,6 @@ This will be the maximum read size for all TCP and UDP socket read operations. --- -#### socketforwarder.debug - -*If not provided this is 'false' or disabled by default.* - -This will enable more logging of messages received, timing taken to forward and the amount of clients in each forwarder group. - ---- - #### socketforwarder.host_address *If not provided the value "0.0.0.0" is used.* diff --git a/socket-forwarder/environment/Environment.h b/socket-forwarder/environment/Environment.h index 7dba39a..9bd9259 100644 --- a/socket-forwarder/environment/Environment.h +++ b/socket-forwarder/environment/Environment.h @@ -9,7 +9,6 @@ namespace forwarder const std::string HOST_ADDRESS = SOCKET_FORWARDER_PREFIX + "host_address"; const std::string NEW_CLIENT_PREFIX = SOCKET_FORWARDER_PREFIX + "new_client_prefix"; const std::string MAX_READ_IN_SIZE = SOCKET_FORWARDER_PREFIX + "max_read_in_size"; - const std::string DEBUG = SOCKET_FORWARDER_PREFIX + "debug"; const std::string PRECONFIG_ADDRESSES_SUFFIX = "preconfig_addresses"; const std::string PORT_SUFFIX = "port"; diff --git a/socket-forwarder/forwarder/Forwarder.cpp b/socket-forwarder/forwarder/Forwarder.cpp index 8f1cfb1..2c19e8f 100644 --- a/socket-forwarder/forwarder/Forwarder.cpp +++ b/socket-forwarder/forwarder/Forwarder.cpp @@ -12,8 +12,8 @@ namespace forwarder { - Forwarder::Forwarder(std::optional tcpSocket, std::optional udpSocket, const std::string prefix, const unsigned short maxRead, const bool debugFlag): - tcpServerSocket(tcpSocket), udpRecieveSocket(udpSocket), newClientPrefix(prefix), maxReadInSize(maxRead), debug(debugFlag) + Forwarder::Forwarder(std::optional tcpSocket, std::optional udpSocket, const std::string prefix, const unsigned short maxRead): + tcpServerSocket(tcpSocket), udpRecieveSocket(udpSocket), newClientPrefix(prefix), maxReadInSize(maxRead) { } void Forwarder::addSocketToTCPGroup(const std::string& groupId, kt::TCPSocket socket) diff --git a/socket-forwarder/forwarder/Forwarder.h b/socket-forwarder/forwarder/Forwarder.h index 7806a33..ec315e6 100644 --- a/socket-forwarder/forwarder/Forwarder.h +++ b/socket-forwarder/forwarder/Forwarder.h @@ -48,7 +48,6 @@ namespace forwarder bool forwarderIsRunning = false; std::string newClientPrefix; unsigned short maxReadInSize; - bool debug = false; std::optional udpRecieveSocket = std::nullopt; std::optional tcpServerSocket = std::nullopt; diff --git a/socket-forwarder/main.cpp b/socket-forwarder/main.cpp index 658012c..1616df7 100644 --- a/socket-forwarder/main.cpp +++ b/socket-forwarder/main.cpp @@ -25,19 +25,15 @@ int main(int argc, char** argv) const std::string newClientPrefix = forwarder::getEnvironmentVariableValueOrDefault(forwarder::NEW_CLIENT_PREFIX, forwarder::NEW_CLIENT_PREFIX_DEFAULT); const unsigned short maxReadInSize = std::atoi(forwarder::getEnvironmentVariableValueOrDefault(forwarder::MAX_READ_IN_SIZE, std::to_string(forwarder::MAX_READ_IN_DEFAULT)).c_str()); - const bool debug = forwarder::getEnvironmentVariableValue(forwarder::DEBUG).has_value(); LOG4CXX_INFO(logger, "Using new client prefix: [" << newClientPrefix << "]."); LOG4CXX_INFO(logger, "Using max read in size: [" << maxReadInSize << "]."); - - // TODO: Remove debug flag, use log4cxx configuration - LOG4CXX_INFO(logger, "DEBUG flag set to [" << debug << "]."); LOG4CXX_INFO(logger, "Binding to host address [" << forwarder::getEnvironmentVariableValueOrDefault(forwarder::HOST_ADDRESS, forwarder::HOST_ADDRESS_DEFAULT) << "]."); std::optional serverSocket = forwarder::setUpTcpServerSocket(argc > 1 ? std::make_optional(std::string(argv[1])) : std::nullopt); std::optional udpSocket = forwarder::setUpUDPSocket(argc > 2 ? std::make_optional(std::string(argv[2])) : std::nullopt); - forwarder::Forwarder forwarder(serverSocket, udpSocket, newClientPrefix, maxReadInSize, debug); + forwarder::Forwarder forwarder(serverSocket, udpSocket, newClientPrefix, maxReadInSize); std::vector udpPreconfiguredAddresses = forwarder::getPreconfiguredUDPAddresses(); if (!udpPreconfiguredAddresses.empty()) From 78ccd6117a33b3dd8798ee7709176bb759b047b1 Mon Sep 17 00:00:00 2001 From: Kilemonn Date: Tue, 15 Oct 2024 17:21:53 +0900 Subject: [PATCH 10/13] Resolve compilation errors when removing debug flag properly... --- socket-forwarder/forwarder/Forwarder.h | 2 +- tests/socket-forwarder/forwarder/TCPSocketForwarderTest.cpp | 2 +- tests/socket-forwarder/forwarder/UDPSocketForwarderTest.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/socket-forwarder/forwarder/Forwarder.h b/socket-forwarder/forwarder/Forwarder.h index ec315e6..5925f0e 100644 --- a/socket-forwarder/forwarder/Forwarder.h +++ b/socket-forwarder/forwarder/Forwarder.h @@ -65,7 +65,7 @@ namespace forwarder void addSocketToTCPGroup(const std::string&, kt::TCPSocket); public: - Forwarder(std::optional, std::optional, const std::string, const unsigned short, const bool); + Forwarder(std::optional, std::optional, const std::string, const unsigned short); void preConfigureTCPAddress(const std::string&, kt::SocketAddress); void addAddressToUDPGroup(kt::SocketAddress); diff --git a/tests/socket-forwarder/forwarder/TCPSocketForwarderTest.cpp b/tests/socket-forwarder/forwarder/TCPSocketForwarderTest.cpp index 47ecc46..3455e20 100644 --- a/tests/socket-forwarder/forwarder/TCPSocketForwarderTest.cpp +++ b/tests/socket-forwarder/forwarder/TCPSocketForwarderTest.cpp @@ -19,7 +19,7 @@ namespace forwarder kt::ServerSocket serverSocket; forwarder::Forwarder forwarder; protected: - TCPSocketForwarderTest() : serverSocket(kt::SocketType::Wifi), forwarder(serverSocket, std::nullopt, NEW_CLIENT_PREFIX_DEFAULT, MAX_READ_IN_DEFAULT, true) {} + TCPSocketForwarderTest() : serverSocket(kt::SocketType::Wifi), forwarder(serverSocket, std::nullopt, NEW_CLIENT_PREFIX_DEFAULT, MAX_READ_IN_DEFAULT) {} static void SetUpTestCase() { diff --git a/tests/socket-forwarder/forwarder/UDPSocketForwarderTest.cpp b/tests/socket-forwarder/forwarder/UDPSocketForwarderTest.cpp index 3a00b56..cdc830b 100644 --- a/tests/socket-forwarder/forwarder/UDPSocketForwarderTest.cpp +++ b/tests/socket-forwarder/forwarder/UDPSocketForwarderTest.cpp @@ -22,7 +22,7 @@ namespace forwarder UDPSocketForwarderTest() : udpSocket() { udpSocket.bind(); - forwarder = forwarder::Forwarder(std::nullopt, udpSocket, NEW_CLIENT_PREFIX_DEFAULT, MAX_READ_IN_DEFAULT, true); + forwarder = forwarder::Forwarder(std::nullopt, udpSocket, NEW_CLIENT_PREFIX_DEFAULT, MAX_READ_IN_DEFAULT); } static void SetUpTestCase() From 69d0ff506bb476d6f3f8ef61096c54743195353c Mon Sep 17 00:00:00 2001 From: Kilemonn Date: Tue, 15 Oct 2024 17:35:08 +0900 Subject: [PATCH 11/13] Tweak logging when provided preconfig list value is empty. --- socket-forwarder/sockets/Sockets.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/socket-forwarder/sockets/Sockets.cpp b/socket-forwarder/sockets/Sockets.cpp index 996652f..8cc0dd3 100644 --- a/socket-forwarder/sockets/Sockets.cpp +++ b/socket-forwarder/sockets/Sockets.cpp @@ -90,7 +90,7 @@ namespace forwarder std::vector parts = split(s, ":"); if (parts.size() == 1 && parts[0].empty()) { - LOG4CXX_WARN(logger, "[TCP] - Skipping processing address with value [" << s << "], expected format to be \":
:\"."); + // Don't even log since its empty } else if (parts.size() < 3) { @@ -144,7 +144,7 @@ namespace forwarder std::vector parts = split(s, ":"); if (parts.size() == 1 && parts[0].empty()) { - LOG4CXX_WARN(logger, "[UDP] - Skipping processing address with value [" << s << "], expected format to be \"
:\"."); + // Don't even log since its empty } else if (parts.size() < 2) { From 9bad741649d6a1813e593ca68fa222c926462845 Mon Sep 17 00:00:00 2001 From: Kilemonn Date: Wed, 16 Oct 2024 13:17:20 +0900 Subject: [PATCH 12/13] Initialise console logger by default. --- CMakeLists.txt | 1 + socket-forwarder/forwarder/Forwarder.h | 2 +- socket-forwarder/logger/Logger.cpp | 17 +++++++++++++++++ socket-forwarder/logger/Logger.h | 5 +++++ socket-forwarder/main.cpp | 7 +++---- tests/CMakeLists.txt | 1 + .../forwarder/TCPSocketForwarderTest.cpp | 3 ++- .../forwarder/UDPSocketForwarderTest.cpp | 3 ++- 8 files changed, 32 insertions(+), 7 deletions(-) create mode 100644 socket-forwarder/logger/Logger.cpp create mode 100644 socket-forwarder/logger/Logger.h diff --git a/CMakeLists.txt b/CMakeLists.txt index b44f742..2ce23f2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,6 +26,7 @@ set(FORWARDER_SOURCE socket-forwarder/environment/Environment.cpp socket-forwarder/forwarder/Forwarder.cpp socket-forwarder/sockets/Sockets.cpp + socket-forwarder/logger/Logger.cpp ) add_executable(SocketForwarder ${FORWARDER_SOURCE}) diff --git a/socket-forwarder/forwarder/Forwarder.h b/socket-forwarder/forwarder/Forwarder.h index 5925f0e..aa15b88 100644 --- a/socket-forwarder/forwarder/Forwarder.h +++ b/socket-forwarder/forwarder/Forwarder.h @@ -44,7 +44,7 @@ namespace forwarder std::optional> tcpRunningThreads = std::nullopt; std::optional> udpRunningThreads = std::nullopt; - log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger("SocketForwarder"); + log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger("Forwarder"); bool forwarderIsRunning = false; std::string newClientPrefix; unsigned short maxReadInSize; diff --git a/socket-forwarder/logger/Logger.cpp b/socket-forwarder/logger/Logger.cpp new file mode 100644 index 0000000..bd6eab4 --- /dev/null +++ b/socket-forwarder/logger/Logger.cpp @@ -0,0 +1,17 @@ +#include "Logger.h" + +#include +#include +#include +#include + +namespace forwarder +{ + void initialiseConsoleLogger() + { + log4cxx::PatternLayoutPtr patternLayout = std::make_shared(); + patternLayout->setConversionPattern("%d{yyyy-MM-dd HH:mm:ss} [%-5p] %c{1}:%L - %m%n"); + log4cxx::ConsoleAppenderPtr consoleAppender = std::make_shared(patternLayout); + log4cxx::BasicConfigurator::configure(consoleAppender); + } +} diff --git a/socket-forwarder/logger/Logger.h b/socket-forwarder/logger/Logger.h new file mode 100644 index 0000000..f49520a --- /dev/null +++ b/socket-forwarder/logger/Logger.h @@ -0,0 +1,5 @@ + +namespace forwarder +{ + void initialiseConsoleLogger(); +} diff --git a/socket-forwarder/main.cpp b/socket-forwarder/main.cpp index 1616df7..11fc520 100644 --- a/socket-forwarder/main.cpp +++ b/socket-forwarder/main.cpp @@ -8,18 +8,17 @@ #include "sockets/Sockets.h" #include "environment/Environment.h" #include "forwarder/Forwarder.h" +#include "logger/Logger.h" -#include -#include // Make sure version of built image matches const std::string VERSION = "0.4.0"; int main(int argc, char** argv) { - log4cxx::BasicConfigurator::configure(); + forwarder::initialiseConsoleLogger(); - auto logger = log4cxx::Logger::getLogger("SocketForwarder"); + auto logger = log4cxx::Logger::getLogger("main"); LOG4CXX_INFO(logger, "Running SocketForwarder v" << VERSION); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 71b6bb0..8c4be38 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -31,6 +31,7 @@ set(FORWARDER_SOURCE_FOR_TEST ../socket-forwarder/environment/Environment.cpp ../socket-forwarder/forwarder/Forwarder.cpp ../socket-forwarder/sockets/Sockets.cpp + ../socket-forwarder/logger/Logger.cpp ) add_executable(${PROJECT_NAME} ${FORWARDER_TEST_SOURCE} ${FORWARDER_SOURCE_FOR_TEST}) diff --git a/tests/socket-forwarder/forwarder/TCPSocketForwarderTest.cpp b/tests/socket-forwarder/forwarder/TCPSocketForwarderTest.cpp index 3455e20..c693fb2 100644 --- a/tests/socket-forwarder/forwarder/TCPSocketForwarderTest.cpp +++ b/tests/socket-forwarder/forwarder/TCPSocketForwarderTest.cpp @@ -6,6 +6,7 @@ #include "../../../socket-forwarder/environment/Environment.h" #include "../../../socket-forwarder/forwarder/Forwarder.h" +#include "../../../socket-forwarder/logger/Logger.h" #include @@ -24,7 +25,7 @@ namespace forwarder static void SetUpTestCase() { log4cxx::BasicConfigurator::resetConfiguration(); - log4cxx::BasicConfigurator::configure(); + forwarder::initialiseConsoleLogger(); log4cxx::Logger::getRootLogger()->setLevel(log4cxx::Level::getDebug()); } diff --git a/tests/socket-forwarder/forwarder/UDPSocketForwarderTest.cpp b/tests/socket-forwarder/forwarder/UDPSocketForwarderTest.cpp index cdc830b..200e4f7 100644 --- a/tests/socket-forwarder/forwarder/UDPSocketForwarderTest.cpp +++ b/tests/socket-forwarder/forwarder/UDPSocketForwarderTest.cpp @@ -5,6 +5,7 @@ #include "../../../socket-forwarder/environment/Environment.h" #include "../../../socket-forwarder/forwarder/Forwarder.h" +#include "../../../socket-forwarder/logger/Logger.h" #include @@ -28,7 +29,7 @@ namespace forwarder static void SetUpTestCase() { log4cxx::BasicConfigurator::resetConfiguration(); - log4cxx::BasicConfigurator::configure(); + forwarder::initialiseConsoleLogger(); log4cxx::Logger::getRootLogger()->setLevel(log4cxx::Level::getDebug()); } From b145d1976a8df04031a4a5061cd35a67abfc3b16 Mon Sep 17 00:00:00 2001 From: Kilemonn Date: Wed, 16 Oct 2024 13:33:47 +0900 Subject: [PATCH 13/13] Add pragma once to logger header. --- socket-forwarder/logger/Logger.h | 1 + 1 file changed, 1 insertion(+) diff --git a/socket-forwarder/logger/Logger.h b/socket-forwarder/logger/Logger.h index f49520a..28005fa 100644 --- a/socket-forwarder/logger/Logger.h +++ b/socket-forwarder/logger/Logger.h @@ -1,3 +1,4 @@ +#pragma once namespace forwarder {