升压日志severity_logger init_from_stream [英] Boost log severity_logger init_from_stream

查看:481
本文介绍了升压日志severity_logger init_from_stream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用升压1.54.0。
下面你可以找到,说明我的问题最少的例子。

我使用boost日志severity_logger。
我想从一个流配置我汇。
(在下面的例子中我使用了一个字符串流。
在我的实际应用数据流来自一个文件。)
我想用%严重性%输出或过滤的目的。

我的问题是:如果我用它作为在下面的例子中给出的%严重性%为空

如预期

%,另一LineID%和%消息%充满。
如果我成立了一个水槽作为outcommented行给出,它按预期工作。

任何想法?

 的#include<升压/日志/来源/ severity_logger.hpp>
#包括LT&;升压/日志/来源/ record_ostream.hpp>
#包括LT&;升压/日志/工具/设置/ common_attributes.hpp>
#包括LT&;升压/日志/工具/设置/ from_stream.hpp>
#包括LT&;升压/日志/工具/设置/ console.hpp>
#包括LT&;升压/日志/ EX pressions.hpp>枚举SeverityLevel {痕迹,致命};INT主(INT ARGC,CHAR *的argv [])
{
    提高::登录:: add_common_attributes();
    / *
    结构severity_tag;
    提高::登录:: add_console_log(的std ::堵塞,
        提高::登录::关键词:格式=(
            提高::登录::前pressions ::流
                <<提高::登录::前pressions :: ATTR< unsigned int类型>(另一LineID)
                << :其中, <<提高::登录::前pressions :: ATTR< SeverityLevel,severity_tag>(严重性)
                << >中与所述;&下;提高::登录::前pressions :: smessage)
    ); * /    的std :: stringstream的S;
    小号所述&;&下; [Sinks.MySink]<<的std :: ENDL;
    小号所述&;&下; 目标=控制台<<的std :: ENDL;
    小号所述&;&下; 格式= \\%另一LineID%:其中,%严重性%GT; - %消息%\\<<的std :: ENDL;
    提高::登录:: init_from_stream(S);    提高::登录::来源:: severity_logger< SeverityLevel> LG;
    BOOST_LOG_SEV(LG,跟踪)LT;< 这是一个跟踪消息;
    BOOST_LOG_SEV(LG,致命)LT;< 这是一个致命的消息;
    返回0;
}


解决方案

您是对的。这是一个已知的bug,并固定在当前开发版本。

下面是错误报告:<一href=\"https://svn.boost.org/trac/boost/ticket/8840\">https://svn.boost.org/trac/boost/ticket/8840

为了使这个答案不那么依赖这里的链接是它是在报告内解决方式:


  

您需要解析的设置文件之前登记在库中的严重程度属性。见<一href=\"http://www.boost.org/doc/libs/1_54_0/libs/log/doc/html/log/extension/settings.html#log.extension.settings.adding_support_for_user_defined_types_to_the_formatter_parser\">here.该属性可能有格式化和过滤分析器进行注册两种,如果你想根据严重程度来筛选记录。


  
  

OK,这是工作,但我不得不添加流输入/提取功能,然后我从设置文件加载设置之前下面两行添加:


 记录:: register_simple_formatter_factory&LT; ESeverityLevel,焦炭&GT;(严重性);
记录:: register_simple_filter_factory&LT; ESeverityLevel,焦炭&GT;(严重性);

I am using boost 1.54.0. Below you can find a minimum example that illustrates my problem.

I use the severity_logger of boost log. I want to configure my sinks from a stream. (In the following example I use a stringstream. In my real application the stream comes from a file.) I want to use the %Severity% for output or filtering purposes.

My problem is: If I use it as given in the example below, %Severity% is empty.

%LineID% and %Message% are filled as expected. If I set up a sink as given in the outcommented lines, it works as expected.

Any ideas?

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

解决方案

You are right. This is a known bug and fixed in the current development version.

Here is the bug report: https://svn.boost.org/trac/boost/ticket/8840

To make this answer less dependent on the link here is the way it was resolved within the report:

You need to register your severity attribute in the library before parsing the settings file. See here. 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"); 

这篇关于升压日志severity_logger init_from_stream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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