c – 升级log severity_logger init_from_stream

我使用boost 1.54.0.
下面你可以找到一个说明我的问题的最小例子.

我使用提升日志的severity_logger.
我想从流中配置我的接收器.
(在下面的例子中我使用一个stringstream.
在我的真实应用程序中,流来自一个文件.)
我想使用%Severity%作为输出或过滤目的.

我的问题是:如果我使用它在下面的例子中给出,%Severity%是空的.

%LineID%和%Message%按预期填写.
如果我在附近的线路中设置了一个水槽,它的工作原理如预期.

有任何想法吗?

#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/from_stream.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/expressions.hpp>

enum SeverityLevel { trace,fatal };

int main (int argc,char *argv[])
{
    boost::log::add_common_attributes();
    /*
    struct severity_tag;
    boost::log::add_console_log(std::clog,boost::log::keywords::format = (
            boost::log::expressions::stream
                << boost::log::expressions::attr< unsigned int >("LineID")
                << ": <" << boost::log::expressions::attr<SeverityLevel,severity_tag >("Severity")
                << "> " << boost::log::expressions::smessage)
    ); */

    std::stringstream s;
    s << "[Sinks.MySink]" << std::endl;
    s << "Destination=Console" << std::endl;
    s << "Format=\"%LineID%: <%severity%> - %Message%\"" << std::endl;
    boost::log::init_from_stream(s);

    boost::log::sources::severity_logger<SeverityLevel> lg;
    BOOST_LOG_SEV(lg,trace) << "This is a trace message";
    BOOST_LOG_SEV(lg,fatal) << "This is a fatal message";
    return 0;
}

解决方法

你是对的.这是一个已知的错误,并在当前的开发版本中修复.

这是错误报告:https://svn.boost.org/trac/boost/ticket/8840

为了使这个答案更少地依赖于这里的链接是在报告中解决的方式:

You need to register your severity attribute in the library before parsing the settings file. See 07001. The attribute may have to be registered both for formatter and filter parsers,if you want to filter records based on severity level.

OK,this is working but I had to add a stream input/extraction function and then I had to add the following two lines before loading the settings from the settings file:

logging::register_simple_formatter_factory<ESeverityLevel,char>("Severity");
logging::register_simple_filter_factory<ESeverityLevel,char>("Severity");

相关文章

对象的传值与返回说起函数,就不免要谈谈函数的参数和返回值...
从实现装饰者模式中思考C++指针和引用的选择最近在看...
关于vtordisp知多少?我相信不少人看到这篇文章,多半是来自...
那些陌生的C++关键字学过程序语言的人相信对关键字并...
命令行下的树形打印最近在处理代码分析问题时,需要将代码的...
虚函数与虚继承寻踪封装、继承、多态是面向对象语言的三大特...