I am implementing a logger using Boost.Log I have a global logger which has one sink to the console and one optional to file if an init method is called.
I would like to use C++11 lambda expression to set severity filters. From the documentation it seems it is possible : http://www.boost.org/doc/libs/1_59_0/libs/log/doc/html/log/tutorial/trivial_filtering.html
However I did not find any examples, and my naive approaches did not compile.
My init function looks like this :
template <typename filter_type>
void init_log_file( filter_type filter)
{
boost::shared_ptr< boost::log::sinks::text_file_backend > backend =
boost::make_shared< boost::log::sinks::text_file_backend >
(
boost::log::keywords::file_name = "log_%Y-%m-%d_%H-%M-%S.%N.log",
boost::log::keywords::rotation_size = 10 * 1024 * 1024,
boost::log::keywords::max_size = 1000 * 1024 * 1024,
boost::log::keywords::min_free_space = 2000 * 1024 * 1024,
boost::log::keywords::auto_flush = true
);
typedef boost::log::sinks::synchronous_sink< boost::log::sinks::text_file_backend > sink_t;
boost::shared_ptr< sink_t > sink(new sink_t(backend));
sink->set_filter( filter(severity) );
//sink->set_filter( filter(severity.or_none(), tag_attr.or_none()) );
boost::log::core::get()->add_sink(sink);
}
and I call the init as follows:
int main()
{
//typedef boost::log::value_ref< custom_severity_level, tag::severity > sev_type;
typedef boost::log::expressions::attribute_keyword<tag::severity> sev_type;
init_log_file([](sev_type const& level)
{
return level == custom_severity_level::ERROR;
});
return 0;
}
I have tried several things but either the lambda argument types do not match or I get the error :
error: implicit instantiation of undefined template 'boost::log::v2_mt_posix::expressions::aux::date_time_formatter_generator_traits' m_name(name), m_formatter(formatter_generator::parse(format)), m_visitor_invoker(fallback)
Any hint on how to do what I want properly would be awesome
Thanks in advance for your replies
EDIT : the answer by Kassiar solved my problem.