Wednesday, 25 December 2013

use boost log step 7

Keep moving on step 6. This time I will show how to customize severity level. I have two sinks in step 6. I hope my sink2 will only record the log whose  level is equal to report. Below is how to complete this.
1. Comment namespace logging::trivial
//using namespace logging::trivial;
I will not use default severity level from trivial now.

2. Define my own enum
 enum sign_severity_level {
  trace,
  debug,
  info,
  warning,
  error,
  fatal,
  report
};
3. Update format for two sinks like so:
  sink1->set_formatter (
expr::format("[%1%]<%2%>(%3%): %4%")
% expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S")
% expr::attr<sign_severity_level>("Severity")
% expr::attr<attrs::current_thread_id::value_type >("ThreadID")
% expr::smessage
);
4. Create  severity_logger with new enum
src::severity_logger<sign_severity_level> lg;
5. Change the filter of sink2
   sink2->set_filter(expr::attr<sign_severity_level>("Severity") == report);

The following is full example code:
 #include <fstream>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/thread/thread.hpp>
#include <boost/log/attributes/scoped_attribute.hpp>
namespace logging = boost::log;
namespace src = boost::log::sources;
namespace expr = boost::log::expressions;
namespace sinks = boost::log::sinks;
namespace keywords = boost::log::keywords;
namespace attrs = boost::log::attributes;
//using namespace logging::trivial;
enum sign_severity_level {
  trace,
  debug,
  info,
  warning,
  error,
  fatal,
  report
};
void InitLog() {
  typedef sinks::synchronous_sink<sinks::text_ostream_backend> text_sink;
  boost::shared_ptr< text_sink > sink1 = boost::make_shared<text_sink>();
  sink1->locked_backend()->add_stream(
     boost::make_shared< std::ofstream >("sign.log"));
  sink1->set_formatter (
expr::format("[%1%]<%2%>(%3%): %4%")
% expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S")
% expr::attr<sign_severity_level>("Severity")
% expr::attr<attrs::current_thread_id::value_type >("ThreadID")
% expr::smessage
);
  logging::core::get()->add_sink(sink1);
  sink1->set_filter(expr::attr<sign_severity_level>("Severity") >= warning);
  boost::shared_ptr< text_sink > sink2 = boost::make_shared< text_sink >();
  sink2->locked_backend()->add_stream(
     boost::make_shared< std::ofstream >("sign.csv"));
  sink2->set_formatter (
expr::format("%1%,%2%,%3%")
% expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S")
% expr::attr<sign_severity_level>("Severity")
% expr::smessage
);
  logging::core::get()->add_sink(sink2);
  sink2->set_filter(expr::attr<sign_severity_level>("Severity") == report);
  logging::add_common_attributes();
  //  BOOST_LOG_SCOPED_THREAD_TAG("ThreadID", boost::this_thread::get_id());
  logging::core::get()->add_global_attribute("ThreadID", attrs::current_thread_id());
}
int main(int, char*[]) {
  InitLog();
  src::severity_logger<sign_severity_level> lg;
  BOOST_LOG_SEV(lg, trace) << "A trace severity message";
  BOOST_LOG_SEV(lg, debug) << "A debug severity message";
  BOOST_LOG_SEV(lg, info) << "An informational severity message";
  BOOST_LOG_SEV(lg, warning) << "A warning severity message";
  BOOST_LOG_SEV(lg, error) << "An error severity message";
  BOOST_LOG_SEV(lg, fatal) << "A fatal severity message";
  BOOST_LOG_SEV(lg, report) << "A report severity message";
  return 0;
}

No comments:

Followers

Contributors