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:
@@ -50,7 +50,7 @@ CMake: https://cmake.org/download/ \
|
|||||||
Qt6::QML: https://www.qt.io/development/download-qt-installer-oss \
|
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"`
|
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
|
Clone repository
|
||||||
```PowerShell
|
```PowerShell
|
||||||
|
|||||||
@@ -74,8 +74,7 @@ ErrorCode TcpClient::init() {
|
|||||||
return ErrorCode::Error;
|
return ErrorCode::Error;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string msg = "Received " + std::to_string(receiveBytes) + " bytes back from server.";
|
logger_->log("TcpClient", LogFlag::Debug, "Received {} bytes back from the server", receiveBytes);
|
||||||
logger_->log("TcpClient", LogFlag::Debug, msg);
|
|
||||||
|
|
||||||
close(socketFd);
|
close(socketFd);
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,5 @@ target_link_libraries(accordion-common PUBLIC
|
|||||||
)
|
)
|
||||||
|
|
||||||
target_compile_definitions(accordion-common PRIVATE
|
target_compile_definitions(accordion-common PRIVATE
|
||||||
# pass in some compiler macros
|
|
||||||
BINARY_DIR="${CMAKE_BINARY_DIR}" # useful for runtime filepaths
|
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,13 +1,15 @@
|
|||||||
|
|
||||||
|
#include "LoggerService.hpp"
|
||||||
|
|
||||||
#include <chrono> // Tracking the time when the log function is called
|
#include <chrono> // Tracking the time when the log function is called
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <source_location>
|
// #include <source_location> TODO: investigate adding this back in
|
||||||
#include "LoggerService.hpp"
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <cstdarg>
|
||||||
|
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
@@ -58,8 +60,7 @@ LoggerService::~LoggerService() {
|
|||||||
if(outfile_) outfile_.close();
|
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
|
// check if flag is in the list of active flags
|
||||||
bool culled = true;
|
bool culled = true;
|
||||||
for(LogFlag& testFlag : activeFlags_) {
|
for(LogFlag& testFlag : activeFlags_) {
|
||||||
@@ -83,14 +84,15 @@ void LoggerService::log(std::string component, LogFlag flag, std::string message
|
|||||||
|
|
||||||
level = LogFlagStrings[flag];
|
level = LogFlagStrings[flag];
|
||||||
|
|
||||||
// level.append(7 - level.length(), ' ') pads out the level string with whitespace so every line is aligned the same
|
// level.append(7 - level.length(), ' ') pads out the level string with whitespace so every line is aligned the sam
|
||||||
// it looked weird though
|
|
||||||
finalmessage = finalmessage + "[" + level + "] ";
|
finalmessage = finalmessage + "[" + level + "] ";
|
||||||
finalmessage = finalmessage + message + " ";
|
finalmessage = finalmessage + message + " ";
|
||||||
|
|
||||||
|
/* Removed because didn't want to bother with variadics + default arguments
|
||||||
if (configuration_.showSourceTrace) {
|
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) {
|
if(configuration_.coutEnabled) {
|
||||||
std::cout << finalmessage << std::endl;
|
std::cout << finalmessage << std::endl;
|
||||||
@@ -99,5 +101,5 @@ void LoggerService::log(std::string component, LogFlag flag, std::string message
|
|||||||
if(configuration_.fileEnabled) {
|
if(configuration_.fileEnabled) {
|
||||||
outfile_ << finalmessage << std::endl;
|
outfile_ << finalmessage << std::endl;
|
||||||
}
|
}
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <source_location>
|
#include <source_location>
|
||||||
|
#include <format>
|
||||||
|
|
||||||
#include "config/ConfigService.hpp"
|
#include "config/ConfigService.hpp"
|
||||||
#include "config/LoggerConfig.hpp"
|
#include "config/LoggerConfig.hpp"
|
||||||
@@ -30,7 +31,10 @@ public:
|
|||||||
LoggerService(ConfigService* config, const std::string& loggerId);
|
LoggerService(ConfigService* config, const std::string& loggerId);
|
||||||
~LoggerService();
|
~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:
|
private:
|
||||||
|
|
||||||
@@ -38,4 +42,6 @@ private:
|
|||||||
std::vector<LogFlag> activeFlags_;
|
std::vector<LogFlag> activeFlags_;
|
||||||
|
|
||||||
LoggerParams configuration_;
|
LoggerParams configuration_;
|
||||||
|
|
||||||
|
void write(std::string component, LogFlag flag, std::string message);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ ErrorCode TcpServer::init() {
|
|||||||
|
|
||||||
memset(buffer, 0x0, sizeof(buffer));
|
memset(buffer, 0x0, sizeof(buffer));
|
||||||
listen(socketFd, 10);
|
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);
|
unsigned int sinSize = sizeof(struct sockaddr);
|
||||||
clientFd = accept(socketFd, (struct sockaddr*)&clientAddress, &sinSize);
|
clientFd = accept(socketFd, (struct sockaddr*)&clientAddress, &sinSize);
|
||||||
@@ -66,20 +66,17 @@ ErrorCode TcpServer::init() {
|
|||||||
struct sockaddr_in peerAddress;
|
struct sockaddr_in peerAddress;
|
||||||
unsigned int len = sizeof(peerAddress);
|
unsigned int len = sizeof(peerAddress);
|
||||||
char serverIp[20];
|
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, "Accept socket... Client address: {} Port: {}", inet_ntoa(clientAddress.sin_addr), ntohs(clientAddress.sin_port));
|
||||||
logger_->log("TcpServer", LogFlag::Debug, msg);
|
|
||||||
getsockname(clientFd, (struct sockaddr*)&peerAddress, &len);
|
getsockname(clientFd, (struct sockaddr*)&peerAddress, &len);
|
||||||
inet_ntop(AF_INET, &peerAddress, serverIp, sizeof(serverIp));
|
inet_ntop(AF_INET, &peerAddress, serverIp, sizeof(serverIp));
|
||||||
msg = "Accept socket... Server address: " + std::string(serverIp);
|
logger_->log("TcpServer", LogFlag::Debug, "Accept socket... Server address: {}", serverIp);
|
||||||
logger_->log("TcpServer", LogFlag::Debug, msg);
|
|
||||||
|
|
||||||
receiveBytes = recv(clientFd, buffer, sizeof(buffer), 0);
|
receiveBytes = recv(clientFd, buffer, sizeof(buffer), 0);
|
||||||
if(receiveBytes < 0) {
|
if(receiveBytes < 0) {
|
||||||
logger_->log("TcpServer", LogFlag::Error, "Unable to receive from client.");
|
logger_->log("TcpServer", LogFlag::Error, "Unable to receive from client.");
|
||||||
return ErrorCode::Error;
|
return ErrorCode::Error;
|
||||||
}
|
}
|
||||||
msg = "Received " + std::to_string(receiveBytes) + " bytes from client: " + std::to_string(buffer[2]);
|
logger_->log("TcpClient", LogFlag::Debug, "Received {} bytes from client: 0x{:x}", receiveBytes, buffer[2]);
|
||||||
logger_->log("TcpClient", LogFlag::Debug, msg);
|
|
||||||
|
|
||||||
// echo back
|
// echo back
|
||||||
sendBytes = send(clientFd, buffer, sizeof(buffer), 0);
|
sendBytes = send(clientFd, buffer, sizeof(buffer), 0);
|
||||||
@@ -87,7 +84,7 @@ ErrorCode TcpServer::init() {
|
|||||||
logger_->log("TcpServer", LogFlag::Error, "Unable to send to client.");
|
logger_->log("TcpServer", LogFlag::Error, "Unable to send to client.");
|
||||||
return ErrorCode::Error;
|
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);
|
close(socketFd);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user