From c712cbf6db89618060efeb7a93b15074582f6e00 Mon Sep 17 00:00:00 2001 From: Quinten Date: Wed, 22 Oct 2025 16:51:32 +0200 Subject: [PATCH] refactor(log): remove mode and clear channels function --- webserv/log/FileChannel.cpp | 9 +++++---- webserv/log/FileChannel.hpp | 2 +- webserv/log/Log.cpp | 10 ++++++++-- webserv/log/Log.hpp | 4 +++- webserv/main.cpp | 2 +- 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/webserv/log/FileChannel.cpp b/webserv/log/FileChannel.cpp index e2c4bf6..f074435 100644 --- a/webserv/log/FileChannel.cpp +++ b/webserv/log/FileChannel.cpp @@ -1,16 +1,17 @@ #include - #include // for Log -#include // for system_clock +#include // for system_clock +#include #include // for localtime, tm #include // for operator<<, put_time #include // for cerr +#include + struct tm; -FileChannel::FileChannel(const std::string &filename, std::ios_base::openmode mode) - : filename_(filename), fileStream_(filename, mode) +FileChannel::FileChannel(const std::string &filename) : filename_(filename), fileStream_(filename, std::ios_base::trunc) { if (!fileStream_.is_open()) { diff --git a/webserv/log/FileChannel.hpp b/webserv/log/FileChannel.hpp index eef6c58..32ee9d9 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); + FileChannel(const std::string &filename); FileChannel(const FileChannel &other) = delete; FileChannel(FileChannel &&other) = delete; diff --git a/webserv/log/Log.cpp b/webserv/log/Log.cpp index 5ea84d6..f264183 100644 --- a/webserv/log/Log.cpp +++ b/webserv/log/Log.cpp @@ -33,7 +33,7 @@ void Log::setStdoutChannel() } } -void Log::setFileChannel(const std::string &filename, std::ios_base::openmode mode) +void Log::setFileChannel(const std::string &filename) { Log &log = getInstance(); if (log.channels_.contains("file")) @@ -42,7 +42,7 @@ void Log::setFileChannel(const std::string &filename, std::ios_base::openmode mo } try { - log.channels_["file"] = std::make_unique(filename, mode); + log.channels_["file"] = std::make_unique(filename); } catch (const std::exception &e) { @@ -50,6 +50,12 @@ void Log::setFileChannel(const std::string &filename, std::ios_base::openmode mo } } +void Log::clearChannels() +{ + Log &log = getInstance(); + log.channels_.clear(); +} + Log &Log::getInstance() { static Log instance; diff --git a/webserv/log/Log.hpp b/webserv/log/Log.hpp index a055ea1..bca3bf2 100644 --- a/webserv/log/Log.hpp +++ b/webserv/log/Log.hpp @@ -51,7 +51,7 @@ class Log static constexpr Log::Level COMPILE_TIME_LOG_LEVEL = Log::Level::Debug; - static void setFileChannel(const std::string &filename, std::ios_base::openmode mode = std::ios_base::app); + static void setFileChannel(const std::string &filename); static void setStdoutChannel(); static int getElapsedTime(); @@ -68,6 +68,8 @@ class Log static std::string logLevelToColoredString(Level level); static Level stringToLogLevel(const std::string &level); + static void clearChannels(); + private: Log(); diff --git a/webserv/main.cpp b/webserv/main.cpp index a70176a..775b042 100644 --- a/webserv/main.cpp +++ b/webserv/main.cpp @@ -16,7 +16,7 @@ int main(int argc, char **argv) return 1; } - Log::setFileChannel("logs/webserv.log", std::ios_base::trunc); + Log::setFileChannel("logs/webserv.log"); Log::setStdoutChannel(); Log::info("\n======================\nStarting webserv...\n======================\n");