3
votes

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.

1

1 Answers

2
votes

I've found that functions of the following signature fit the requirements of the set_filter method:

bool(const boost::log::attribute_value_set&)

Using this signature, the Trivial logging with filters example from Boost.Log tutorial can be implemented as follows:

#include <boost/log/trivial.hpp>
#include <boost/log/core.hpp>
#include <boost/log/expressions.hpp>

namespace logging = boost::log;
namespace sinks = boost::log::sinks;

void init_logging()
{
    logging::core::get()->set_filter([](const logging::attribute_value_set& attr_set)
    {
        return attr_set["Severity"].extract<logging::trivial::severity_level>() >= logging::trivial::info;
    });
}

int main(int argc, char** argv)
{
    init_logging();

    BOOST_LOG_TRIVIAL(trace) << "Trace level log message";
    BOOST_LOG_TRIVIAL(debug) << "Debug level log message";
    BOOST_LOG_TRIVIAL(info) << "Info level log message";
    BOOST_LOG_TRIVIAL(warning) << "Warning level log message";
    BOOST_LOG_TRIVIAL(fatal) << "Fatal level log message";
    return 0;
}