Friday 27 December 2013

use boost log step 9

When using the code from step 8 in my real app. I did not see any log records in sign.csv file even if I wait more than one hour. I need a way to flush the log immediately for testing. How to do this?
The boost log backend classes provide a method called auto_flush, by calling this method, you will make boost log write log for you without any latency. But it will interfere with the performance. You need to make a decision according to your actual situation.
I changed some code from step 8 below.
First, I create backend object explicitly, then create a sink object on it.
  typedef sinks::synchronous_sink<sinks::text_ostream_backend> TextSink;
 
  // init sink1
  boost::shared_ptr<sinks::text_ostream_backend> backend1 = boost::make_shared<sinks::text_ostream_backend>();
  backend1->add_stream(boost::shared_ptr<std::ostream>(new std::ofstream("sign.log")));
  boost::shared_ptr<TextSink> sink1(new TextSink(backend1));
  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
);
  sink1->set_filter(expr::attr<sign_severity_level>("Severity") >= warning);
  logging::core::get()->add_sink(sink1);
Second, I set auto_flush to true on sink2 object's backend.
  boost::shared_ptr<sinks::text_ostream_backend> backend2 = boost::make_shared<sinks::text_ostream_backend>();
  backend2->auto_flush(true)
I have tested it in my app. The sink2 writes log for me immediately now, and the sink1 has some latency.
The full code of main.cc is below, logger.h has not been modified.
#include <iostream>
#include "core/server.h"
#include "business/sign.h"
#include <booster/shared_ptr.h>
#include <boost/make_shared.hpp>
#include <boost/filesystem.hpp>
#include "util/config_error.h"
#include "util/config.h"
#include "util/my_app.h"
using namespace std;
void LoadConfig(string const& xml_path) {
  boost::filesystem::path config_file(xml_path);
  if (!boost::filesystem::exists(config_file)) {
    cout << "The configuration file path specified by paramerter doens't exit, file path:" << xml_path << endl;
    throw ConfigError("The configuration file path specified by paramerter doens't exit,file path:" + xml_path);
  }
  Configuration* config = new Configuration(xml_path);
  MyApp& app = AppHolder::Instance();
  app.set_config(config);
}
#include "util/logger.h"
/*
  void InitLog() {
  boost::log::register_simple_formatter_factory< boost::log::trivial::severity_level, char >("Severity");
  logging::add_file_log(
  keywords::file_name = AppHolder::Instance().config().log_folder + "/sign_%Y-%m-%d_%H-%M-%S.%N.log",
  keywords::rotation_size = 10 * 1024 * 1024,
  keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),
  keywords::format = "[%TimeStamp%]<%Severity%>(%ThreadID%): %Message%",
  keywords::min_free_space = 3 * 1024 * 1024);
  logging::core::get()->set_filter(logging::trivial::severity >= logging::trivial::debug);
  }*/
void InitLog() {
  typedef sinks::synchronous_sink<sinks::text_ostream_backend> TextSink;
  // init sink1
  boost::shared_ptr<sinks::text_ostream_backend> backend1 = boost::make_shared<sinks::text_ostream_backend>();
  backend1->add_stream(boost::shared_ptr<std::ostream>(new std::ofstream("sign.log")));
  boost::shared_ptr<TextSink> sink1(new TextSink(backend1));
  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
);
  sink1->set_filter(expr::attr<sign_severity_level>("Severity") >= warning);
  logging::core::get()->add_sink(sink1);
  // init sink2
  boost::shared_ptr<sinks::text_ostream_backend> backend2 = boost::make_shared<sinks::text_ostream_backend>();
  backend2->auto_flush(true);
  backend2->add_stream(boost::shared_ptr<std::ostream>(new std::ofstream("sign.csv")));
  boost::shared_ptr<TextSink> sink2(new TextSink(backend2));
  sink2->set_formatter (
expr::format("%1%,%3%")
% expr::format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y-%m-%d %H:%M:%S")
% expr::smessage
);
  sink2->set_filter(expr::attr<sign_severity_level>("Severity") == report);
  logging::core::get()->add_sink(sink2);

  logging::add_common_attributes();
  logging::core::get()->add_global_attribute("ThreadID", attrs::current_thread_id());
}

int main(int argc, char ** argv) {
  try {
    //check argument
    string xml_path;
    if (argc != 2) {
      cout << "The 1st parameter doesn't exist, data_service quits now";
      return 1;
    } else {
      xml_path = argv[1];
    }
    LoadConfig(xml_path);
    InitLog();
    logging::add_common_attributes();
    src::severity_logger_mt<sign_severity_level>& lg = my_logger::get();
    //src::severity_logger< severity_level > lg;
    BOOST_LOG_SEV(lg, info) << "thread id: " << this_thread::get_id() << " Initialization succeeded";
    io_service iosev;
    Configuration & config = AppHolder::Instance().config();
    tcp::endpoint listen_endpoint(tcp::v4(), config.listen_port);
    Server<Sign> server(iosev, listen_endpoint, config.thread_number);
    server.Run();
  } catch(std::exception const& ex) {
    cout << "thread id: " << this_thread::get_id() << " Caught an exception: " << ex.what() << endl;
  }
}

No comments:

Followers

Contributors