几天来我一直在尝试创建一个 Boost Global Logger 以在整个应用程序中使用但我似乎无法在 Global Logger 中设置严重性级别。
重要提示:
在下面查看 Andrey 的回答...它被标记为步骤 (a) 和 (b),但我仍然没有做对!
直接来自 Boost 文档 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();
我从 Boost Logger 大师 Andrey 那里了解到,我的问题是严重性类型不匹配。
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:
------------------------------------(现在,答案在这里! !!!) ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------->
(a) specify it in the logger template parameters and (b) set up a sink with a formatter that is aware of your enum. 但我不知道该怎么做,因为即使在尝试按照他的指示进行操作之后,严重性级别看起来像接收器仍在回落到 boost::log::trivial::severity_level::info 严重性.谁能帮我弄清楚如何在我的全局记录器中正确设置严重性?这是代码: 标题 CPP main.cpp 我找到了一个更好的例子62.10. A macro to define a global logger和 this SO question 中的工作版本.但是工作示例不使用 get() 方法。因此,在声明 BOOST_LOG_GLOBAL_LOGGER 之后,我能够访问 log::get() 但我仍然无法让它识别严重性。
#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 >)
#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;
}
#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;
}
最佳答案
我找到了 Boost Log 专家 Andrey。为了将来帮助其他人,我发布了一个链接到我们的 Sourceforge discussion .在我的显示器上撞了很久之后,他解释并重新访问了 this SO question 中的工作版本.但我终于让它工作了!耶!!!
关于C++ 如何在 Boost Global Logger 上设置严重性过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29785243/
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击
我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"