From 88520915dcca26252071e5678f5fe55ec6c7dea1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B0=D1=88=D0=B5=20=D0=98=D0=BC=D1=8F?= Date: Wed, 8 Oct 2025 20:24:17 +0400 Subject: [PATCH] feat(utils/logger): add logging utility with console and file output --- src/utils/logger.cpp | 63 ++++++++++++++++++++++++++++++++++++++++++++ src/utils/logger.hpp | 21 +++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 src/utils/logger.cpp create mode 100644 src/utils/logger.hpp 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); +};