Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

boost log库 使用九

$
0
0

当在真正的App中使用前文的代码时,即使我等待了超过1小时,我都没有看到sign.csv文件中有日志记录。我需要一个方法在测试的时候立刻写入日志文件。如何做呢?

boost log backend的类提供了一个方法auto_flush, 通过调用它,你可以让boost log没有延迟的立刻写文件。但是这会影响性能。你需要根据你的实际情况做个决定。

我改变了前文中的部分代码。

首先,显式的创建了一个backend对象,然后基于它再创建sink对象

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);

其次,我在sink2的backend对象上设置auto_flush为true

boost::shared_ptr<sinks::text_ostream_backend> backend2 = boost::make_shared<sinks::text_ostream_backend>();
  backend2->auto_flush(true)

我已经在程序中做了测试,sink2会立刻为我写日志,而sink1会有一定的延迟。

完整的main.cc的代码在下面,logger.h代码没有改变。

#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;
  }
}




作者:sheismylife 发表于2013-12-28 0:44:40 原文链接
阅读:172 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>