diff --git a/src/utils/logger.cpp b/src/utils/logger.cpp new file mode 100644 index 0000000..8808e31 --- /dev/null +++ b/src/utils/logger.cpp @@ -0,0 +1,63 @@ +#include "logger.hpp" + +#include + +void Logger::init(bool toConsole, const std::string_view filePath) +{ + using namespace boost::log; + using namespace boost::posix_time; + + add_common_attributes(); + + if (toConsole) { + add_console_log( + std::clog, + keywords::format = + (expressions::stream + << "[" + << expressions::attr("TimeStamp") + << "] " + << "<" << trivial::severity << "> " << expressions::smessage) + ); + } + + if (!filePath.empty()) { + add_file_log( + keywords::file_name = filePath, + keywords::auto_flush = true, + keywords::format = + (expressions::stream + << "[" + << expressions::attr("TimeStamp") + << "] " + << "<" << trivial::severity << "> " << expressions::smessage) + ); + } + + core::get()->set_filter(trivial::severity >= trivial::info); +} + +void Logger::info(const std::string_view msg) +{ + BOOST_LOG_TRIVIAL(info) << msg; +} + +void Logger::warn(const std::string_view msg) +{ + BOOST_LOG_TRIVIAL(warning) << msg; +} + +void Logger::error(const std::string_view msg) +{ + BOOST_LOG_TRIVIAL(error) << msg; +} + +void Logger::debug(const std::string_view msg) +{ + BOOST_LOG_TRIVIAL(debug) << msg; +} + +void Logger::fatal(const std::string_view msg) +{ + BOOST_LOG_TRIVIAL(fatal) << msg; +} diff --git a/src/utils/logger.hpp b/src/utils/logger.hpp new file mode 100644 index 0000000..d10e071 --- /dev/null +++ b/src/utils/logger.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +class Logger +{ + public: + static void + init(bool toConsole = true, const std::string_view filePath = ""); + + static void info(const std::string_view msg); + static void warn(const std::string_view msg); + static void error(const std::string_view msg); + static void debug(const std::string_view msg); + static void fatal(const std::string_view msg); +};