我一直在浏览Boost.Log教程和文档,在所有示例中,它们都是指“调试器窗口”或“调试输出窗口”,但我找不到它是什么。这是某种独立的应用程序吗?我在哪里下载呢?
只是一个猜测:这可能意味着IDE的“输出”窗口,即Visual Studios或Eclipses“输出”窗口。
boost.log的下沉后端之一是debugger back end,它使用Windows DebugOutputString函数。
显示这些消息的一个很棒的独立程序是SysInternals' DbgView (debug-view),尽管IDE也会显示它们。
在调用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;
然后输出也将显示在Debugger窗口中