什么是Boost日志“调试输出窗口”?

时间:2013-09-11 08:02:26

标签: logging boost boost-log

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

3 个答案:

答案 0 :(得分:1)

只是一个猜测:这可能意味着"输出" IDE的窗口,即Visual Studios或Eclipses"输出"窗口。

答案 1 :(得分:1)

boost.log的sink后端之一是debugger back end,它使用Windows DebugOutputString函数。

显示这些消息的一个很棒的独立程序是SysInternals' DbgView (debug-view),尽管IDE也会显示它们。

答案 2 :(得分:0)

在调用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;

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