【问题标题】:Different boost log sinks for every class每个班级都有不同的升压日志接收器
【发布时间】:2014-06-04 12:15:31
【问题描述】:

我是提升日志的新手。

我的算法有 4 个主要步骤,我希望在一个文件中记录每个步骤。所以我有4个水槽。我的想法是我可以在每一步都更换水槽。这可能吗?

目前我的looger.h 有一个全局记录器

#ifndef LOGGER_H_
#define LOGGER_H_

#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/thread/thread.hpp>
#include <boost/log/core.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/log/common.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/attributes.hpp>
#include <boost/log/sinks.hpp>
#include <boost/log/sources/logger.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/formatter_parser.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/severity_feature.hpp>
#include <fstream>

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

enum sign_severity_level {
  trace,
  debug,
  info,
  warning,
  error,
  fatal,
  report
};

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")));
    backend1->auto_flush(true);
    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);

    logging::add_common_attributes();
    logging::core::get()->add_global_attribute("ThreadID", attrs::current_thread_id());
}
BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(my_logger, src::severity_logger_mt<sign_severity_level>)


#endif /* LOGGER_H_ */ 

是否可以在每一步之后更换水槽?

【问题讨论】:

    标签: c++ logging boost boost-log


    【解决方案1】:

    您只需要更改接收器的流:

    void set_log_file(const char* filename) {
      backend_->remove_stream(current_stream_);
      current_stream_.reset(new std::ofstream(filename));
      backend_->add_stream(current_stream_);
    }
    

    用法:

    manager.set_log_file("log_1.txt");
    BOOST_LOG_SEV(log, error) << "This will go to log_1";
    BOOST_LOG_SEV(log, error) << "And this one";
    
    manager.set_log_file("log_2.txt");
    BOOST_LOG_SEV(log, error) << "This will go to log_2";
    
    manager.set_log_file("log_3.txt");
    BOOST_LOG_SEV(log, error) << "This will go to log_3";
    

    结果:

    > cat log_1.txt
    [...]<4>(0x00007fe742977780): This will go to log_1
    [...]<4>(0x00007fe742977780): And this one
    > cat log_2.txt
    [...]<4>(0x00007fe742977780): This will go to log_2
    > cat log_3.txt
    [...]<4>(0x00007fe742977780): This will go to log_3
    

    【讨论】:

    • 很好的例子,谢谢。我只需要在 typedef 中使用 Backend... 进行更改。
    • 谢谢我使用旧标准。还有一个问题,我如何以不删除现有文件的方式修改记录器。因为我有一个 main.log 和几个 step.log,所以我在 main 和 step 之间切换。但在每次切换时,它都会重置 main.log
    • 我测试了 text_file_backend,但是无法手动更改日志文件的名称
    • @Hunk 我发现你可以简单地改变ofstreamopenmode:current_stream_.reset(new std::ofstream(filename, std::ios_base::out | std::ios_base::app));
    • ofstream 的开放模式现在运行良好。使用 text_file_backend 也是可能的,但是我无法使用“set_log_file”添加或删除新文件。我在 text_file_backend 上没有找到任何类似的功能。我错过了什么吗?
    猜你喜欢
    • 2015-07-16
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 2014-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-09
    相关资源
    最近更新 更多