TuttleOFX  1
formatters.hpp
Go to the documentation of this file.
00001 #ifndef _COMMON_FORMATTERS_HPP_
00002 #define _COMMON_FORMATTERS_HPP_
00003 
00004 #include "color.hpp"
00005 
00006 #include <fstream>
00007 #include <iomanip>
00008 
00009 #include <boost/utility.hpp>
00010 #include <boost/shared_ptr.hpp>
00011 #include <boost/make_shared.hpp>
00012 
00013 
00014 #include <boost/log/core.hpp>
00015 #include <boost/log/trivial.hpp>
00016 #include <boost/log/expressions.hpp>
00017 #include <boost/log/sinks/sync_frontend.hpp>
00018 #include <boost/log/sinks/text_ostream_backend.hpp>
00019 #include <boost/log/sources/severity_logger.hpp>
00020 #include <boost/log/sources/record_ostream.hpp>
00021 #include <boost/log/utility/setup/common_attributes.hpp>
00022 #include <boost/log/utility/empty_deleter.hpp>
00023 #include <boost/log/expressions/formatters/stream.hpp>
00024 #include <boost/log/expressions/attr.hpp>
00025 #include <boost/log/expressions/message.hpp>
00026 
00027 namespace logging = boost::log;
00028 namespace src = boost::log::sources;
00029 namespace expr = boost::log::expressions;
00030 namespace sinks = boost::log::sinks;
00031 namespace keywords = boost::log::keywords;
00032 
00033 
00034 namespace tuttle {
00035 namespace common {
00036 namespace formatters {
00037 
00038 class Formatter : boost::noncopyable
00039 {
00040         typedef sinks::synchronous_sink< sinks::text_ostream_backend > sink_t;
00041         
00042 private:
00043         Formatter( ) { }
00044         
00045 public:
00046         
00047         static boost::shared_ptr<Formatter> get();
00048         ~Formatter(){ }
00049         
00050         void init_logging()
00051         {
00052                 boost::shared_ptr< sinks::text_ostream_backend > backend = boost::make_shared< sinks::text_ostream_backend >();
00053                 backend->add_stream( boost::shared_ptr< std::ostream >( &std::clog, logging::empty_deleter() ));
00054                 //backend->add_stream( boost::shared_ptr< std::ostream >( new std::ofstream("sample.log") ) );
00055         
00056                 backend->auto_flush(true);
00057 
00058                 //sink(  new sink_t( backend ) );
00059                 
00060                 sink = boost::make_shared< sink_t >( backend );
00061                 
00062                 displayLogLevel( false );
00063                 
00064                 logging::core::get()->add_sink( sink );
00065         }
00066         
00067         void setLogLevel( const logging::trivial::severity_level level )
00068         {
00069                 logging::core::get()->set_filter( logging::trivial::severity >= level );
00070         }
00071         
00072         void displayLogLevel( bool display )
00073         {
00074                 sink->reset_formatter();
00075                 if( display )
00076                 {
00077                         sink->set_formatter (
00078                                 expr::stream
00079                                 //<< Color::get()->_yellow  << "[" + logging::trivial::severity + "]" << Color::get()->_std << " "
00080                                 << "["<< logging::trivial::severity << "]" << " "
00081                                 << expr::smessage
00082                         );
00083                 }
00084                 else
00085                 {
00086                         sink->set_formatter ( expr::stream << expr::smessage );
00087                 }
00088         }
00089 
00090 public:
00091         static boost::shared_ptr< Formatter > formatter;
00092         boost::shared_ptr< sink_t > sink;
00093 };
00094 
00095 }
00096 }
00097 }
00098 #endif