From c401b5fbbd610a7f30775df0d602b9c51b10eb3d Mon Sep 17 00:00:00 2001 From: whaffman Date: Wed, 8 Oct 2025 17:21:48 +0200 Subject: [PATCH] refactor(log): reduce function calls --- webserv/log/Channel.cpp | 9 ------ webserv/log/Channel.hpp | 6 ++-- webserv/log/FileChannel.cpp | 8 ++--- webserv/log/FileChannel.hpp | 2 +- webserv/log/Log.cpp | 58 +++++++++++++++++++++++++---------- webserv/log/Log.hpp | 26 +++++++++------- webserv/log/StdoutChannel.cpp | 10 ++---- webserv/log/StdoutChannel.hpp | 2 +- 8 files changed, 65 insertions(+), 56 deletions(-) diff --git a/webserv/log/Channel.cpp b/webserv/log/Channel.cpp index 9c89028..322cf57 100644 --- a/webserv/log/Channel.cpp +++ b/webserv/log/Channel.cpp @@ -4,15 +4,6 @@ #include // for basic_ostream, operator<<, basic_stringstream, basic_istream, basic_istringstream, right, istringstream, stringstream #include // for get -Log::Level Channel::getLogLevel() const -{ - return logLevel_; -} - -void Channel::setLogLevel(Log::Level level) -{ - logLevel_ = level; -} std::string Channel::printContext(const std::map &context) { diff --git a/webserv/log/Channel.hpp b/webserv/log/Channel.hpp index 490de56..a8f7915 100644 --- a/webserv/log/Channel.hpp +++ b/webserv/log/Channel.hpp @@ -18,10 +18,10 @@ class Channel const std::map &context = {}) = 0; protected: - [[nodiscard]] Log::Level getLogLevel() const; + [[nodiscard]] static std::string printContext(const std::map &context); - void setLogLevel(Log::Level level); + private: - Log::Level logLevel_{Log::Level::Trace}; + }; \ No newline at end of file diff --git a/webserv/log/FileChannel.cpp b/webserv/log/FileChannel.cpp index e5435b3..e2c4bf6 100644 --- a/webserv/log/FileChannel.cpp +++ b/webserv/log/FileChannel.cpp @@ -9,10 +9,9 @@ struct tm; -FileChannel::FileChannel(const std::string &filename, std::ios_base::openmode mode, Log::Level logLevel) +FileChannel::FileChannel(const std::string &filename, std::ios_base::openmode mode) : filename_(filename), fileStream_(filename, mode) { - setLogLevel(logLevel); if (!fileStream_.is_open()) { std::cerr << "Failed to open log file: " << filename << '\n'; @@ -30,10 +29,7 @@ FileChannel::~FileChannel() void FileChannel::log(const Log::Level &logLevel, const std::string &message, const std::map &context) { - if (logLevel < getLogLevel()) - { - return; - } + if (!fileStream_.is_open()) { std::cerr << "Log file is not open: " << filename_ << '\n'; diff --git a/webserv/log/FileChannel.hpp b/webserv/log/FileChannel.hpp index 9490817..eef6c58 100644 --- a/webserv/log/FileChannel.hpp +++ b/webserv/log/FileChannel.hpp @@ -10,7 +10,7 @@ class FileChannel : public Channel { public: - FileChannel(const std::string &filename, std::ios_base::openmode mode, Log::Level logLevel = Log::Level::Trace); + FileChannel(const std::string &filename, std::ios_base::openmode mode); FileChannel(const FileChannel &other) = delete; FileChannel(FileChannel &&other) = delete; diff --git a/webserv/log/Log.cpp b/webserv/log/Log.cpp index 5b5fdbf..02ca2c0 100644 --- a/webserv/log/Log.cpp +++ b/webserv/log/Log.cpp @@ -1,7 +1,6 @@ -#include - #include // for Channel #include // for FileChannel +#include #include // for StdoutChannel #include // for duration_cast, operator-, steady_clock, duration, seconds @@ -16,7 +15,7 @@ Log::Log() start_time_ = std::chrono::steady_clock::now(); } -void Log::setStdoutChannel(Log::Level logLevel) +void Log::setStdoutChannel() { Log &log = getInstance(); if (log.channels_.contains("stdout")) @@ -25,7 +24,7 @@ void Log::setStdoutChannel(Log::Level logLevel) } try { - log.channels_["stdout"] = std::make_unique(logLevel); + log.channels_["stdout"] = std::make_unique(); } catch (const std::exception &e) { @@ -33,7 +32,7 @@ void Log::setStdoutChannel(Log::Level logLevel) } } -void Log::setFileChannel(const std::string &filename, std::ios_base::openmode mode, Log::Level logLevel) +void Log::setFileChannel(const std::string &filename, std::ios_base::openmode mode) { Log &log = getInstance(); if (log.channels_.contains("file")) @@ -42,7 +41,7 @@ void Log::setFileChannel(const std::string &filename, std::ios_base::openmode mo } try { - log.channels_["file"] = std::make_unique(filename, mode, logLevel); + log.channels_["file"] = std::make_unique(filename, mode); } catch (const std::exception &e) { @@ -56,7 +55,7 @@ Log &Log::getInstance() return instance; } -void Log::log(Log::Level level, const std::string &message, const std::map &context) +void Log::log(Level level, const std::string &message, const std::map &context) { for (auto &it : channels_) { @@ -75,36 +74,61 @@ int Log::getElapsedTime() void Log::trace(const std::string &message, const std::map &context) { - getInstance().log(Log::Level::Trace, message, context); + if constexpr (COMPILE_TIME_LOG_LEVEL > Level::Trace) + { + return; + } + getInstance().log(Level::Trace, message, context); } void Log::debug(const std::string &message, const std::map &context) { - getInstance().log(Log::Level::Debug, message, context); + if constexpr (COMPILE_TIME_LOG_LEVEL > Level::Debug) + { + return; + } + getInstance().log(Level::Debug, message, context); } void Log::info(const std::string &message, const std::map &context) { - getInstance().log(Log::Level::Info, message, context); + if constexpr (COMPILE_TIME_LOG_LEVEL > Level::Info) + { + return; + } + getInstance().log(Level::Info, message, context); } void Log::warning(const std::string &message, const std::map &context) { - getInstance().log(Log::Level::Warn, message, context); + if constexpr (COMPILE_TIME_LOG_LEVEL > Level::Warn) + { + return; + } + getInstance().log(Level::Warn, message, context); } void Log::error(const std::string &message, const std::map &context) { - getInstance().log(Log::Level::Error, message, context); + if constexpr (COMPILE_TIME_LOG_LEVEL > Level::Error) + { + return; + } + getInstance().log(Level::Error, message, context); } void Log::fatal(const std::string &message, const std::map &context) { - getInstance().log(Log::Level::Fatal, message, context); + if constexpr (COMPILE_TIME_LOG_LEVEL > Level::Fatal) + { + return; + } + getInstance().log(Level::Fatal, message, context); } -std::string Log::logLevelToString(Log::Level level) +std::string Log::logLevelToString(Level level) { + for (const auto &mapping : LOG_LEVEL_MAP) { if (mapping.level == level) @@ -115,7 +139,7 @@ std::string Log::logLevelToString(Log::Level level) return "UNKNOWN"; } -const char *Log::logLevelToColor(Log::Level level) +const char *Log::logLevelToColor(Level level) { for (const auto &mapping : LOG_LEVEL_MAP) { @@ -127,7 +151,7 @@ const char *Log::logLevelToColor(Log::Level level) return RESET_COLOR; // Default to reset } -std::string Log::logLevelToColoredString(Log::Level level) +std::string Log::logLevelToColoredString(Level level) { return std::string(Log::logLevelToColor(level)) + Log::logLevelToString(level) + RESET_COLOR; } @@ -141,5 +165,5 @@ Log::Level Log::stringToLogLevel(const std::string &level) return mapping.level; } } - return Log::Level::Info; // Default fallback + return Level::Info; // Default fallback } \ No newline at end of file diff --git a/webserv/log/Log.hpp b/webserv/log/Log.hpp index d416bcc..532a58a 100644 --- a/webserv/log/Log.hpp +++ b/webserv/log/Log.hpp @@ -29,10 +29,16 @@ constexpr const char *extractFilename(const char *path) #define LOCATION \ (std::string(extractFilename(__FILE__)) + ":" + std::to_string(__LINE__) + " (" + std::string(__FUNCTION__) + ")") + class Log { public: - enum class Level : uint8_t + Log(const Log &other) = delete; + Log(Log &&other) = delete; + Log &operator=(const Log &other) = delete; + Log &operator=(Log &&other) = delete; + + enum class Level : uint8_t { Trace = 0, Debug = 1, @@ -42,16 +48,12 @@ class Log Fatal = 5 }; - Log(const Log &other) = delete; - Log(Log &&other) = delete; - Log &operator=(const Log &other) = delete; - Log &operator=(Log &&other) = delete; - 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); - static void setStdoutChannel(Level logLevel = Level::Trace); + static constexpr Log::Level COMPILE_TIME_LOG_LEVEL = Log::Level::Info; + + static void setFileChannel(const std::string &filename, std::ios_base::openmode mode = std::ios_base::app); + static void setStdoutChannel(); static int getElapsedTime(); @@ -65,7 +67,7 @@ class Log static std::string logLevelToString(Level level); static const char *logLevelToColor(Level level); static std::string logLevelToColoredString(Level level); - static Log::Level stringToLogLevel(const std::string &level); + static Level stringToLogLevel(const std::string &level); private: Log(); @@ -78,7 +80,7 @@ class Log struct LevelMapping { - Log::Level level; + Level level; std::string_view name; const char *color; }; @@ -93,3 +95,5 @@ class Log constexpr static const char *RESET_COLOR = "\033[0m"; }; + + diff --git a/webserv/log/StdoutChannel.cpp b/webserv/log/StdoutChannel.cpp index 5fdd5ec..c5e6c8e 100644 --- a/webserv/log/StdoutChannel.cpp +++ b/webserv/log/StdoutChannel.cpp @@ -5,18 +5,12 @@ #include // for operator<<, setfill, setw #include // for basic_ostream, operator<<, basic_ostream::operator<<, cerr, cout, flush, ostream -StdoutChannel::StdoutChannel(Log::Level logLevel) -{ - setLogLevel(logLevel); -} + void StdoutChannel::log(const Log::Level &logLevel, const std::string &message, const std::map &context) { - if (logLevel < getLogLevel()) - { - return; - } + std::ostream &out = (logLevel >= Log::Level::Warn) ? std::cerr : std::cout; out << "[" << std::setw(3) << std::setfill('0') << Log::getElapsedTime() << "] "; std::string prefix = "[" + Log::logLevelToColoredString(logLevel) + "] "; diff --git a/webserv/log/StdoutChannel.hpp b/webserv/log/StdoutChannel.hpp index e88dcd7..2811b05 100644 --- a/webserv/log/StdoutChannel.hpp +++ b/webserv/log/StdoutChannel.hpp @@ -9,7 +9,7 @@ class StdoutChannel : public Channel { public: - StdoutChannel(Log::Level logLevel = Log::Level::Trace); + StdoutChannel() = default; StdoutChannel(const StdoutChannel &other) = delete; StdoutChannel(StdoutChannel &&other) = delete;