log.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "log.h"
  2. #include <boost/log/core.hpp>
  3. #include <boost/log/trivial.hpp>
  4. #include <boost/log/expressions.hpp>
  5. #include <boost/log/support/date_time.hpp>
  6. #include <boost/log/utility/setup/file.hpp>
  7. #include <boost/log/sinks/async_frontend.hpp>
  8. #include <boost/log/sinks/text_multifile_backend.hpp>
  9. #include <boost/log/utility/setup/common_attributes.hpp>
  10. #include <boost/log/sources/severity_channel_logger.hpp>
  11. #include <boost/date_time/posix_time/posix_time.hpp>
  12. #include <boost/date_time/gregorian/gregorian.hpp>
  13. namespace dt = boost::posix_time;
  14. namespace logging = boost::log;
  15. namespace logexpr = boost::log::expressions;
  16. #ifdef USING_ASYNC_LOG_MODE
  17. typedef logging::sinks::asynchronous_sink<logging::sinks::text_multifile_backend,
  18. logging::sinks::unbounded_fifo_queue> sink_t;
  19. #else
  20. typedef logging::sinks::synchronous_sink<logging::sinks::text_multifile_backend> sink_t;
  21. #endif // USING_ASYNC_LOG_MODE
  22. // variables
  23. static logging::sources::severity_channel_logger_mt<boost::log::trivial::severity_level> lg =
  24. logging::sources::severity_channel_logger_mt<boost::log::trivial::severity_level>(logging::keywords::channel = "net");
  25. static boost::shared_ptr<sink_t> sink = nullptr;
  26. static logging::attributes::mutable_constant<std::string> rotation_date =
  27. logging::attributes::mutable_constant<std::string>(dt::to_iso_extended_string(dt::second_clock::local_time() + dt::hours(4)).substr(0, 10));
  28. #define logger_trace BOOST_LOG_SEV(lg,logging::trivial::severity_level::trace)
  29. #define logger_debug BOOST_LOG_SEV(lg,logging::trivial::severity_level::debug)
  30. #define logger_info BOOST_LOG_SEV(lg,logging::trivial::severity_level::info)
  31. #define logger_warning BOOST_LOG_SEV(lg,logging::trivial::severity_level::warning)
  32. #define logger_error BOOST_LOG_SEV(lg,logging::trivial::severity_level::error)
  33. #define logger_fatal BOOST_LOG_SEV(lg,logging::trivial::severity_level::fatal)
  34. bool logger::init(std::string strLogPath, std::string strLogName) {
  35. // 初始化日志
  36. logging::add_common_attributes();
  37. boost::shared_ptr<logging::core > core = logging::core::get();
  38. core->add_global_attribute("RotationDate", rotation_date);
  39. boost::shared_ptr<logging::sinks::text_multifile_backend> backend =
  40. boost::make_shared<logging::sinks::text_multifile_backend>();
  41. // Set up the file naming pattern
  42. boost::gregorian::date d(boost::gregorian::day_clock::local_day());
  43. backend->set_file_name_composer(logging::sinks::file::as_file_name_composer(
  44. logexpr::stream << strLogPath
  45. << "/" << boost::gregorian::to_iso_string(d) << "_"
  46. // 文件名还是带上Level,否者copy到一起的时候分不清
  47. << logexpr::attr<logging::trivial::severity_level>("Severity") << "_"
  48. << strLogName << ".log"));
  49. sink = boost::shared_ptr<sink_t>(new sink_t(backend));
  50. // Set the formatter
  51. sink->set_formatter(logexpr::stream
  52. << "datetime=" << logexpr::format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y-%m-%d %H:%M:%S.%f")
  53. << logexpr::smessage);
  54. core->add_sink(sink);
  55. return true;
  56. }
  57. void logger::update_rotation_time() {
  58. dt::ptime t = dt::second_clock::local_time();
  59. if (t.time_of_day().hours() <= 20)
  60. return;
  61. t += dt::hours(4); // 以20点为界切割一个交易日,选择20的原因是这样使得所有的数据可以用一天表示,原油也可以
  62. rotation_date.set(dt::to_iso_extended_string(t).substr(0, 10));
  63. }
  64. void logger::showLog(std::string msg)
  65. {
  66. logger_info << msg;
  67. }
  68. void logger::stop() {
  69. if (sink == nullptr)
  70. return;
  71. boost::shared_ptr<logging::core > core = logging::core::get();
  72. core->remove_sink(sink);
  73. #ifdef USING_ASYNC_LOG_MODE
  74. sink->stop();
  75. #endif // USING_ASYNC_LOG_MODE
  76. sink->flush();
  77. sink.reset();
  78. }