【问题标题】:Can I copy a Boost::Log sink and change it?我可以复制 Boost::Log 接收器并更改它吗?
【发布时间】:2016-10-29 02:12:38
【问题描述】:

我目前正在创建一个日志接收器,它将日志行从我的Trainer 类的输出转储到cout

    using namespace boost;                                                      
    using namespace log;                                                        
    using namespace expressions;                                                
    using namespace sinks;                                                      

    auto sink = make_shared<synchronous_sink<text_ostream_backend>>();          

    sink->set_formatter(Utility::GetLoggingFormat());                           

    sink->locked_backend()->add_stream(                                         
        shared_ptr<std::ostream>(&std::cout, null_deleter()));                  
    sink->set_filter((                                                          
        has_attr(Utility::ClassTag) &&                                          
        Utility::ClassTag == "App::Trainer"));                                  
    boost::log::core::get()->add_sink(sink);                                    

但是,我已经添加了一个接收器(通过在其他地方调用add_file_log),它将所有日志行转储到一个文件中。它使用相同的GetLoggingFormat() 格式化程序。事实上,我拥有该功能的唯一原因是我不必重复格式化程序代码。

相反,我宁愿只“复制”文件接收器(使用add_file_log 在其他地方创建),将其输出更改为std::cout 并添加过滤器。

我可以这样做吗?

【问题讨论】:

    标签: c++ boost boost-log


    【解决方案1】:

    不,那是不可能的。无法复制接收器,实际上add_file_log 创建的接收器使用的不是text_ostream_backend,而是text_file_backend;您不能将text_file_backend 配置为输出到std::cout

    您最好的方法是使用一个函数,就像您已经使用的那样,或者将格式化程序缓存在 formatter 对象中并将其设置为您的两个接收器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-07
      • 1970-01-01
      • 2014-05-11
      • 1970-01-01
      • 2014-01-08
      • 1970-01-01
      相关资源
      最近更新 更多