Search code examples
c++asynchronousloggingboostsyslog

Boost.Log asynchronous logging with syslog backend


I am implementing a wrapper on Boost.log for logging sync and async ways. As bellow I defined asychronous sink frontend with with syslog_backend.

typedef sinks::asynchronous_sink<
    sinks::syslog_backend,
    sinks::unbounded_ordering_queue<
    logging::attribute_value_ordering< unsigned int, std::less< unsigned int > >
>
> Async_syslog_sink;

But inside init() when I use this definition to create a backend and add to logging::core, an error was occured.

// Create a backend
boost::shared_ptr< Async_syslog_sink > sink(new Async_syslog_sink(
    keywords::facility = sinks::syslog::user,
    keywords::use_impl = sinks::syslog::native
));
sink->set_formatter(&my_formatter);
logging::core::get()->add_sink(sink);

The error is:

/usr/include/boost/log/sinks/async_frontend.hpp: In instantiation of ‘boost::log::v2_mt_posix::sinks::asynchronous_sink::asynchronous_sink(bool) [with SinkBackendT = boost::log::v2_mt_posix::sinks::syslog_backend; QueueingStrategyT = boost::log::v2_mt_posix::sinks::unbounded_ordering_queue > >]’: /root/sample-logger/src/Loggers/Async/AsyncLogger.h:314:9:
required from here /usr/include/boost/log/sinks/async_frontend.hpp:230:31: error: no matching function for call to ‘boost::log::v2_mt_posix::sinks::unbounded_ordering_queue > >::unbounded_ordering_queue()’ m_FlushRequested(false)

How Can I have an asynchronous logger with syslog backend?

thanks.


Solution

  • You need to provide an ordering predicate in the keywords::order named parameter of the sink constructor. Given that you use logging::attribute_value_ordering, you should specify the attribute name based on which you want to order log records when constructing the predicate. For example:

    keywords::order = logging::attribute_value_ordering< unsigned int, std::less< unsigned int > >("RecordID")