123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- #include "log.h"
- #include <boost/log/core.hpp>
- #include <boost/log/trivial.hpp>
- #include <boost/log/expressions.hpp>
- #include <boost/log/support/date_time.hpp>
- #include <boost/log/utility/setup/file.hpp>
- #include <boost/log/sinks/async_frontend.hpp>
- #include <boost/log/sinks/text_multifile_backend.hpp>
- #include <boost/log/utility/setup/common_attributes.hpp>
- #include <boost/log/sources/severity_channel_logger.hpp>
- #include <boost/date_time/posix_time/posix_time.hpp>
- #include <boost/date_time/gregorian/gregorian.hpp>
- namespace dt = boost::posix_time;
- namespace logging = boost::log;
- namespace logexpr = boost::log::expressions;
- #ifdef USING_ASYNC_LOG_MODE
- typedef logging::sinks::asynchronous_sink<logging::sinks::text_multifile_backend,
- logging::sinks::unbounded_fifo_queue> sink_t;
- #else
- typedef logging::sinks::synchronous_sink<logging::sinks::text_multifile_backend> sink_t;
- #endif // USING_ASYNC_LOG_MODE
- // variables
- static logging::sources::severity_channel_logger_mt<boost::log::trivial::severity_level> lg =
- logging::sources::severity_channel_logger_mt<boost::log::trivial::severity_level>(logging::keywords::channel = "net");
- static boost::shared_ptr<sink_t> sink = nullptr;
- static logging::attributes::mutable_constant<std::string> rotation_date =
- logging::attributes::mutable_constant<std::string>(dt::to_iso_extended_string(dt::second_clock::local_time() + dt::hours(4)).substr(0, 10));
- #define logger_trace BOOST_LOG_SEV(lg,logging::trivial::severity_level::trace)
- #define logger_debug BOOST_LOG_SEV(lg,logging::trivial::severity_level::debug)
- #define logger_info BOOST_LOG_SEV(lg,logging::trivial::severity_level::info)
- #define logger_warning BOOST_LOG_SEV(lg,logging::trivial::severity_level::warning)
- #define logger_error BOOST_LOG_SEV(lg,logging::trivial::severity_level::error)
- #define logger_fatal BOOST_LOG_SEV(lg,logging::trivial::severity_level::fatal)
- bool logger::init(std::string strLogPath, std::string strLogName) {
- // 初始化日志
- logging::add_common_attributes();
- boost::shared_ptr<logging::core > core = logging::core::get();
- core->add_global_attribute("RotationDate", rotation_date);
- boost::shared_ptr<logging::sinks::text_multifile_backend> backend =
- boost::make_shared<logging::sinks::text_multifile_backend>();
- // Set up the file naming pattern
- boost::gregorian::date d(boost::gregorian::day_clock::local_day());
- backend->set_file_name_composer(logging::sinks::file::as_file_name_composer(
- logexpr::stream << strLogPath
- << "/" << boost::gregorian::to_iso_string(d) << "_"
- // 文件名还是带上Level,否者copy到一起的时候分不清
- << logexpr::attr<logging::trivial::severity_level>("Severity") << "_"
- << strLogName << ".log"));
-
- sink = boost::shared_ptr<sink_t>(new sink_t(backend));
- // Set the formatter
- sink->set_formatter(logexpr::stream
- << "datetime=" << logexpr::format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y-%m-%d %H:%M:%S.%f")
- << logexpr::smessage);
- core->add_sink(sink);
- return true;
- }
- void logger::update_rotation_time() {
- dt::ptime t = dt::second_clock::local_time();
- if (t.time_of_day().hours() <= 20)
- return;
- t += dt::hours(4); // 以20点为界切割一个交易日,选择20的原因是这样使得所有的数据可以用一天表示,原油也可以
- rotation_date.set(dt::to_iso_extended_string(t).substr(0, 10));
- }
- void logger::showLog(std::string msg)
- {
- logger_info << msg;
- }
- void logger::stop() {
- if (sink == nullptr)
- return;
- boost::shared_ptr<logging::core > core = logging::core::get();
- core->remove_sink(sink);
- #ifdef USING_ASYNC_LOG_MODE
- sink->stop();
- #endif // USING_ASYNC_LOG_MODE
- sink->flush();
- sink.reset();
- }
|