实时提升日志记录

Boost logging in real time

我使用 boost log 将日志输出到两个单独的文件中,问题是我需要立即写入这些输出,但 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();
}

您可以按照此处所述为您的后端启用 auto-flushing:http://boost-log.sourceforge.net/libs/log/doc/html/log/detailed/sink_backends.html

按照@Andy T 的建议启用自动刷新是实现此目的的好方法,在将接收器添加到核心之前,我们需要启用auto-flushing。

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