diff --git a/webserv/log/Channel.cpp b/webserv/log/Channel.cpp index 35623e9..9089ef0 100644 --- a/webserv/log/Channel.cpp +++ b/webserv/log/Channel.cpp @@ -1,4 +1,6 @@ #include +#include +#include Log::Level Channel::getLogLevel() const { @@ -8,4 +10,28 @@ Log::Level Channel::getLogLevel() const void Channel::setLogLevel(Log::Level level) { logLevel_ = level; +} + +std::string Channel::printContext(const std::map &context) +{ + std::stringstream ss; + if (!context.empty()) + { + bool first = true; + for (const auto &[key, value] : context) + { + if (!first) + { + + ss << "\n"; + } + ss << "\t| " << std::setw(15) << std::right << std::setfill(' ') << key << " : " << value; + + first = false; + } + ss << "\n\n"; + + } + + return ss.str(); } \ No newline at end of file diff --git a/webserv/log/Channel.hpp b/webserv/log/Channel.hpp index de86697..490de56 100644 --- a/webserv/log/Channel.hpp +++ b/webserv/log/Channel.hpp @@ -19,6 +19,7 @@ class Channel protected: [[nodiscard]] Log::Level getLogLevel() const; + [[nodiscard]] static std::string printContext(const std::map &context); void setLogLevel(Log::Level level); private: diff --git a/webserv/log/FileChannel.cpp b/webserv/log/FileChannel.cpp index 0e40573..f9d5d81 100644 --- a/webserv/log/FileChannel.cpp +++ b/webserv/log/FileChannel.cpp @@ -47,20 +47,6 @@ void FileChannel::log(const Log::Level &logLevel, const std::string &message, << "[" << Log::logLevelToString(logLevel) << "] " << message << '\n'; // Log the context if it exists - if (!context.empty()) - { - fileStream_ << "\n\t| Context: {"; - bool first = true; - for (const auto &[key, value] : context) - { - if (!first) - { - fileStream_ << ", "; - } - fileStream_ << key << ": " << value; - first = false; - } - fileStream_ << "}\n"; - } + fileStream_ << printContext(context); fileStream_ << std::flush; } diff --git a/webserv/log/StdoutChannel.cpp b/webserv/log/StdoutChannel.cpp index 76dfe31..ab59c38 100644 --- a/webserv/log/StdoutChannel.cpp +++ b/webserv/log/StdoutChannel.cpp @@ -5,6 +5,7 @@ #include #include #include +#include StdoutChannel::StdoutChannel(Log::Level logLevel) { @@ -18,24 +19,11 @@ void StdoutChannel::log(const Log::Level &logLevel, const std::string &message, { return; } - std::cout << "[" << std::setw(3) << std::setfill('0') << Log::getElapsedTime() << "] "; + 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) + "] "; - std::cout << prefix; - std::cout << message; - if (!context.empty()) - { - std::cout << "\n\t| Context: {"; - bool first = true; - for (const auto &[key, value] : context) - { - if (!first) - { - std::cout << ", "; - } - std::cout << key << ": " << value; - first = false; - } - std::cout << "}\n"; - } - std::cout << "\n" << std::flush; + out << prefix; + out << message << '\n'; + out << printContext(context); + out << std::flush; } \ No newline at end of file diff --git a/webserv/main.cpp b/webserv/main.cpp index 5edcf13..4542046 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: " + LOCATION, {{"key1", "value1"}, {"key2", "value2"}}); ConfigManager::getInstance().init(argv[1]); // NOLINT Server server(ConfigManager::getInstance()); server.start();