boost :: log输出到Visual Studio输出控制台 - 添加额外的LF / CR格式

时间:2018-01-13 17:28:24

标签: c++ visual-studio boost

我正在使用boost 1.65.1并且我想配置boost :: log以将日志消息输出到我的Visual Studio 2015输出调试窗口。问题是消息具有unix行结尾,所以我的所有消息都在输出窗口的一(长)行中。

我不想在每条日志消息中添加额外的CR / LF,因为这会混淆并发输出消息到我正在使用的文件。

这就是我正在初始化boost :: log以输出VS可以接收的消息:

#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/sinks/debug_output_backend.hpp>
#include <boost/exception/all.hpp>
#include <exception>

typedef sinks::synchronous_sink< sinks::debug_output_backend > sink_t;
...

logging::add_console_log( <stuff> );
logging::add_file_log( <stuff> );

boost::shared_ptr< logging::core > core = logging::core::get();

// Create the sink. The backend requires synchronization in the frontend.
boost::shared_ptr< sink_t > sink(new sink_t());

// Set the special filter to the frontend
// in order to skip the sink when no debugger is available
sink->set_filter(expr::is_debugger_present());

我在add_file_log和add_console_log中有一个格式说明符,但是 是否可以添加一个特定于debug_output_backend的格式说明符来输出我需要的额外CR / LF。即只是输出到Visual Studio(其他输出方法保持不变)?

注意:此接收器与boost :: log docs称为“控制台”的100%不同,后者只是“标准输出”的日志消息目标。

1 个答案:

答案 0 :(得分:1)

原来这是一个简单的解决方案。我在Sink Backends文档和Windows事件日志接收器部分中进一步阅读,他们使用set_formatter。所以只需在调用set_filter之后添加它就可以修复它:

sink->set_formatter
(
  expr::format("%1%: [%2%] - %3%\r\n")
  % expr::format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y-%m-%d %H:%M:%S")
  % logging::trivial::severity
  % expr::smessage
);

希望这有助于某人。