简体   繁体   English

如何使用boost.log写入文件?

[英]How do I write to a file with boost.log?

I couldn't find much helpful ressources on the web. 我在网络上找不到太多有用的资源。 They said i had to use boost::log::add_file_log. 他们说我必须使用boost :: log :: add_file_log。 But Visual Studio as well as the compiler could find this function in the boost library. 但是Visual Studio和编译器都可以在boost库中找到此函数。 I also made sure to include all boost/log header files. 我还确保包括所有boost / log头文件。

I reckon they were idly chatting about boost-log v2 , which is not in boost yet to the best of my knowledge. 我认为他们在闲聊关于boost-log v2 ,据我所知,该版本尚未得到增强。

You need to add a sink back-end . 您需要添加一个接收器后端 Here's an example: 这是一个例子:

#define BOOST_LOG_DYN_LINK
#define BOOST_SYSTEM_NO_DEPRECATED

#include <boost/log/sinks.hpp>
#include <boost/log/trivial.hpp>

int main() {

    using namespace boost::log;

    typedef sinks::synchronous_sink<sinks::text_file_backend> sink_t;

    boost::shared_ptr<sink_t> sink(
        new sink_t(boost::make_shared<sinks::text_file_backend>(
            keywords::file_name="my.log")));
    core::get()->add_sink(sink);

    BOOST_LOG_TRIVIAL(debug) << "hello";
}

*See it live* *现场观看*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM