I have created a logger mechanism based on Boost Log.

My code is based on the trivial logger as shown in this example.

I was wondering how to automatically call system exit

exit(1)

(or any other custom callback function) whenever a fatal error occurs.

Any help is welcomed!

UPDATE:

The solution is to extend the backend sink by overloading the consume() method.

有帮助吗?

解决方案

Example of a sink examining the severity level of the trivial logger:

#include <boost/log/trivial.hpp>
#include <boost/log/sinks/basic_sink_backend.hpp>
#include <boost/log/attributes/value_extraction.hpp>
#include <boost/log/sinks/async_frontend.hpp>
namespace sinks = boost::log::sinks;

void initBoostLog() {
  struct Sink: public sinks::basic_formatted_sink_backend<char, sinks::concurrent_feeding> {
    void consume (const boost::log::record_view& rec, const string& str) {
      using boost::log::trivial::severity_level;
      auto severity = rec.attribute_values()[boost::log::aux::default_attribute_names::severity()].extract<severity_level>();
      if (!severity || severity.get() <= severity_level::info) {
        std::cout << str << std::endl;
      } else {
        std::cerr << str << std::endl;
      }
    }
  };

  typedef sinks::asynchronous_sink<Sink> sink_t; boost::shared_ptr<sink_t> sink (new sink_t());
  boost::shared_ptr<boost::log::core> logc = boost::log::core::get();
  logc->add_sink (sink);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top