gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:05:44 25 4
gpt4 key购买 nike

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

最佳答案

事实证明这是一个简单的修复。我在 Sink Backends 文档和 Windows Event Log sink 部分进一步阅读,他们使用 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
);

希望这对某人有帮助。

关于c++ - boost::log 输出到 Visual Studio 输出控制台 - 添加额外的 LF/CR 以格式化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48242414/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com