1+ #ifndef MY_MANAGER_H
2+ #define MY_MANAGER_H
3+
4+
5+ #include < boost/log/attributes.hpp>
6+ #include < boost/log/common.hpp>
7+ #include < boost/log/core.hpp>
8+ #include < boost/log/expressions.hpp>
9+ #include < boost/log/sinks.hpp>
10+ #include < boost/log/sinks/sync_frontend.hpp>
11+ #include < boost/log/sinks/text_ostream_backend.hpp>
12+ #include < boost/log/sources/logger.hpp>
13+ #include < boost/log/sources/record_ostream.hpp>
14+ #include < boost/log/sources/severity_feature.hpp>
15+ #include < boost/log/sources/severity_feature.hpp>
16+ #include < boost/log/sources/severity_logger.hpp>
17+ #include < boost/log/sources/severity_logger.hpp>
18+ #include < boost/log/support/date_time.hpp>
19+ #include < boost/log/utility/setup/common_attributes.hpp>
20+ #include < boost/log/utility/setup/common_attributes.hpp>
21+ #include < boost/log/utility/setup/formatter_parser.hpp>
22+ #include < boost/make_shared.hpp>
23+ #include < boost/shared_ptr.hpp>
24+ #include < boost/thread/thread.hpp>
25+ #include < fstream>
26+ #include < boost/core/null_deleter.hpp>
27+ #include < boost/log/trivial.hpp>
28+ namespace logging = boost::log;
29+ namespace attrs = boost::log::attributes;
30+ namespace src = boost::log::sources;
31+ namespace sinks = boost::log::sinks;
32+ namespace expr = boost::log::expressions;
33+ namespace keywords = boost::log::keywords;
34+
35+ enum sign_severity_level
36+ {
37+ trace,
38+ debug,
39+ info,
40+ warning,
41+ error,
42+ fatal,
43+ report
44+ };
45+
46+
47+ BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT (
48+ my_logger, src::severity_logger_mt<sign_severity_level>
49+ );
50+
51+
52+ class Manager
53+ {
54+ public:
55+ using Backend = sinks::text_ostream_backend;
56+ using TextSink = sinks::synchronous_sink<Backend>;
57+
58+ Manager ()
59+ {
60+ backend_ = boost::make_shared<Backend>();
61+ backend_->auto_flush (true );
62+
63+ boost::shared_ptr<TextSink> sink (new TextSink (backend_));
64+ sink->set_formatter (
65+ expr::format (" [%1%]<%2%>: %3%" ) %
66+ // expr::format("[%1%]<%2%>(%3%): %4%") %
67+ expr::format_date_time<boost::posix_time::ptime>(" TimeStamp" , " %Y-%m-%d %H:%M:%S" ) %
68+ // logging::trivial::severity %
69+ expr::attr < sign_severity_level > (" Severity" ) %
70+ // expr::attr < attrs::current_thread_id::value_type > ("ThreadID") %
71+ expr::smessage
72+ );
73+ sink->set_filter (expr::attr<sign_severity_level>(" Severity" ) >= warning);
74+ logging::core::get ()->add_sink (sink);
75+
76+ logging::add_common_attributes ();
77+ logging::core::get ()->add_global_attribute (
78+ " ThreadID" , attrs::current_thread_id ()
79+ );
80+ }
81+
82+ void set_log_file (const char * filename)
83+ {
84+ backend_->remove_stream (current_stream_);
85+ current_stream_.reset (new std::ofstream (filename));
86+ backend_->add_stream (current_stream_);
87+ // stdout
88+ backend_->add_stream (
89+ boost::shared_ptr< std::ostream >(&std::clog, boost::null_deleter ()));
90+ }
91+
92+ private:
93+ boost::shared_ptr<Backend> backend_;
94+ boost::shared_ptr<std::ostream> current_stream_;
95+ };
96+
97+
98+ #endif
0 commit comments