gpt4 book ai didi

logging - 什么是 Boost 日志 "debug output window"?

转载 作者:行者123 更新时间:2023-12-05 01:48:07 26 4
gpt4 key购买 nike

我一直在浏览 Boost.Log 教程和文档,在所有示例中,它们都指的是“调试器窗口”或“调试输出窗口”,但我找不到那是什么。这是某种独立的应用程序吗?那我在哪里可以下载呢?

最佳答案

在调用 boost log 方法之前,请确保已设置接收器,这意味着您需要调用如下内容:

static void init_log(void)
{
/* init boost log
* 1. Add common attributes
* 2. set log filter to trace
*/
boost::log::add_common_attributes();
boost::log::core::get()->add_global_attribute("Scope",
boost::log::attributes::named_scope());
boost::log::core::get()->set_filter(
boost::log::trivial::severity >= boost::log::trivial::trace
);

/* log formatter:
* [TimeStamp] [ThreadId] [Severity Level] [Scope] Log message
*/
auto fmtTimeStamp = boost::log::expressions::
format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y-%m-%d %H:%M:%S.%f");
auto fmtThreadId = boost::log::expressions::
attr<boost::log::attributes::current_thread_id::value_type>("ThreadID");
auto fmtSeverity = boost::log::expressions::
attr<boost::log::trivial::severity_level>("Severity");
auto fmtScope = boost::log::expressions::format_named_scope("Scope",
boost::log::keywords::format = "%n(%f:%l)",
boost::log::keywords::iteration = boost::log::expressions::reverse,
boost::log::keywords::depth = 2);
boost::log::formatter logFmt =
boost::log::expressions::format("[%1%] (%2%) [%3%] [%4%] %5%")
% fmtTimeStamp % fmtThreadId % fmtSeverity % fmtScope
% boost::log::expressions::smessage;

/* console sink */
auto consoleSink = boost::log::add_console_log(std::clog);
consoleSink->set_formatter(logFmt);

/* fs sink */
auto fsSink = boost::log::add_file_log(
boost::log::keywords::file_name = "test_%Y-%m-%d_%H-%M-%S.%N.log",
boost::log::keywords::rotation_size = 10 * 1024 * 1024,
boost::log::keywords::min_free_space = 30 * 1024 * 1024,
boost::log::keywords::open_mode = std::ios_base::app);
fsSink->set_formatter(logFmt);
fsSink->locked_backend()->auto_flush(true);
}

上面是在这里找到的一个片段: A simple sample of Boost Log

当你想记录一些东西时,你可以这样写:

BOOST_LOG_TRIVIAL(error) << "{ ERROR } Trying to bla bla bla\n";
BOOST_LOG_TRIVIAL(warning) << "{ WARNING } bla bla bla\n";

当在 Debug模式下运行时,输出将在您的调试文件夹中找到。将创建一个如下所示的文件:

test_2018-04-09_20-08-12.0.log

输出不会显示在 Visual Studio 的调试输出窗口中

如果你不想登录到一个文件而只是在调试窗口登录,那么使用这样的东西:

OutputDebugString(L"This is an output");

或者在 init_log 中添加一个额外的接收器——像这样:

/* setup logging to debugger window */
// Complete sink type
boost::shared_ptr< boost::log::core > core = boost::log::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(boost::log::expressions::is_debugger_present());
core->add_sink(sink);

你还需要添加这个:

#include <boost/log/sinks/debug_output_backend.hpp>
#include <boost/log/sinks/event_log_backend.hpp>
#include <boost/thread/future.hpp>

typedef boost::log::sinks::synchronous_sink<boost::log::sinks::debug_output_backend> sink_t;

然后输出也将显示在调试器窗口中

关于logging - 什么是 Boost 日志 "debug output window"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18735763/

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