How to redirect Boost.Log to file

时间秒杀一切 提交于 2019-12-20 18:02:33

问题


I want a simple log file in a concurrent application. I've download Boost.Log v2.0 and using compiled it with Boost 1.53.0.

The problem is that Boost.Log output on console. I'm using BOOST_LOG_TRIVIAL(trace).

Is there a nice way to redirect BOOST_LOG_TRIVIAL to a file?


回答1:


You can make BOOST_LOG_TRIVIAL use a file with (assuming that namespace logging = boost::log;:

#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/utility/setup/file.hpp>

void init()
{
    logging::add_file_log("sample.log");

    logging::core::get()->set_filter
    (
        logging::trivial::severity >= logging::trivial::info
    );
}

And in main:

int main(int, char*[])
{
    init();

    BOOST_LOG_TRIVIAL(trace) << "A trace severity message";
 // other types of severity
    BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message";

    return 0;
}


来源:https://stackoverflow.com/questions/16102128/how-to-redirect-boost-log-to-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!