There are still something need to be improved in the demo of step 9.
First, I move all statements related to boost log into logger.h. This logger.h could be included in any app which wants to use boost log library. Just need to modify the code of InitLog function if you want different sinks.#include <boost/shared_ptr.hpp>Second, prevent app from crashing
#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")));
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);
// init sink2
boost::shared_ptr<sinks::text_ostream_backend> backend2 = boost::make_shared<sinks::text_ostream_backend>();
backend2->auto_flush(true);
backend2->add_stream(boost::shared_ptr<std::ostream>(new std::ofstream("sign.csv")));
boost::shared_ptr<TextSink> sink2(new TextSink(backend2));
sink2->set_formatter (
expr::format("%1%,%2%,%3%")
% expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S")
% expr::attr<sign_severity_level>("Severity")
% expr::smessage
);
sink2->set_filter(expr::attr<sign_severity_level>("Severity") == report);
logging::core::get()->add_sink(sink2);
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>)
See the official article:http://www.boost.org/doc/libs/1_55_0/libs/log/doc/html/log/rationale/why_crash_on_term.html
I use 2nd solution, add one line in main function before return statement.
#include "logger.h"Next time, I will apply rotation log to both sink objects.
int main(int, char*[]) {
InitLog();
src::severity_logger_mt<sign_severity_level>& lg = my_logger::get();
BOOST_LOG_SEV(lg, trace) << "A trace severity message";
BOOST_LOG_SEV(lg, debug) << "A debug severity message";
BOOST_LOG_SEV(lg, info) << "An informational severity message";
BOOST_LOG_SEV(lg, warning) << "A warning severity message";
BOOST_LOG_SEV(lg, error) << "An error severity message";
BOOST_LOG_SEV(lg, fatal) << "A fatal severity message";
BOOST_LOG_SEV(lg, report) << "A report severity message";
logging::core::get()->remove_all_sinks();
return 0;
}
No comments:
Post a Comment