Advertisement
Guest User

Untitled

a guest
Jun 4th, 2014
762
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.92 KB | None | 0 0
  1. #include <boost/log/attributes.hpp>
  2. #include <boost/log/common.hpp>
  3. #include <boost/log/core.hpp>
  4. #include <boost/log/expressions.hpp>
  5. #include <boost/log/sinks.hpp>
  6. #include <boost/log/sinks/sync_frontend.hpp>
  7. #include <boost/log/sinks/text_ostream_backend.hpp>
  8. #include <boost/log/sources/logger.hpp>
  9. #include <boost/log/sources/record_ostream.hpp>
  10. #include <boost/log/sources/severity_feature.hpp>
  11. #include <boost/log/sources/severity_feature.hpp>
  12. #include <boost/log/sources/severity_logger.hpp>
  13. #include <boost/log/sources/severity_logger.hpp>
  14. #include <boost/log/support/date_time.hpp>
  15. #include <boost/log/utility/setup/common_attributes.hpp>
  16. #include <boost/log/utility/setup/common_attributes.hpp>
  17. #include <boost/log/utility/setup/formatter_parser.hpp>
  18. #include <boost/make_shared.hpp>
  19. #include <boost/shared_ptr.hpp>
  20. #include <boost/thread/thread.hpp>
  21. #include <fstream>
  22.  
  23. namespace logging = boost::log;
  24. namespace attrs = boost::log::attributes;
  25. namespace src = boost::log::sources;
  26. namespace sinks = boost::log::sinks;
  27. namespace expr = boost::log::expressions;
  28. namespace keywords = boost::log::keywords;
  29.  
  30. enum sign_severity_level {
  31.   trace,
  32.   debug,
  33.   info,
  34.   warning,
  35.   error,
  36.   fatal,
  37.   report
  38. };
  39.  
  40. BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(
  41.     my_logger, src::severity_logger_mt<sign_severity_level>
  42. );
  43.  
  44. class Manager {
  45.  public:
  46.   using Backend = sinks::text_ostream_backend;
  47.   using TextSink = sinks::synchronous_sink<Backend>;
  48.  
  49.   Manager() {
  50.     backend_ = boost::make_shared<Backend>();
  51.     backend_->auto_flush(true);
  52.  
  53.     boost::shared_ptr<TextSink> sink(new TextSink(backend_));
  54.     sink->set_formatter(
  55.         expr::format("[%1%]<%2%>(%3%): %4%") %
  56.             expr::format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y-%m-%d %H:%M:%S") %
  57.             expr::attr < sign_severity_level > ("Severity") %
  58.             expr::attr < attrs::current_thread_id::value_type > ("ThreadID") %
  59.             expr::smessage
  60.     );
  61.     sink->set_filter(expr::attr<sign_severity_level>("Severity") >= warning);
  62.     logging::core::get()->add_sink(sink);
  63.  
  64.     logging::add_common_attributes();
  65.     logging::core::get()->add_global_attribute(
  66.         "ThreadID", attrs::current_thread_id()
  67.     );
  68.   }
  69.  
  70.   void set_log_file(const char* filename) {
  71.     backend_->remove_stream(current_stream_);
  72.     current_stream_.reset(new std::ofstream(filename));
  73.     backend_->add_stream(current_stream_);
  74.   }
  75.  
  76.  private:
  77.   boost::shared_ptr<Backend> backend_;
  78.   boost::shared_ptr<std::ostream> current_stream_;
  79. };
  80.  
  81. int main() {
  82.   Manager manager;
  83.   auto log = my_logger::get();
  84.  
  85.   manager.set_log_file("log_1.txt");
  86.   BOOST_LOG_SEV(log, error) << "This will go to log_1";
  87.   BOOST_LOG_SEV(log, error) << "And this one";
  88.  
  89.   manager.set_log_file("log_2.txt");
  90.   BOOST_LOG_SEV(log, error) << "This will go to log_2";
  91.  
  92.   manager.set_log_file("log_3.txt");
  93.   BOOST_LOG_SEV(log, error) << "This will go to log_3";
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement