diff --git a/README.md b/README.md index 0354647..cf8c68c 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ CMake: https://cmake.org/download/ \ Qt6::QML: https://www.qt.io/development/download-qt-installer-oss \ Note: you must export the Qt install location to your environment. E.g. add the following to a powershell profile: `$env:Qt6_DIR = "C:\Qt\6.10.1\msvc2022_64"` -Compiler that supports C++20. Builds have been tested with GCC12 & MSVC17 +Compiler that supports C++20. Builds have been tested with GCC13 & MSVC17 Clone repository ```PowerShell diff --git a/client/src/TcpClient.cpp b/client/src/TcpClient.cpp index d2b267b..888483f 100644 --- a/client/src/TcpClient.cpp +++ b/client/src/TcpClient.cpp @@ -74,8 +74,7 @@ ErrorCode TcpClient::init() { return ErrorCode::Error; } - std::string msg = "Received " + std::to_string(receiveBytes) + " bytes back from server."; - logger_->log("TcpClient", LogFlag::Debug, msg); + logger_->log("TcpClient", LogFlag::Debug, "Received {} bytes back from the server", receiveBytes); close(socketFd); diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 2574952..3e44c1b 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -15,6 +15,5 @@ target_link_libraries(accordion-common PUBLIC ) target_compile_definitions(accordion-common PRIVATE - # pass in some compiler macros - BINARY_DIR="${CMAKE_BINARY_DIR}" # useful for runtime filepaths + ) diff --git a/common/LoggerService.cpp b/common/LoggerService.cpp index cd220a8..791f9ac 100644 --- a/common/LoggerService.cpp +++ b/common/LoggerService.cpp @@ -1,13 +1,15 @@ +#include "LoggerService.hpp" + #include // Tracking the time when the log function is called #include -#include -#include "LoggerService.hpp" +// #include TODO: investigate adding this back in #include #include #include #include #include +#include namespace fs = std::filesystem; @@ -58,8 +60,7 @@ LoggerService::~LoggerService() { if(outfile_) outfile_.close(); } -void LoggerService::log(std::string component, LogFlag flag, std::string message, std::source_location Source) { - +void LoggerService::write(std::string component, LogFlag flag, std::string message) { // check if flag is in the list of active flags bool culled = true; for(LogFlag& testFlag : activeFlags_) { @@ -83,14 +84,15 @@ void LoggerService::log(std::string component, LogFlag flag, std::string message level = LogFlagStrings[flag]; - // level.append(7 - level.length(), ' ') pads out the level string with whitespace so every line is aligned the same - // it looked weird though + // level.append(7 - level.length(), ' ') pads out the level string with whitespace so every line is aligned the sam finalmessage = finalmessage + "[" + level + "] "; finalmessage = finalmessage + message + " "; + /* Removed because didn't want to bother with variadics + default arguments if (configuration_.showSourceTrace) { - finalmessage = finalmessage + "[Function: " + Source.function_name() + "]" + " " + "[Line: " + std::to_string(Source.line()) + "]" + " " + "[File: " + Source.file_name() + "]"; + finalmessage = finalmessage + "[Function: " + Source.function_name() + "]" + " " + "[Line: " + std::to_string(source.line()) + "]" + " " + "[File: " + source.file_name() + "]"; } + */ if(configuration_.coutEnabled) { std::cout << finalmessage << std::endl; @@ -99,5 +101,5 @@ void LoggerService::log(std::string component, LogFlag flag, std::string message if(configuration_.fileEnabled) { outfile_ << finalmessage << std::endl; } - return; } + diff --git a/common/LoggerService.hpp b/common/LoggerService.hpp index 4fb35f4..233d725 100644 --- a/common/LoggerService.hpp +++ b/common/LoggerService.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "config/ConfigService.hpp" #include "config/LoggerConfig.hpp" @@ -30,7 +31,10 @@ public: LoggerService(ConfigService* config, const std::string& loggerId); ~LoggerService(); - void log(std::string component, LogFlag flag, std::string message, std::source_location Source = std::source_location::current()); // Using the + template + void log(std::string component, LogFlag flag, std::format_string message, Args&&... args) { + write(component, flag, std::format(message, std::forward(args)...)); + } private: @@ -38,4 +42,6 @@ private: std::vector activeFlags_; LoggerParams configuration_; + + void write(std::string component, LogFlag flag, std::string message); }; diff --git a/server/src/TcpServer.cpp b/server/src/TcpServer.cpp index fd39bef..3b79d3a 100644 --- a/server/src/TcpServer.cpp +++ b/server/src/TcpServer.cpp @@ -54,7 +54,7 @@ ErrorCode TcpServer::init() { memset(buffer, 0x0, sizeof(buffer)); listen(socketFd, 10); - logger_->log("TcpServer", LogFlag::Debug, "Server listening..."); + logger_->log("TcpServer", LogFlag::Debug, "Server listening on port {}...", configuration_.port); unsigned int sinSize = sizeof(struct sockaddr); clientFd = accept(socketFd, (struct sockaddr*)&clientAddress, &sinSize); @@ -66,20 +66,17 @@ ErrorCode TcpServer::init() { struct sockaddr_in peerAddress; unsigned int len = sizeof(peerAddress); char serverIp[20]; - std::string msg = "Accept socket... Client address: " + std::string(inet_ntoa(clientAddress.sin_addr)) + " Port: " + std::to_string(ntohs(clientAddress.sin_port)); - logger_->log("TcpServer", LogFlag::Debug, msg); + logger_->log("TcpServer", LogFlag::Debug, "Accept socket... Client address: {} Port: {}", inet_ntoa(clientAddress.sin_addr), ntohs(clientAddress.sin_port)); getsockname(clientFd, (struct sockaddr*)&peerAddress, &len); inet_ntop(AF_INET, &peerAddress, serverIp, sizeof(serverIp)); - msg = "Accept socket... Server address: " + std::string(serverIp); - logger_->log("TcpServer", LogFlag::Debug, msg); + logger_->log("TcpServer", LogFlag::Debug, "Accept socket... Server address: {}", serverIp); receiveBytes = recv(clientFd, buffer, sizeof(buffer), 0); if(receiveBytes < 0) { logger_->log("TcpServer", LogFlag::Error, "Unable to receive from client."); return ErrorCode::Error; } - msg = "Received " + std::to_string(receiveBytes) + " bytes from client: " + std::to_string(buffer[2]); - logger_->log("TcpClient", LogFlag::Debug, msg); + logger_->log("TcpClient", LogFlag::Debug, "Received {} bytes from client: 0x{:x}", receiveBytes, buffer[2]); // echo back sendBytes = send(clientFd, buffer, sizeof(buffer), 0); @@ -87,7 +84,7 @@ ErrorCode TcpServer::init() { logger_->log("TcpServer", LogFlag::Error, "Unable to send to client."); return ErrorCode::Error; } - msg = "Echoed " + std::to_string(sendBytes) + " bytes back to client."; + logger_->log("TcpClient", LogFlag::Debug, "Echoed {} bytes back to the client.", sendBytes); close(socketFd);