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:
@@ -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
|
||||
|
||||
)
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user