在过去的三个小时里,我一直被以下编译错误搞糊涂了。谁能告诉我这是怎么回事?
我试图将 log::formatter(在下面标记)定义为它自己的变量,因此它可以在几个地方重新使用。但是,在尝试重新使用它时出现编译错误。
但是,如果我完全摆脱那个变量,而是复制并粘贴代码,它就会编译。有没有搞错?有什么办法可以做我想做的事吗?
boost::shared_ptr<log::core> logger = log::core::get();
logger->set_logging_enabled( enabled );
logger->set_filter(trivial::severity >= level);
logger->add_global_attribute(
"TimeStamp", attr::local_clock());
logger->add_global_attribute(
"ProcessID", attr::current_process_id());
logger->add_global_attribute(
"ThreadID", attr::current_thread_id());
// want this to be it's own variable
log::formatter fmt = expr::stream
<< "[" << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%d.%m.%Y %H:%M:%S.%f") << "] "
<< "[" << expr::attr< attr::current_process_id::value_type >("ProcessID") << "] "
<< "[" << expr::attr< attr::current_thread_id::value_type >("ThreadID") << "] "
<< "[" << expr::attr< std::string >("Channel") << "] "
<< "[" << expr::attr< severity_level >("Severity") << "]: "
<< expr::smessage
;
// so it can be reused here, but this is a compiler error
log::add_console_log(
std::clog, keywords::format=fmt);
// and here, too. But this is also a compiler error
log::add_file_log(
"test.log", keywords::format=fmt);
编译错误(使用 clang++)是:
In file included from ../src/util/logging/Logging.cpp:34:
In file included from /opt/boost-1.54.0/include/boost/log/utility/setup/console.hpp:22:
/opt/boost-1.54.0/include/boost/log/detail/sink_init_helpers.hpp:107:21: error: no matching function for call to 'acquire_formatter'
s.set_formatter(aux::acquire_formatter(args[keywords::format]));
^~~~~~~~~~~~~~~~~~~~~~
/opt/boost-1.54.0/include/boost/log/utility/setup/console.hpp:76:5: note: in instantiation of function template specialization 'boost::log::v2_mt_posix::aux::setup_format
boost::log::v2_mt_posix::sinks::synchronous_sink<boost::log::v2_mt_posix::sinks::basic_text_ostream_backend<char> >, boost::parameter::aux::tagged_argument<boost::log::v2
posix::keywords::tag::format, boost::log::v2_mt_posix::basic_formatter<char> > >' requested here
aux::setup_formatter(*pSink, args,
^
/opt/boost-1.54.0/include/boost/log/utility/setup/console.hpp:136:12: note: in instantiation of function template specialization 'boost::log::v2_mt_posix::aux::add_consol
g<char, boost::parameter::aux::tagged_argument<boost::log::v2_mt_posix::keywords::tag::format, boost::log::v2_mt_posix::basic_formatter<char> > >' requested here
return aux::add_console_log(strm, arg1);
^
../src/util/logging/Logging.cpp:121:2: note: in instantiation of function template specialization 'boost::log::v2_mt_posix::add_console_log<char, boost::parameter::aux::t
d_argument<boost::log::v2_mt_posix::keywords::tag::format, boost::log::v2_mt_posix::basic_formatter<char> > >' requested here
log::add_console_log(
^
/opt/boost-1.54.0/include/boost/log/detail/sink_init_helpers.hpp:80:33: note: candidate template ignored: failed template argument deduction
inline basic_formatter< CharT > acquire_formatter(const CharT* formatter)
^
/opt/boost-1.54.0/include/boost/log/detail/sink_init_helpers.hpp:85:33: note: candidate template ignored: failed template argument deduction
inline basic_formatter< CharT > acquire_formatter(std::basic_string< CharT, TraitsT, AllocatorT > const& formatter)
^
/opt/boost-1.54.0/include/boost/log/detail/sink_init_helpers.hpp:91:5: note: candidate template ignored: disabled by 'enable_if' [with FormatterT = boost::log::v2_mt_posi
asic_formatter<char>]
phoenix::is_actor< FormatterT >,
^
但是,如果我改为将其改为这样,它会编译:
boost::shared_ptr<log::core> logger = log::core::get();
logger->set_logging_enabled( enabled );
logger->set_filter(trivial::severity >= level);
logger->add_global_attribute(
"TimeStamp", attr::local_clock());
logger->add_global_attribute(
"ProcessID", attr::current_process_id());
logger->add_global_attribute(
"ThreadID", attr::current_thread_id());
// copy and paste the expression
log::add_console_log(
std::clog,
keywords::format =
(
expr::stream
<< "[" << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%d.%m.%Y %H:%M:%S.%f") << "] "
<< "[" << expr::attr< attr::current_process_id::value_type >("ProcessID") << "] "
<< "[" << expr::attr< attr::current_thread_id::value_type >("ThreadID") << "] "
<< "[" << expr::attr< std::string >("Channel") << "] "
<< "[" << expr::attr< severity_level >("Severity") << "]: "
<< expr::smessage
)
);
// copy and paste the expression again
log::add_file_log(
"test.log",
keywords::format =
(
expr::stream
<< "[" << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%d.%m.%Y %H:%M:%S.%f") << "] "
<< "[" << expr::attr< attr::current_process_id::value_type >("ProcessID") << "] "
<< "[" << expr::attr< attr::current_thread_id::value_type >("ThreadID") << "] "
<< "[" << expr::attr< std::string >("Channel") << "] "
<< "[" << expr::attr< severity_level >("Severity") << "]: "
<< expr::smessage
)
);
最佳答案
log::formatter 类型是实际(相当复杂的)表达式类型的包装器。由于某些原因,它不能与 add_file/console_log 一起使用。
如果您使用的是 C++11,auto 关键字将避免包装类:
auto fmt = expr::stream
<< ...
如果没有 C++11,模板函数参数将起作用:
template <class F>
void add_logs(const F & fmt)
{
log::add_console_log(std::clog, keywords::format = fmt);
log::add_file_log("test.log", keywords::format = fmt);
}
add_logs(expr::stream
<< ...
);
关于c++ - Boost Log - 为什么不编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17766998/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?
我刚刚被困在这个问题上一段时间了。以这个基地为例:moduleTopclassTestendmoduleFooendend稍后,我可以通过这样做在Foo中定义扩展Test的类:moduleTopmoduleFooclassSomeTest但是,如果我尝试通过使用::指定模块来最小化缩进:moduleTop::FooclassFailure这失败了:NameError:uninitializedconstantTop::Foo::Test这是一个错误,还是仅仅是Ruby解析变量名的方式的逻辑结果? 最佳答案 Isthisabug,or
我正在学习如何在我的Ruby代码中使用Module.prepend而不是alias_method_chain,我注意到有些人使用send调用它(example):ActionView::TemplateRenderer.send(:prepend,ActionViewTemplateRendererWithCurrentTemplate)而其他人直接调用它(example):ActionView::TemplateRenderer.prepend(ActionViewTemplateRendererWithCurrentTemplate)而且,虽然我还没有看到任何人使用这种风格,但我从
我在用Ruby执行简单任务时遇到了一件奇怪的事情。我只想用每个方法迭代字母表,但迭代在执行中先进行:alfawit=("a".."z")puts"That'sanalphabet:\n\n#{alfawit.each{|litera|putslitera}}"这段代码的结果是:(缩写)abc⋮xyzThat'sanalphabet:a..z知道为什么它会这样工作或者我做错了什么吗?提前致谢。 最佳答案 因为您的each调用被插入到在固定字符串之前执行的字符串文字中。此外,each返回一个Enumerable,实际上您甚至打印它。试试
我不知道为什么,但是当我设置这个设置时它无法编译设置:static_cache_control,[:public,:max_age=>300]这是我得到的syntaxerror,unexpectedtASSOC,expecting']'(SyntaxError)set:static_cache_control,[:public,:max_age=>300]^我只想将“过期”header设置为css、javaascript和图像文件。谢谢。 最佳答案 我猜您使用的是Ruby1.8.7。Sinatra文档中显示的语法似乎是在Ruby1.
这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Rubysyntaxquestion:Rational(a,b)andRational.new!(a,b)我正在阅读ruby镐书,我对创建有理数的语法感到困惑。Rational(3,4)*Rational(1,2)产生=>3/8为什么Rational不需要new方法(我还注意到例如我可以在没有new方法的情况下创建字符串)?