updated logger to support std::format (upgrading to gcc >13 was one of the most tedious things i've ever done)

This commit is contained in:
2026-07-18 03:42:43 -05:00
parent 235530c03d
commit 1c74c04db3
6 changed files with 25 additions and 22 deletions

View File

@@ -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

View File

@@ -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);

View File

@@ -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
)

View File

@@ -1,13 +1,15 @@
#include "LoggerService.hpp"
#include <chrono> // Tracking the time when the log function is called
#include <string>
#include <source_location>
#include "LoggerService.hpp"
// #include <source_location> TODO: investigate adding this back in
#include <iostream>
#include <iomanip>
#include <fstream>
#include <filesystem>
#include <algorithm>
#include <cstdarg>
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;
}

View File

@@ -4,6 +4,7 @@
#include <string>
#include <fstream>
#include <source_location>
#include <format>
#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 <source_location>
template<typename... Args>
void log(std::string component, LogFlag flag, std::format_string<Args...> message, Args&&... args) {
write(component, flag, std::format(message, std::forward<Args>(args)...));
}
private:
@@ -38,4 +42,6 @@ private:
std::vector<LogFlag> activeFlags_;
LoggerParams configuration_;
void write(std::string component, LogFlag flag, std::string message);
};

View File

@@ -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);