升压set_filter不工作 [英] Boost set_filter is not working

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

问题描述

我学习的推动作用。下面的教程,我试​​图通过发送一个参考方法onlyWarnings设置在水槽的过滤器。

简介:

  sink-> set_filter(安培; onlyWarnings);

在onlyWarnings:

 设置[严重性]提取< INT>()//始终为0

我显然缺少我的code的东西,教程的一个重要组成部分。

标题:

 的#ifndef ONEPRINT_LOGGER_H
#定义ONEPRINT_LOGGER_H#包括LT&;升压/日志/核心/ core.hpp>
#包括LT&;升压/日志/属性/ attribute_value_set.hpp>
#包括LT&;升压/日志/ trivial.hpp>
#包括LT&;升压/日志/来源/ severity_logger.hpp>
#包括LT&;升压/日志/工具/设置/ file.hpp>
#包括LT&;升压/日志/工具/设置/ console.hpp>
#包括LT&;升压/日志/工具/设置/ common_attributes.hpp>
#包括LT&;升压/日志/ sinks.hpp>
#包括LT&;升压/核心/ null_deleter.hpp>
#包括LT&;&iostream的GT;命名空间EXPR =提振::登录::前pressions;
命名空间来源=的boost ::登录::来源;
命名空间=下沉的boost ::登录::汇;
命名空间关键字=提振::登录::关键词;命名空间IDS {    枚举severity_level
    {
        正常,
        警告,
        错误,
        危急
    };    类记录器{
    上市:
        记录仪();
        〜记录仪();
        无效的Logit(的std :: string信息);    保护:
        汇的typedef :: asynchronous_sink<汇:: text_ostream_backend> asynchronousSink;
        无效设置→();
    };}
#ENDIF // ONEPRINT_LOGGER_H

CPP:

 的#includeLogger.h一流的计数器;使用命名空间的ID;命名空间{//非成员方法
    布尔onlyWarnings(常量的boost ::登录:: attribute_value_set&安培;集)
    {
        返回设置[严重性]提取<&诠释GT;()> 0;
    }    无效severity_and_message(常量的boost ::登录:: record_view&放大器;查看,提振::登录:: formatting_ostream&放大器; OS)
    {
        OS<< 。view.attribute_values​​()[严重性]提取<&诠释GT;()<< :&所述;&下;
        view.attribute_values​​()[消息]提取<标准::字符串>();
    }
}记录仪记录仪::(){
    设置→();
    Logit模型(测试);
}记录仪::〜记录仪(){}无效记录仪::设置→()
{
    提高:: shared_ptr的<提高::登录::核心>核心=提振::登录::核心::得到();
    提高:: shared_ptr的<汇:: text_ostream_backend>后端=的boost :: make_shared<汇:: text_ostream_backend>();    提高:: shared_ptr的<记录仪:: asynchronousSink>沉(新记录器:: asynchronousSink(后端));
    提高:: shared_ptr的<的std :: ostream的>流{&放大器;的std ::堵塞,提振:: null_deleter {}};
    sink-> locked_backend() - > add_stream(流);    sink-> set_filter(安培; onlyWarnings);
    sink-> set_formatter(安培; severity_and_message);    核心 - > add_sink(片);
}无效记录仪:: Logit模型(的std :: string信息){
    BOOST_LOG_TRIVIAL(警告)LT;<味精;    来源:: severity_logger< severity_level> severityLogger;
    BOOST_LOG_SEV(severityLogger,关键)LT;<味精;
}


解决方案

您确定严重程度只是另一个属性?我建议你​​从开始工作的例子,像这样的:

http://www.boost.org/doc/libs/1_56_0/libs/log/doc/html/log/tutorial/advanced_filtering.html

http://boost-log.sourceforge.net/库/日志/例子/ DOC / tutorial_filtering.cpp

你让他们工作,你开始自己的编码之前?
标签严重性记录仪有一个例子过滤器,看起来你很不同。

 布尔my_filter(登录:: value_ref< severity_level,标签::严重程度和GT;常量和放大器;水平,
               记录:: value_ref<标准::字符串,标签:: tag_attr>常量和放大器;标签)
{
    返回级> = ||警告标签==IMPORTANT_MESSAGE
}

也许尝试更多的东西,如:

 布尔my_filter(登录:: value_ref< severity_level,标签::严重程度和GT;常量和放大器;电平)
{
    返回级> =警告;
}

I'm learning Boost. Following a tutorial, I try to set a filter on a sink by sending a reference to the method onlyWarnings.

Brief:

sink->set_filter(&onlyWarnings);

In onlyWarnings:

set["Severity"].extract<int>()  // is always 0

I'm obviously missing something in my code and an important part of the tutorial.

HEADER:

#ifndef ONEPRINT_LOGGER_H
#define ONEPRINT_LOGGER_H

#include <boost/log/core/core.hpp>
#include <boost/log/attributes/attribute_value_set.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/sinks.hpp>
#include <boost/core/null_deleter.hpp>
#include <iostream>

namespace expr = boost::log::expressions;
namespace sources = boost::log::sources;
namespace sinks = boost::log::sinks;
namespace keywords = boost::log::keywords;

namespace ids {

    enum severity_level
    {
        normal,
        warning,
        error,
        critical
    };

    class Logger {
    public:
        Logger();
        ~Logger();
        void logIt(std::string msg);

    protected:
        typedef sinks::asynchronous_sink<sinks::text_ostream_backend> asynchronousSink;
        void setupLogging();
    };

}
#endif //ONEPRINT_LOGGER_H

CPP:

#include "Logger.h"

class counter;

using namespace ids;

namespace {  // NON-MEMBER METHODS
    bool onlyWarnings(const boost::log::attribute_value_set& set)
    {
        return set["Severity"].extract<int>() > 0;
    }

    void severity_and_message(const boost::log::record_view &view, boost::log::formatting_ostream &os)
    {
        os << view.attribute_values()["Severity"].extract<int>() << ": " <<
        view.attribute_values()["Message"].extract<std::string>();
    }
}

Logger::Logger() {
    setupLogging();
    logIt("Testing");
}

Logger::~Logger() {

}

void Logger::setupLogging()
{
    boost::shared_ptr< boost::log::core > core = boost::log::core::get();
    boost::shared_ptr<sinks::text_ostream_backend> backend = boost::make_shared<sinks::text_ostream_backend>();

    boost::shared_ptr<Logger::asynchronousSink> sink(new Logger::asynchronousSink(backend));
    boost::shared_ptr<std::ostream> stream{&std::clog, boost::null_deleter{}};
    sink->locked_backend()->add_stream(stream);

    sink->set_filter(&onlyWarnings);
    sink->set_formatter(&severity_and_message);

    core->add_sink(sink);
}

void Logger::logIt(std::string msg) {
    BOOST_LOG_TRIVIAL(warning) << msg;

    sources::severity_logger<severity_level> severityLogger;
    BOOST_LOG_SEV(severityLogger, critical) << msg;
}

解决方案

Are you sure that severity is just another attribute? I would recommend that you start from a working example, like this one:

http://www.boost.org/doc/libs/1_56_0/libs/log/doc/html/log/tutorial/advanced_filtering.html

http://boost-log.sourceforge.net/libs/log/example/doc/tutorial_filtering.cpp

Did you get them working before you started your own coding? The tag-severity logger has an example filter that looks very different from yours.

bool my_filter(logging::value_ref< severity_level, tag::severity > const& level,
               logging::value_ref< std::string, tag::tag_attr > const& tag)
{
    return level >= warning || tag == "IMPORTANT_MESSAGE";
}

Perhaps try something more like:

bool my_filter(logging::value_ref< severity_level, tag::severity > const& level)
{
    return level >= warning ;
}

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

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