2015-04-17 44 views
1

我正在學習Boost。在教程之後,我嘗試通過發送對方法onlyWarnings的引用來設置接收器上的過濾器。Boost set_filter無效

簡介:

sink->set_filter(&onlyWarnings); 

在onlyWarnings:

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

我顯然缺少在我的代碼的東西,教程的一個重要組成部分。

部首:

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

回答

2

你確定嚴重程度只是另一個屬性?我建議你從工作的例子開始,像這樣的:

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

你得到他們的工作,你開始自己的編碼之前? 標記嚴重性記錄器有一個示例過濾器,與您的看起來非常不同。

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

也許嘗試更多的東西一樣:

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

約翰:我還有一個問題,你也許能幫助,設在這裏:http://stackoverflow.com/q/29785243/1735836 – Patricia

相關問題