• boost.log在项目中应用


    //头文件
    #pragma
    once #include <string> #include <boost/log/trivial.hpp> using std::string; #define LOG_DEBUG BOOST_LOG_SEV((MyLog::s_slg),(boost::log::trivial::debug)) #define LOG_INFO BOOST_LOG_SEV((MyLog::s_slg),(boost::log::trivial::info)) #define LOG_ERROR BOOST_LOG_SEV((MyLog::s_slg),(boost::log::trivial::error)) #define LOG_WARNING BOOST_LOG_SEV((MyLog::s_slg),(boost::log::trivial::warning)) // 在使用之前必须先调用 init // 使用方式 LOG_DEBUG<<"test string"; // 也可以用boost 中的宏 BOOST_LOG_TRIVIAL(info)<<"test msg"; class MyLog { public: MyLog(); ~MyLog(void); // 在使用之前必须先调用此函数 static void Init(const string & dir); static void Log(const string & msg ); static boost::log::sources::severity_logger<boost::log::trivial::severity_level > s_slg; protected: private: };
    #include "MyLog.h"
    
    #include <iostream>
    #include <string>
    
    #include <boost/filesystem.hpp>
    
    
    #include <boost/log/sources/logger.hpp>
    #include <boost/log/sources/record_ostream.hpp>
    #include <boost/log/sources/global_logger_storage.hpp>
    #include <boost/log/utility/setup/file.hpp>
    #include <boost/log/utility/setup/common_attributes.hpp>
    #include <boost/log/sinks/text_ostream_backend.hpp>
    #include <boost/log/attributes/named_scope.hpp>
    #include <boost/log/expressions.hpp>
    #include <boost/log/support/date_time.hpp>
    #include <boost/log/detail/format.hpp>
    #include <boost/log/detail/thread_id.hpp>
    
    
    
    namespace logging = boost::log;
    namespace src = boost::log::sources;
    namespace keywords = boost::log::keywords;
    namespace sinks = boost::log::sinks;
    namespace expr = boost::log::expressions;
    
    BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(my_logger, src::logger_mt)
    boost::log::sources::severity_logger<boost::log::trivial::severity_level > MyLog::s_slg;
    
    MyLog::MyLog()
    {
    
    }
    
    MyLog::~MyLog(void)
    {
     
    }
    
    void MyLog::Init(const string & dir)
    {
      if (boost::filesystem::exists(dir) == false)
      {
        boost::filesystem::create_directories(dir);
      }
    
      auto pSink = logging::add_file_log
      (
          keywords::open_mode = std::ios::app,
          keywords::file_name = dir + "/%Y%m%d.log",
          keywords::rotation_size = 10 * 1024 * 1024,
          keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0), 
          //keywords::format = "[%TimeStamp% %ThreadID%]: %Message%"
          keywords::format =
          (
            expr::stream
            << "[" <<expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S.%f")
            << " " << expr::attr< boost::log::aux::thread::id >("ThreadID")
            << " " << logging::trivial::severity
            << "] " << expr::smessage
          )
      );
      // 如果不写这个 它不会实时的把日志写下去, 而是等待缓冲区满了,或者程序正常退出时写下,这样做的好处是减少IO操作.提高效率,  不过我这里不需要它. 因为我的程序可能会异常退出.
      pSink->locked_backend()->auto_flush(true);//使日志实时更新
      //pSink->imbue(std::locale("zh_CN.UTF-8")); // 本地化 
      logging::add_common_attributes();
    }
    
    
    void MyLog::Log(const string & msg )
    {
      src::logger lg;
      BOOST_LOG(lg) << msg;
    }
  • 相关阅读:
    Linux 进程学习(四) sigaction 函数
    Netty 编解码奥秘
    我的博客即将同步至 OSCHINA 社区,这是我的 OSCHINA ID:护国小将,邀请大家一同入驻:https://www.oschina.net/sharingplan/apply
    Netty数据如何在 pipeline 中流动
    PLM系统安装四:主卷服务安装(FSC缓存服务器plm4IP:42.20)
    Linux系统信息和进程相关命令(CPU,内存,进程)
    SAN交换机配置的备份还原,固件升级
    san交换机的级联
    PLM系统安装五(2):Corporate服务安装plm1IP:42.106
    第三步:服务器虚拟化XenServer实施部署文档
  • 原文地址:https://www.cnblogs.com/liaocheng/p/4222893.html
Copyright © 2020-2023  润新知