Included source location info (file name and line #) in debug logging

This commit is contained in:
Nav
2025-01-09 21:58:56 +00:00
parent a3501153d4
commit 9c142d8b87
3 changed files with 18 additions and 7 deletions

View File

@@ -121,6 +121,8 @@ target_compile_options(
PUBLIC -Wsuggest-override PUBLIC -Wsuggest-override
PUBLIC -Wreorder PUBLIC -Wreorder
PUBLIC -fno-sized-deallocation PUBLIC -fno-sized-deallocation
PUBLIC -ffile-prefix-map=${CMAKE_SOURCE_DIR}/./src/=
PUBLIC -ffile-prefix-map=${CMAKE_SOURCE_DIR}/src/=
PUBLIC $<$<CONFIG:DEBUG>:-g> PUBLIC $<$<CONFIG:DEBUG>:-g>
# PUBLIC $<$<CONFIG:DEBUG>:-O0> # PUBLIC $<$<CONFIG:DEBUG>:-O0>
PUBLIC $<$<CONFIG:DEBUG>:-Ofast> PUBLIC $<$<CONFIG:DEBUG>:-Ofast>

View File

@@ -16,7 +16,7 @@ void Logger::silence() {
Logger::warningPrintingEnabled = false; Logger::warningPrintingEnabled = false;
} }
void Logger::log(const std::string& message, LogLevel level) { void Logger::log(const std::string& message, LogLevel level, std::optional<std::source_location> sourceLocation) {
using Services::DateTimeService; using Services::DateTimeService;
const auto timestamp = DateTimeService::currentDateTime(); const auto timestamp = DateTimeService::currentDateTime();
const auto threadName = Logger::threadName(); 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; std::cout << message << "\033[0m" << std::endl;
} }

View File

@@ -3,6 +3,8 @@
#include <cstdint> #include <cstdint>
#include <string> #include <string>
#include <mutex> #include <mutex>
#include <source_location>
#include <optional>
#include "src/ProjectConfig.hpp" #include "src/ProjectConfig.hpp"
#include "src/Services/DateTimeService.hpp" #include "src/Services/DateTimeService.hpp"
@@ -27,25 +29,28 @@ public:
static void info(const std::string& message) { static void info(const std::string& message) {
if (Logger::infoPrintingEnabled) { if (Logger::infoPrintingEnabled) {
Logger::log(message, LogLevel::INFO); Logger::log(message, LogLevel::INFO, std::nullopt);
} }
} }
static void warning(const std::string& message) { static void warning(const std::string& message) {
if (Logger::warningPrintingEnabled) { if (Logger::warningPrintingEnabled) {
Logger::log(message, LogLevel::WARNING); Logger::log(message, LogLevel::WARNING, std::nullopt);
} }
} }
static void error(const std::string& message) { static void error(const std::string& message) {
if (Logger::errorPrintingEnabled) { 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) { 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 inline std::mutex printMutex;
static void log(const std::string& message, LogLevel level); static void log(const std::string& message, LogLevel level, std::optional<std::source_location> sourceLocation);
static const std::string& threadName(); static const std::string& threadName();
}; };