From 9c142d8b877bcaedc6001b41e004057e4c1d77ed Mon Sep 17 00:00:00 2001 From: Nav Date: Thu, 9 Jan 2025 21:58:56 +0000 Subject: [PATCH] Included source location info (file name and line #) in debug logging --- CMakeLists.txt | 2 ++ src/Logger/Logger.cpp | 6 +++++- src/Logger/Logger.hpp | 17 +++++++++++------ 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d84dcf53..ec3f67b0 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -121,6 +121,8 @@ target_compile_options( PUBLIC -Wsuggest-override PUBLIC -Wreorder PUBLIC -fno-sized-deallocation + PUBLIC -ffile-prefix-map=${CMAKE_SOURCE_DIR}/./src/= + PUBLIC -ffile-prefix-map=${CMAKE_SOURCE_DIR}/src/= PUBLIC $<$:-g> # PUBLIC $<$:-O0> PUBLIC $<$:-Ofast> diff --git a/src/Logger/Logger.cpp b/src/Logger/Logger.cpp index 79ccadb4..b5a82a96 100644 --- a/src/Logger/Logger.cpp +++ b/src/Logger/Logger.cpp @@ -16,7 +16,7 @@ void Logger::silence() { Logger::warningPrintingEnabled = false; } -void Logger::log(const std::string& message, LogLevel level) { +void Logger::log(const std::string& message, LogLevel level, std::optional sourceLocation) { using Services::DateTimeService; const auto timestamp = DateTimeService::currentDateTime(); const auto threadName = Logger::threadName(); @@ -58,6 +58,10 @@ void Logger::log(const std::string& message, LogLevel level) { } } + if (sourceLocation.has_value()) { + std::cout << "[" << sourceLocation->file_name() << ":" << sourceLocation->line() << "] "; + } + std::cout << message << "\033[0m" << std::endl; } diff --git a/src/Logger/Logger.hpp b/src/Logger/Logger.hpp index 4c5c1fdb..289e2bce 100644 --- a/src/Logger/Logger.hpp +++ b/src/Logger/Logger.hpp @@ -3,6 +3,8 @@ #include #include #include +#include +#include #include "src/ProjectConfig.hpp" #include "src/Services/DateTimeService.hpp" @@ -27,25 +29,28 @@ public: static void info(const std::string& message) { if (Logger::infoPrintingEnabled) { - Logger::log(message, LogLevel::INFO); + Logger::log(message, LogLevel::INFO, std::nullopt); } } static void warning(const std::string& message) { if (Logger::warningPrintingEnabled) { - Logger::log(message, LogLevel::WARNING); + Logger::log(message, LogLevel::WARNING, std::nullopt); } } static void error(const std::string& message) { if (Logger::errorPrintingEnabled) { - Logger::log(message, LogLevel::ERROR); + Logger::log(message, LogLevel::ERROR, std::nullopt); } } - static void debug(const std::string& message) { + static void debug( + const std::string& message, + const std::source_location sourceLocation = std::source_location::current() + ) { if (Logger::debugPrintingEnabled) { - Logger::log(message, LogLevel::DEBUG); + Logger::log(message, LogLevel::DEBUG, sourceLocation); } } @@ -57,6 +62,6 @@ private: static inline std::mutex printMutex; - static void log(const std::string& message, LogLevel level); + static void log(const std::string& message, LogLevel level, std::optional sourceLocation); static const std::string& threadName(); };