每个类的不同的提升日志接收器 [英] Different boost log sinks for every class
本文介绍了每个类的不同的提升日志接收器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是新的提升日志。
我的算法有4个主要步骤,我想在一个文件中记录每一步。所以我有4个水槽。我的想法是,我可以改变水槽的每一步。这是可能吗?
目前我有一个全局记录器 looger.h
b
$ b
#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 /源/ severity_logger.hpp>
#include< boost / log /源/ severity_feature.hpp>
#include< fstream>
命名空间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& )%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_ * /
解决方案
div>
您只需要更改接收器的流:
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)<< 这将去log_1;
BOOST_LOG_SEV(log,error)<< 这一个;
manager.set_log_file(log_2.txt);
BOOST_LOG_SEV(log,error)<< 这将转到log_2;
manager.set_log_file(log_3.txt);
BOOST_LOG_SEV(log,error)<< 这将去log_3;
结果:
code>> cat log_1.txt
[...]< 4>(0x00007fe742977780):这将转到log_1
[...] 4(0x00007fe742977780):这一个
> cat log_2.txt
[...]< 4>(0x00007fe742977780):这将转到log_2
> cat log_3.txt
[...]< 4>(0x00007fe742977780):这将转到log_3
I am new to boost log.
My algorithm has 4 main steps and I want the logging for every step in one file. So that I have 4 sinks. My idea is that I can change the sink on every step. Is that possible?
At the moment I have my
looger.h
with a global logger#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_ */
Is it possible to change the sink after every step?
解决方案You need to change only stream of a sink:
void set_log_file(const char* filename) { backend_->remove_stream(current_stream_); current_stream_.reset(new std::ofstream(filename)); backend_->add_stream(current_stream_); }
Usage:
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";
Result:
> 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
- Full example: http://pastebin.com/e3qDQ8tT
这篇关于每个类的不同的提升日志接收器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文