C ++如何设置严重性过滤器在促进全球记录器 [英] C++ How to set a severity filter on a Boost Global Logger

查看:164
本文介绍了C ++如何设置严重性过滤器在促进全球记录器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想了好几天,现在创建整个应用程序的升压全局记录器使用,但我似乎无法获得严重性级别的全局记录

设置

重要:

看看下面安德烈的回答...它打成步骤(a)和(b),但我还没有得到它的权利!


右键出Boost文档的此处


  

...这将是有一个或几个全球伐木工人更方便
  以便在需要时方便地访问它们的每一个地方。在这方面
  性病::法院就是这样一个记录器的一个很好的例子。


  
  

该库提供了一个方法来声明全局记录器,可以
  访问pretty很像的std ::法院。其实这个功能可以用于
  与任何记录,包括用户定义的。在宣布全球
  记录仪,可以肯定有这个记录一个线程安全的访问
  比如从应用code中的任何地方。该库还
  保证一个全球性的记录器实例将是唯一的甚至可以跨越
  模块的边界。这允许使用记录,即使在仅标头
  可能会被编译成不同的模块组件。


  
  

  
  

不管您用于声明记录的宏,你可以
  与记录器的静态get函数获取记录器实例
  标签:


  SRC :: severity_logger_mt< >&安培; LG = my_logger :: get()方法;

我来自Boost记录仪大师发现了,安德烈,我的问题是,的严重性类型不匹配


  

您已经实例化severity_logger_mt与默认模板
  参数,因此严重等级属性的类型为int。您枚举
  值被转换为int和发送到日志芯。你还没有
  设立任何水槽,因此默认情况下将使用默认的水槽。水槽
  试图从日志记录提取级别的属性值,
  但没有这样做,因为它预计的严重等级限制的
  类型boost ::登录::琐碎:: severity_level。该故障后下沉
  回落提振::登录::琐碎:: severity_level ::信息严重程度。


  
  

如果你想使用你的枚举你必须严重性级别:


------------------------------------(现在,这里就是答案! !)---------------------------------------


  

(一)在记录模板参数指定它和


  
  

(二)成立了一个格式化是了解你的枚举水槽。


但我无法弄清楚如何做到这一点,因为即使想后,按照他的指示严重级别看起来像汇仍然是回落到了boost ::登录::琐碎:: severity_level ::信息严重性。谁能帮我弄清楚如何获得的严重性在我的全球记录器设置是否正确?这里是code:

标题

 的#include<升压/日志/ trivial.hpp>
#包括LT&;升压/日志/来源/ global_logger_storage.hpp>枚举severity_level
{
    正常,
    警告,
    错误,
    危急
};BOOST_LOG_GLOBAL_LOGGER(记录仪,提振::登录::来源:: severity_logger_mt< severity_level>)

CPP

 的#includeGlobalLogger.h#包括LT&;升压/日志/ EX pressions /格式化/ date_time.hpp>
#包括LT&;升压/日志/ EX pressions.hpp>
#包括LT&;升压/日志/片/ sync_frontend.hpp>
#包括LT&;升压/日志/片/ text_ostream_backend.hpp>
#包括LT&;升压/日志/支持/ date_time.hpp>
#包括LT&;升压/核心/ null_deleter.hpp>
#包括LT&;升压/日志/工具/设置/ common_attributes.hpp>
#包括LT&;升压/ make_shared.hpp>
#包括LT&;升压/日志/工具/设置/ file.hpp>
#包括LT&;升压/日志/工具/设置/ console.hpp>
#包括LT&;升压/日志/ sinks.hpp>
#包括LT&;&的fstream GT;空间日志=提振::登录;
命名空间SRC =的boost ::登录::来源;
命名空间EXPR =提振::登录::前pressions;
命名空间=下沉的boost ::登录::汇;
命名空间的attrs =提振::登录::属性;布尔onlyWarnings(常量的boost ::登录:: attribute_value_set&安培;集)
{
    返回设置[严重性]提取< severity_level>()> 0;
}无效severity_and_message(常量的boost ::登录:: record_view&放大器;查看,提振::登录:: formatting_ostream&放大器; OS)
{
    OS<< view.attribute_values​​()[严重性]提取< severity_level>()<< :&所述;&下;
    view.attribute_values​​()[消息]提取<标准::字符串>();
}BOOST_LOG_GLOBAL_LOGGER_INIT(记录仪,提振::登录::来源:: severity_logger_mt< severity_level>)
{
    提高::登录::来源:: severity_logger_mt< severity_level>记录仪;    //添加一个文本水槽
    汇的typedef :: asynchronous_sink<汇:: text_ostream_backend> text_sink;
    提高:: shared_ptr的< text_sink> =下沉的boost :: make_shared< text_sink>();    //添加控制台输出流水槽我们
    提高:: shared_ptr的<的std :: ostream的>流{&放大器;的std ::堵塞,提振:: null_deleter {}};
    sink-> locked_backend() - > add_stream(流);    //指定日志消息的格式
    sink-> set_formatter(安培; severity_and_message);    //只要登录严重性&GT消息; = SEVERITY_THRESHOLD写
    sink-> set_filter(安培; onlyWarnings);    //注册我们的汇
    记录::核心::得到() - GT; add_sink(片);    测井:: add_common_attributes();    返回记录仪;
}

的main.cpp

 的#include<&iostream的GT;#包括GlobalLogger.h使用命名空间std;诠释主(){
    提高::登录::来源:: severity_logger_mt< severity_level>&安培; LG =记录::得到();
    BOOST_LOG_SEV(LG,severity_level ::正常)LT;< 注意;
    BOOST_LOG_SEV(LG,severity_level ::警告)LT;< 警告;
    BOOST_LOG_SEV(LG,severity_level ::关键)LT;< 危急;    返回0;
}

我找到了一个更好的例子 62.10。宏定义一个全局记录器并在工作版本这太问题。但工作实施例不使用get()方法。所以,宣告BOOST_LOG_GLOBAL_LOGGER后,我能够访问日志::得到(),但我仍然无法得到它认识到严重性


解决方案

我找到安德烈,升压日志专家。为了帮助别人,将来,我张贴的链接到我们的 Sourceforge上讨论。经过一番敲打我的头我的显示器上,它带着explanaition和回访工作版本中这太问题。但我终于得到它的工作! YAY !!!

I've been trying for days now to create a Boost Global Logger for use throughout the application but I can't seem to get the severity level set in the Global Logger.

IMPORTANT:

Look below for Andrey's answer...it's labeled as step (a) and (b) but I'm still not getting it right!


Right out of the Boost Documentation here

...it would be more convenient to have one or several global loggers in order to easily access them in every place when needed. In this regard std::cout is a good example of such a logger.

The library provides a way to declare global loggers that can be accessed pretty much like std::cout. In fact, this feature can be used with any logger, including user-defined ones. Having declared a global logger, one can be sure to have a thread-safe access to this logger instance from any place of the application code. The library also guarantees that a global logger instance will be unique even across module boundaries. This allows employing logging even in header-only components that may get compiled into different modules.


Regardless of the macro you used to declare the logger, you can acquire the logger instance with the static get function of the logger tag:

src::severity_logger_mt< >& lg = my_logger::get();

I found out from the Boost Logger guru, Andrey, that my problem is the severity type mismatch.

You have instantiated severity_logger_mt with default template parameters, so the severity level attribute has type int. Your enum values are converted to int and sent to the logging core. You have not set up any sinks, so by default the default sink is used. The sink attempts to extract severity level attribute value from log records, but fails to do that because it expects the severity level to be of type boost::log::trivial::severity_level. After that failure the sink falls back to boost::log::trivial::severity_level::info severity.

If you want to use your enum for severity levels you have to:

------------------------------------ ( Now, here is the ANSWER !!!! ) ---------------------------------------

(a) specify it in the logger template parameters and

(b) set up a sink with a formatter that is aware of your enum.

But I can't figure out how to do this, because even after trying to follow his instructions the severity level looks like the sink is still falling back to the boost::log::trivial::severity_level::info severity. Can anyone help me figure out how to get the severity set correctly in my global logger? Here is the code:

HEADER

#include <boost/log/trivial.hpp>
#include <boost/log/sources/global_logger_storage.hpp>

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

BOOST_LOG_GLOBAL_LOGGER(logger, boost::log::sources::severity_logger_mt< severity_level >)

CPP

#include "GlobalLogger.h"

#include <boost/log/expressions/formatters/date_time.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/core/null_deleter.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/make_shared.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/sinks.hpp>
#include <fstream>

namespace logging = boost::log;
namespace src = boost::log::sources;
namespace expr = boost::log::expressions;
namespace sinks = boost::log::sinks;
namespace attrs = boost::log::attributes;

bool onlyWarnings(const boost::log::attribute_value_set& set)
{
    return set["Severity"].extract<severity_level>() > 0;
}

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

BOOST_LOG_GLOBAL_LOGGER_INIT(logger, boost::log::sources::severity_logger_mt< severity_level >)
{
    boost::log::sources::severity_logger_mt< severity_level > logger;

    // add a text sink
    typedef sinks::asynchronous_sink<sinks::text_ostream_backend> text_sink;
    boost::shared_ptr<text_sink> sink = boost::make_shared<text_sink>();

    // add "console" output stream to our sink
    boost::shared_ptr<std::ostream> stream{&std::clog, boost::null_deleter{}};
    sink->locked_backend()->add_stream(stream);

    // specify the format of the log message
    sink->set_formatter(&severity_and_message);

    // just log messages with severity >= SEVERITY_THRESHOLD are written
    sink->set_filter(&onlyWarnings);

    // "register" our sink
    logging::core::get()->add_sink(sink);

    logging::add_common_attributes();

    return logger;
}

main.cpp

#include <iostream>

#include "GlobalLogger.h"

using namespace std;

int main() {
    boost::log::sources::severity_logger_mt<  severity_level >& lg = logger::get();
    BOOST_LOG_SEV(lg, severity_level::normal) << "note";
    BOOST_LOG_SEV(lg, severity_level::warning) << "warning";
    BOOST_LOG_SEV(lg, severity_level::critical) << "critical";

    return 0;
}

I found a better example 62.10. A macro to define a global logger and a working version in this SO question. But the working example does not use the get() method. So, after declaring the BOOST_LOG_GLOBAL_LOGGER, I was able to access log::get() but I still can't get it to recognize the severity.

解决方案

I found Andrey, the Boost Log expert. To help others in the future, I am posting a link to our Sourceforge discussion. After much banging my head on my monitor, it took his explanaition and revisiting the working version in this SO question. But I finally got it working! YAY!!!

这篇关于C ++如何设置严重性过滤器在促进全球记录器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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