实时增强日志记录

时间:2016-01-04 15:41:02

标签: c++ boost boost-logging

我使用boost日志将我的日志输出到两个单独的文件中,问题是我需要立即写入这些输出,但是boost等待200多行将其输出到文件中。我不想拥有超快的输出,但是一秒或每秒两次可能会很棒。

有没有办法管理这个时间或每次写入之间的行数?

我的h(BoostLogging.h):

#ifndef AUDIO_RECO_MODULES_COMMON_BOOSTLOGGING_H_
#define AUDIO_RECO_MODULES_COMMON_BOOSTLOGGING_H_

#include <cstddef>
#include <string>
#include <ostream>
#include <fstream>
#include <iomanip>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/smart_ptr/make_shared_object.hpp>
#include <boost/phoenix/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/log/core.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/attributes.hpp>
#include <boost/log/sources/basic_logger.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/severity_channel_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/attributes/scoped_attribute.hpp>
#include <boost/log/utility/value_ref.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>

namespace logging = boost::log;
namespace src = boost::log::sources;
namespace expr = boost::log::expressions;
namespace sinks = boost::log::sinks;
namespace attrs = boost::log::attributes;
namespace keywords = boost::log::keywords;

enum severity_level {
  TRACE,
  DEBUG,
  INFO,
  WARNING,
  ERROR,
  FATAL,
  NODE
};

BOOST_LOG_ATTRIBUTE_KEYWORD(line_id, "LineID", unsigned int)
BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", severity_level)
BOOST_LOG_ATTRIBUTE_KEYWORD(tag_attr, "Tag", std::string)

std::ostream& operator<<(std::ostream& strm, severity_level level);
void InitBoostLog(severity_level logging_level, std::string module_name);

#endif //AUDIO_RECO_MODULES_COMMON_BOOSTLOGGING_H_

来源(BoostLogging.cc):

#include <BoostLogging.h>

std::ostream& operator<<(std::ostream& strm, severity_level level) {
  static const char* strings[] = {
      "trace",
      "debug",
      "info",
      "warning",
      "error",
      "fatal",
      "node"
  };

  if (static_cast< std::size_t >(level) < sizeof(strings) / sizeof(*strings))
    strm << strings[level];
  else
    strm << static_cast< int >(level);

  return strm;
}

void InitBoostLog(severity_level logging_level, std::string module_name) {
  // Setup the common formatter for all sinks
  logging::formatter fmt = expr::stream
      << std::setw(6) << std::setfill('0') << line_id << std::setfill(' ')
      << ": <" << severity << ">\t"
      << expr::if_(expr::has_attr(tag_attr))
  [
      expr::stream << "[" << tag_attr << "] "
  ]
      << expr::smessage;

  // Initialize sinks
  boost::shared_ptr<logging::core> core = logging::core::get();
  typedef sinks::synchronous_sink <sinks::text_ostream_backend> text_sink;
  boost::shared_ptr<text_sink> sink = boost::make_shared<text_sink>();

  sink->locked_backend()->add_stream(
      boost::make_shared<std::ofstream>("arm-" + module_name + ".log"));
  sink->set_formatter(fmt);
  sink->set_filter(severity != NODE && severity >= logging_level);
  core->add_sink(sink);

  sink = boost::make_shared<text_sink>();
  sink->locked_backend()->add_stream(
      boost::make_shared<std::ofstream>("arm-" + module_name + "-node.log"));
  sink->set_filter(severity == NODE);
  core->add_sink(sink);

  // Add attributes
  logging::add_common_attributes();
}

2 个答案:

答案 0 :(得分:1)

您可以按照此处所述为后端启用自动刷新功能:http://boost-log.sourceforge.net/libs/log/doc/html/log/detailed/sink_backends.html

答案 1 :(得分:0)

启用@Andy T建议的自动刷新是一种很好的方法,在将接收器添加到核心之前我们需要启用自动刷新。

sink->locked_backend()->add_stream(
    boost::make_shared<std::ofstream>("arm-" + module_name + ".log"));
sink->set_formatter(fmt);
sink->set_filter(severity != NODE && severity >= logging_level);
sink->locked_backend()->auto_flush(true); // HERE
core->add_sink(sink);

sink = boost::make_shared<text_sink>();
sink->locked_backend()->add_stream(
    boost::make_shared<std::ofstream>("arm-" + module_name + "-node.log"));
sink->set_filter(severity == NODE);
sink->locked_backend()->auto_flush(true); // AND HERE
core->add_sink(sink);