如何将Boost.Log重定向到文件 [英] How to redirect Boost.Log to file

查看:306
本文介绍了如何将Boost.Log重定向到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在并发应用程序中使用一个简单的日志文件.我已经下载了Boost.Log v2.0并将其与Boost 1.53.0一起使用.

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.

问题是Boost.Log在控制台上输出.我正在使用BOOST_LOG_TRIVIAL(trace).

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

是否有一种很好的方法将BOOST_LOG_TRIVIAL重定向到文件?

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

推荐答案

您可以使BOOST_LOG_TRIVIAL使用以下文件(假设namespace logging = boost::log;:

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
    );
}

在主要方面:

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;
}

这篇关于如何将Boost.Log重定向到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆