提升set_filter不起作用
Boost set_filter is not working
我正在学习Boost。 在教程之后,我尝试通过发送对方法的引用来设置接收器上的筛选器 onlyWarnings。
短:
sink->set_filter(&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;
}
你确定严重性只是另一个属性吗?我建议您从一个工作示例开始,例如:
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 ;
}
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 为什么二进制搜索在我的测试中不起作用
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 带有指定长度字符* 参数的 std::regex_search 在 VS2017 中不起作用?
- Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)
- 为什么简单的算术减法在"if"条件下不起作用?
- 为什么Stroustup书中的has_f不起作用
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- 嵌套While循环不起作用(C++问题)
- C++Matching Brackets 2解决方案不起作用
- 为什么这段代码不起作用,我该如何解决?
- boost zlib filter 在 Windows 中不起作用