From f119d98c705929ef718f5c08bad1776c5f315fa4 Mon Sep 17 00:00:00 2001 From: Quinten Mennen Date: Tue, 23 Sep 2025 15:37:32 +0200 Subject: [PATCH] fix(log): get rid of source_location --- webserv/log/FileChannel.cpp | 1 + webserv/log/Log.cpp | 45 +++++++++++++------------------------ webserv/log/Log.hpp | 40 ++++++++++++++++++++------------- webserv/main.cpp | 2 +- 4 files changed, 42 insertions(+), 46 deletions(-) diff --git a/webserv/log/FileChannel.cpp b/webserv/log/FileChannel.cpp index 617d016..0e40573 100644 --- a/webserv/log/FileChannel.cpp +++ b/webserv/log/FileChannel.cpp @@ -4,6 +4,7 @@ #include #include #include +#include FileChannel::FileChannel(const std::string &filename, std::ios_base::openmode mode, Log::Level logLevel) : filename_(filename), fileStream_(filename, mode) diff --git a/webserv/log/Log.cpp b/webserv/log/Log.cpp index 5571ca0..732cb45 100644 --- a/webserv/log/Log.cpp +++ b/webserv/log/Log.cpp @@ -6,7 +6,6 @@ #include #include #include -#include Log::Log() { @@ -54,20 +53,12 @@ Log &Log::getInstance() return instance; } -void Log::log(Log::Level level, const std::string &message, const std::map &context, - const std::source_location &location) -{ +void Log::log(Log::Level level, const std::string &message, const std::map &context) +{ for (auto &it : channels_) { - std::string extendedMessage; - extendedMessage += message + "\n\t| "; - - extendedMessage += std::filesystem::path(location.file_name()).filename().string(); - extendedMessage += ":" + std::to_string(location.line()) + ":" + std::to_string(location.column()); - extendedMessage += " (" + std::string(location.function_name()) + ")"; - // extendedMessage += " | " + message; - it.second->log(level, extendedMessage, context); + it.second->log(level, message, context); } } @@ -79,40 +70,34 @@ int Log::getElapsedTime() return static_cast(elapsed); } -void Log::trace(const std::string &message, const std::map &context, - const std::source_location &location) +void Log::trace(const std::string &message, const std::map &context) { - getInstance().log(Log::Level::Trace, message, context, location); + getInstance().log(Log::Level::Trace, message, context); } -void Log::debug(const std::string &message, const std::map &context, - const std::source_location &location) +void Log::debug(const std::string &message, const std::map &context) { - getInstance().log(Log::Level::Debug, message, context, location); + getInstance().log(Log::Level::Debug, message, context); } -void Log::info(const std::string &message, const std::map &context, - const std::source_location &location) +void Log::info(const std::string &message, const std::map &context) { - getInstance().log(Log::Level::Info, message, context, location); + getInstance().log(Log::Level::Info, message, context); } -void Log::warning(const std::string &message, const std::map &context, - const std::source_location &location) +void Log::warning(const std::string &message, const std::map &context) { - getInstance().log(Log::Level::Warn, message, context, location); + getInstance().log(Log::Level::Warn, message, context); } -void Log::error(const std::string &message, const std::map &context, - const std::source_location &location) +void Log::error(const std::string &message, const std::map &context) { - getInstance().log(Log::Level::Error, message, context, location); + getInstance().log(Log::Level::Error, message, context); } -void Log::fatal(const std::string &message, const std::map &context, - const std::source_location &location) +void Log::fatal(const std::string &message, const std::map &context) { - getInstance().log(Log::Level::Fatal, message, context, location); + getInstance().log(Log::Level::Fatal, message, context); } std::string Log::logLevelToString(Log::Level level) diff --git a/webserv/log/Log.hpp b/webserv/log/Log.hpp index c5767be..b957cce 100644 --- a/webserv/log/Log.hpp +++ b/webserv/log/Log.hpp @@ -2,15 +2,32 @@ #include #include +#include #include #include -#include #include #include #include class Channel; // Forward declaration +constexpr const char *extractFilename(const char *path) +{ + const char *filename = path; + while (*path != '\0') + { + if (*path == '/' || *path == '\\') + { + filename = path + 1; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) + } + ++path; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) + } + return filename; +} + +#define LOCATION \ + (std::string(extractFilename(__FILE__)) + ":" + std::to_string(__LINE__) + " (" + std::string(__FUNCTION__) + ")") + class Log { public: @@ -29,8 +46,7 @@ class Log Log &operator=(const Log &other) = delete; Log &operator=(Log &&other) = delete; - void log(Level level, const std::string &message, const std::map &context, - const std::source_location &location); + void log(Level level, const std::string &message, const std::map &context); static void setFileChannel(const std::string &filename, std::ios_base::openmode mode = std::ios_base::app, Level logLevel = Level::Trace); @@ -38,18 +54,12 @@ class Log static int getElapsedTime(); - static void trace(const std::string &message, const std::map &context = {}, - const std::source_location &location = std::source_location::current()); - static void debug(const std::string &message, const std::map &context = {}, - const std::source_location &location = std::source_location::current()); - static void info(const std::string &message, const std::map &context = {}, - const std::source_location &location = std::source_location::current()); - static void warning(const std::string &message, const std::map &context = {}, - const std::source_location &location = std::source_location::current()); - static void error(const std::string &message, const std::map &context = {}, - const std::source_location &location = std::source_location::current()); - static void fatal(const std::string &message, const std::map &context = {}, - const std::source_location &location = std::source_location::current()); + static void trace(const std::string &message, const std::map &context = {}); + static void debug(const std::string &message, const std::map &context = {}); + static void info(const std::string &message, const std::map &context = {}); + static void warning(const std::string &message, const std::map &context = {}); + static void error(const std::string &message, const std::map &context = {}); + static void fatal(const std::string &message, const std::map &context = {}); static std::string logLevelToString(Level level); static const char *logLevelToColor(Level level); diff --git a/webserv/main.cpp b/webserv/main.cpp index 0e97fd3..5edcf13 100644 --- a/webserv/main.cpp +++ b/webserv/main.cpp @@ -18,7 +18,7 @@ int main(int argc, char **argv) Log::setFileChannel("webserv.log", std::ios_base::app, Log::Level::Trace); Log::setStdoutChannel(Log::Level::Trace); Log::info("\n======================\nStarting webserv...\n======================\n"); - Log::warning("Testing context", {{"key1", "value1"}, {"key2", "value2"}}); + ConfigManager::getInstance().init(argv[1]); // NOLINT Server server(ConfigManager::getInstance()); server.start();