c – 升级log severity_logger init_from_stream

前端之家收集整理的这篇文章主要介绍了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");
原文链接:https://www.f2er.com/c/113287.html

猜你在找的C&C++相关文章