db-lab1/bserv/logging.hpp

48 lines
1.3 KiB
C++
Raw Normal View History

2021-03-05 15:39:47 +08:00
#ifndef _LOGGING_HPP
#define _LOGGING_HPP
#define BOOST_LOG_DYN_LINK
#include <boost/log/core.hpp>
#include <boost/log/common.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/utility/setup.hpp>
2021-07-13 19:53:10 +08:00
#include <cstddef>
#include <string>
2021-03-05 15:39:47 +08:00
#include "config.hpp"
namespace bserv {
namespace logging = boost::log;
namespace keywords = boost::log::keywords;
namespace src = boost::log::sources;
2021-07-13 19:53:10 +08:00
// this function should be called before logging is used
inline void init_logging(const server_config& config) {
2021-03-05 15:39:47 +08:00
logging::add_file_log(
2021-07-13 19:53:10 +08:00
keywords::file_name = config.get_log_path() + "_%Y%m%d_%H-%M-%S.%N.log",
keywords::rotation_size = config.get_log_rotation_size(),
2021-03-05 15:39:47 +08:00
keywords::format = "[%Severity%][%TimeStamp%][%ThreadID%]: %Message%"
);
logging::core::get()->set_filter(
logging::trivial::severity >= logging::trivial::trace
);
logging::add_common_attributes();
}
#define lgtrace BOOST_LOG_TRIVIAL(trace)
#define lgdebug BOOST_LOG_TRIVIAL(debug)
#define lginfo BOOST_LOG_TRIVIAL(info)
#define lgwarning BOOST_LOG_TRIVIAL(warning)
#define lgerror BOOST_LOG_TRIVIAL(error)
#define lgfatal BOOST_LOG_TRIVIAL(fatal)
inline void fail(const boost::system::error_code& ec, const char* what) {
2021-03-13 19:33:01 +08:00
lgerror << what << ": " << ec.message() << std::endl;
}
2021-03-05 15:39:47 +08:00
} // bserv
#endif // _LOGGING_HPP