首页 > 其他 > 详细

boost log multifile multilog

时间:2014-02-27 02:44:32      阅读:660      评论:0      收藏:0      [点我收藏+]
 

#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/log/sources/channel_feature.hpp>
#include <boost/log/sources/channel_logger.hpp>
#include <string>
#include <boost/smart_ptr/shared_ptr.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/sinks/text_file_backend.hpp>
#include <boost/log/sources/severity_channel_logger.hpp>


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


using namespace std;


void createModuleOneSink() {
boost::shared_ptr<logging::core> core = logging::core::get();


boost::shared_ptr<sinks::text_file_backend> backend = boost::make_shared<
sinks::text_file_backend>(keywords::file_name =
"ModuleOne_%Y-%m-%d_%H-%M-%S_%N.log",
keywords::rotation_size = 5 * 1024 * 1024,
keywords::time_based_rotation = sinks::file::rotation_at_time_point(
12, 0, 0));


typedef sinks::synchronous_sink<sinks::text_file_backend> sink_t;
boost::shared_ptr<sink_t> sink(new sink_t(backend));
sink->set_formatter(
expr::stream
<< expr::format_date_time<boost::posix_time::ptime>(
"TimeStamp", "%Y-%m-%d %H:%M:%S")
<< ": <"
<< logging::trivial::severity
<<"> "
<< expr::attr<string>("Channel")
<< ":"
<< expr::smessage);
using namespace logging::trivial;
sink->set_filter(expr::attr<string>("Channel") == "module one"&&logging::trivial::severity>=warning);
core->add_sink(sink);


}


void createModuleTwoSink() {
boost::shared_ptr<logging::core> core = logging::core::get();


boost::shared_ptr<sinks::text_file_backend> backend = boost::make_shared<
sinks::text_file_backend>(keywords::file_name =
"ModuleTwo_%Y-%m-%d_%H-%M-%S_%N.log",
keywords::rotation_size = 5 * 1024 * 1024,
keywords::time_based_rotation = sinks::file::rotation_at_time_point(
12, 0, 0));


typedef sinks::synchronous_sink<sinks::text_file_backend> sink_t;
boost::shared_ptr<sink_t> sink(new sink_t(backend));
sink->set_formatter(
expr::stream
<< expr::format_date_time<boost::posix_time::ptime>(
"TimeStamp", "%Y-%m-%d %H:%M:%S")
<< ": <"
<< logging::trivial::severity
<< "> "
<< expr::attr<string>("Channel")
<<":"
<< expr::smessage);


sink->set_filter(expr::attr<string>("Channel") == "module two");
core->add_sink(sink);


}


int main(int, char*[]) {
createModuleOneSink();
createModuleTwoSink();


logging::add_common_attributes();


using namespace logging::trivial;


logging::core::get()->set_filter
   (
       logging::trivial::severity >= logging::trivial::info
   );


src::severity_channel_logger<severity_level, // the type of the severity level
std::string> moduleOneLogger(keywords::channel = "module one");
src::severity_channel_logger<severity_level, // the type of the severity level
std::string> moduleTwoLogger(keywords::channel = "module two");


BOOST_LOG_SEV(moduleOneLogger, trace)<< "A trace severity message";
BOOST_LOG_SEV(moduleOneLogger, debug)<< "A debug severity message";
BOOST_LOG_SEV(moduleOneLogger, info)<< "An informational severity message";
BOOST_LOG_SEV(moduleOneLogger, warning)<< "A warning severity message";
BOOST_LOG_SEV(moduleOneLogger, error)<< "An error severity message";
BOOST_LOG_SEV(moduleOneLogger, fatal)<< "A fatal severity message";


BOOST_LOG_SEV(moduleTwoLogger, info)<< "An informational severity message";
BOOST_LOG_SEV(moduleTwoLogger, fatal)<< "An informational severity message";
BOOST_LOG_SEV(moduleOneLogger, trace)<< "A trace severity message";


return 0;
}



2014-02-25 22:10:20: <warning> module one:A warning severity message
2014-02-25 22:10:20: <error> module one:An error severity message
2014-02-25 22:10:20: <fatal> module one:A fatal severity message


2014-02-25 22:10:20: <info> module two:An informational severity message
2014-02-25 22:10:20: <fatal> module two:An informational severity message

boost log multifile multilog,布布扣,bubuko.com

boost log multifile multilog

原文:http://blog.csdn.net/jiafu1115/article/details/19936069

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!