feat(utils/logger): add logging utility with console and file output
This commit is contained in:
@@ -0,0 +1,63 @@
|
|||||||
|
#include "logger.hpp"
|
||||||
|
|
||||||
|
#include <boost/date_time/posix_time/posix_time_io.hpp>
|
||||||
|
|
||||||
|
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<boost::posix_time::ptime>("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<boost::posix_time::ptime>("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;
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <boost/log/core.hpp>
|
||||||
|
#include <boost/log/expressions.hpp>
|
||||||
|
#include <boost/log/trivial.hpp>
|
||||||
|
#include <boost/log/utility/setup/common_attributes.hpp>
|
||||||
|
#include <boost/log/utility/setup/console.hpp>
|
||||||
|
#include <boost/log/utility/setup/file.hpp>
|
||||||
|
|
||||||
|
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);
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user