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

boost::log output to Visual Studio output console - adding extra LF/CR to format

本文关键字:输出 添加 CR 格式 LF log Visual Studio boost 控制台      更新时间:2023-10-16

我正在使用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 文档所称的"控制台"有 100% 不同,后者只是"标准输出"的日志消息目标。

事实证明,

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

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

希望这对某人有所帮助。