从text_file_backend获取格式化字符串

时间:2016-04-28 08:13:31

标签: c++ logging boost boost-log

我在我的应用程序中使用 boost-log 。我想将日志写入文件并通过网络发送记录的字符串(用于复制目的)。

对于文件记录,我使用text_file_backend(与synchronous_sink<text_file_backend>一起使用);这很好用,并使用特定格式记录到文件信息。我想获得相同格式的字符串,以便能够通过网络发送。

如何从text_file_backend中将其写入文件的实际字符串中挤出来?

1 个答案:

答案 0 :(得分:0)

您可以尝试将multiple stream添加到.txt

backend

更新

坚持text_file_backend,您可以使用void init_logging() { boost::shared_ptr< logging::core > core = logging::core::get(); // Create a backend and attach a couple of streams to it boost::shared_ptr< sinks::text_ostream_backend > backend = boost::make_shared< sinks::text_ostream_backend >(); backend->add_stream( boost::shared_ptr< std::ostream >(&std::clog, boost::null_deleter())); backend->add_stream( boost::shared_ptr< std::ostream >(new std::ofstream("sample.log"))); /*** Add your network stream here ***/ typedef sinks::synchronous_sink< sinks::text_ostream_backend > sink_t; boost::shared_ptr< sink_t > sink(new sink_t(backend)); core->add_sink(sink); } 设置回调函数,该函数读取已关闭的日志文件并将其发送到您的日志服务器。

如果您无法忍受延迟,请使用void set_close_handler(close_handler_type const & handler),然后启动新线程来读取文件。

最终,你可以继承set_open_handler并重载我认为fontend调用的text_file_backend方法。有关后端API的信息,请参阅this页面

相关问题