【问题标题】:boost::log output to Visual Studio output console - adding extra LF/CR to formatboost::log 输出到 Visual Studio 输出控制台 - 添加额外的 LF/CR 到格式
【发布时间】:2018-01-13 17:28:23
【问题描述】:

我正在使用 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% 的不同,后者只是“标准输出”的日志消息目的地。

【问题讨论】:

  • 我不知道如何在 Boost 中解决这个问题,但归根结底必须是 fopen 调用。以“文本”模式打开文件(将“t”添加到模式字符串)解决了这个问题。
  • 根本不是重复的。控制台本质上是 cout。 Windows 控制台是一个完全不同的输出“目的地”。它在 boost::log 中有自己的接收器,就像 cout 一样。

标签: c++ visual-studio boost


【解决方案1】:

事实证明这是一个简单的解决方法。我在接收器后端文档和 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
);

希望这对某人有所帮助。

【讨论】:

  • 供参考,可以在here找到一个windows调试器日志记录示例
猜你喜欢
  • 1970-01-01
  • 2011-01-01
  • 2016-11-13
  • 1970-01-01
  • 2014-09-08
  • 2023-03-16
  • 2020-04-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多