zoukankan      html  css  js  c++  java
  • boost::log随笔

    boost::log包含两个大部分:logger和sink

    logging::core是全局的一个单例

    1,sink

    在boost::log中有一个默认的sink与控制台流相关关联;

    如果想把日志输出到指定的文件流中可以使用如下代码:

    auto sink=add_log_file("xxx.log");

    上面的一行代码与下面是等价的:

    ===========================================================================

    void init()
    {
    // Construct the sink
    typedef sinks::synchronous_sink< sinks::text_ostream_backend > text_sink;
    boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >();
     
    // Add a stream to write log to
    sink->locked_backend()->add_stream(
    boost::make_shared< std::ofstream >("sample.log"));
     
    // Register the sink in the logging core
    logging::core::get()->add_sink(sink);
    }
    ===========================================================================
     
    从上面的代码中可能知道add_log_file内部是把相关的sink已经加入到loggint::core了;
     
    如果要使用多个sink,可以通过设置channel属性,eg
     1 typedef boost::log::sources::channel_logger_mt<>  channel_logger;
     2 
     3 std::shard_ptr<channel_logger>  create_logger(const std::string& channel)
     4 {
     5     auto sink = add_file_log(
     6            keywords::file_name="xx.log",
     7            keywords::channel=channel
     8             );  
     9 
    10   //只将属于此channel的信息输出到xx.log
    11   sink->set_filter(expr::attr<std::string>("Channel")==channel);
    12 
    13   return std::shared_ptr<channel_logger>(new channel_logger(keywords::channel));
    14 }
    如此就创建一个指向channel相关的logger,有了logger就可以打印日志了,eg

    std::shared<channel_logger> logger = create_logger("my_channel");

     BOOST_LOG(*logger)<<"Hello World!";

     这样就可以在xx.log文件中看到Hello World!信息了
     
     
    参考链接:http://www.wanguanglu.com/2016/07/28/boost-log-document/#detailed-logging-source 
    https://www.boost.org/doc/libs/1_55_0/libs/log/doc/html/log/detailed/sources.html 
  • 相关阅读:
    Pyspider
    tornado websocket
    cookie、session、csrf
    python图片拼接
    SQLAlchemy查询
    tornado的ORM
    模板继承和UImodul 和 UImethods
    图像处理-05-浮雕效果处理
    图像处理-04-图像的黑白处理
    图像处理-03-实现图像的旋转
  • 原文地址:https://www.cnblogs.com/guoliushui/p/9884520.html
Copyright © 2011-2022 走看看