Wednesday 25 December 2013

use boost log step 6

This time I will show how to use two sinks in one app. These two sinks have different filter, generate two log files with different formats.
The full code is listed below:
#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;
using namespace logging::trivial;
void InitLog() {
  typedef sinks::synchronous_sink< sinks::text_ostream_backend > text_sink;
  boost::shared_ptr< text_sink > sink1 = boost::make_shared< text_sink >();
    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")
      % logging::trivial::severity
      % expr::attr<boost::log::attributes::current_thread_id::value_type >("ThreadID")
      % expr::smessage
  sink1->set_filter(expr::attr< severity_level >("Severity") >= warning);
  boost::shared_ptr< text_sink > sink2 = boost::make_shared< text_sink >();
    boost::make_shared< std::ofstream >("sign.csv"));
  sink2->set_formatter (
      % expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S")
      % logging::trivial::severity
      % expr::smessage
  sink2->set_filter(expr::attr< severity_level >("Severity") < warning);
  BOOST_LOG_SCOPED_THREAD_TAG("ThreadID", boost::this_thread::get_id());
int main(int, char*[]) {
  src::severity_logger<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";
  return 0;
I created sink1 & sink2 in above example. sink1 outputs sign.log file and sink2 creates sign.csv file.
All logs whose severity is greater than or equal to warning will appear in sign.log file, all logs less than warning will be recorded into sign.csv file with csv format.
Run this example and watch the result of two files:

$ cat sign.csv
2013-12-25 16:31:27,trace,A trace severity message
2013-12-25 16:31:27,debug,A debug severity message
2013-12-25 16:31:27,info,An informational severity message 
$ cat sign.log
[2013-12-25 16:31:27]<warning>(0x00007f64b5cf3740): A warning severity message
[2013-12-25 16:31:27]<error>(0x00007f64b5cf3740): An error severity message
[2013-12-25 16:31:27]<fatal>(0x00007f64b5cf3740): A fatal severity message

No comments: