refactor(log): remove mode and clear channels function

This commit is contained in:
Quinten 2025-10-22 16:51:32 +02:00
parent c49b259862
commit c712cbf6db
5 changed files with 18 additions and 9 deletions

View File

@ -1,16 +1,17 @@
#include <webserv/log/FileChannel.hpp> #include <webserv/log/FileChannel.hpp>
#include <webserv/log/Log.hpp> // for Log #include <webserv/log/Log.hpp> // for Log
#include <chrono> // for system_clock #include <chrono> // for system_clock
#include <cstdio>
#include <ctime> // for localtime, tm #include <ctime> // for localtime, tm
#include <iomanip> // for operator<<, put_time #include <iomanip> // for operator<<, put_time
#include <iostream> // for cerr #include <iostream> // for cerr
#include <fcntl.h>
struct tm; struct tm;
FileChannel::FileChannel(const std::string &filename, std::ios_base::openmode mode) FileChannel::FileChannel(const std::string &filename) : filename_(filename), fileStream_(filename, std::ios_base::trunc)
: filename_(filename), fileStream_(filename, mode)
{ {
if (!fileStream_.is_open()) if (!fileStream_.is_open())
{ {

View File

@ -10,7 +10,7 @@
class FileChannel : public Channel class FileChannel : public Channel
{ {
public: public:
FileChannel(const std::string &filename, std::ios_base::openmode mode); FileChannel(const std::string &filename);
FileChannel(const FileChannel &other) = delete; FileChannel(const FileChannel &other) = delete;
FileChannel(FileChannel &&other) = delete; FileChannel(FileChannel &&other) = delete;

View File

@ -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(); Log &log = getInstance();
if (log.channels_.contains("file")) if (log.channels_.contains("file"))
@ -42,7 +42,7 @@ void Log::setFileChannel(const std::string &filename, std::ios_base::openmode mo
} }
try try
{ {
log.channels_["file"] = std::make_unique<FileChannel>(filename, mode); log.channels_["file"] = std::make_unique<FileChannel>(filename);
} }
catch (const std::exception &e) 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() Log &Log::getInstance()
{ {
static Log instance; static Log instance;

View File

@ -51,7 +51,7 @@ class Log
static constexpr Log::Level COMPILE_TIME_LOG_LEVEL = Log::Level::Debug; 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 void setStdoutChannel();
static int getElapsedTime(); static int getElapsedTime();
@ -68,6 +68,8 @@ class Log
static std::string logLevelToColoredString(Level level); static std::string logLevelToColoredString(Level level);
static Level stringToLogLevel(const std::string &level); static Level stringToLogLevel(const std::string &level);
static void clearChannels();
private: private:
Log(); Log();

View File

@ -16,7 +16,7 @@ int main(int argc, char **argv)
return 1; return 1;
} }
Log::setFileChannel("logs/webserv.log", std::ios_base::trunc); Log::setFileChannel("logs/webserv.log");
Log::setStdoutChannel(); Log::setStdoutChannel();
Log::info("\n======================\nStarting webserv...\n======================\n"); Log::info("\n======================\nStarting webserv...\n======================\n");