#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace logging = boost::log; namespace sinks = boost::log::sinks; namespace src = boost::log::sources; namespace fmt = boost::log::formatters; namespace flt = boost::log::filters; namespace attrs = boost::log::attributes; namespace keywords = boost::log::keywords; enum severity_level { trace, debug, info, warning, error, fatal, out_of_range // just for test }; template< typename CharT, typename TraitsT > inline std::basic_ostream< CharT, TraitsT >& operator<< ( std::basic_ostream< CharT, TraitsT >& strm, severity_level lvl) { switch (lvl) { case trace: strm << "trace"; break; case debug: strm << "debug"; break; case info: strm << "info "; break; case warning: strm << "warn "; break; case error: strm << "error"; break; case fatal: strm << "fatal"; break; default: strm << "unknown log level: " << static_cast< int >(lvl); break; } return strm; } void init() { boost::shared_ptr< logging::core > core = logging::core::get(); core->add_global_attribute("TimeStamp", boost::make_shared< attrs::local_clock >()); core->add_global_attribute("ThreadId", boost::make_shared< attrs::current_thread_id >()); boost::shared_ptr< sinks::text_ostream_backend > backend(new sinks::text_ostream_backend); backend->add_stream(boost::shared_ptr< std::ostream >(&std::clog, logging::empty_deleter())); backend->auto_flush(true); backend->set_formatter( fmt::stream << fmt::date_time< boost::posix_time::ptime >("TimeStamp", keywords::format = "%Y.%m.%dT%H:%M:%S.%f") << ": [" << fmt::attr< severity_level >("Severity") << "] <" << fmt::attr< boost::thread::id >("ThreadId") << "> (" << fmt::attr< std::string >("Channel") << ") " << fmt::message() ); boost::shared_ptr< sinks::synchronous_sink< sinks::text_ostream_backend > > sink(new sinks::synchronous_sink< sinks::text_ostream_backend >(backend)); core->add_sink(sink); } int main(int, char*[]) { init(); src::severity_channel_logger lg1(keywords::channel = "FirstComponent"); BOOST_LOG_SEV(lg1, trace) << "A trace severity message"; BOOST_LOG_SEV(lg1, debug) << "A debug severity message"; BOOST_LOG_SEV(lg1, info) << "An info severity message"; BOOST_LOG_SEV(lg1, warning) << "A warning severity message"; BOOST_LOG_SEV(lg1, error) << "An error severity message"; BOOST_LOG_SEV(lg1, fatal) << "A fatal severity message"; BOOST_LOG_SEV(lg1, out_of_range) << "A message with unhandled severity"; src::severity_channel_logger lg2(keywords::channel = "SecondComponent"); BOOST_LOG_SEV(lg2, trace) << "A trace severity message"; BOOST_LOG_SEV(lg2, debug) << "A debug severity message"; BOOST_LOG_SEV(lg2, info) << "An info severity message"; BOOST_LOG_SEV(lg2, warning) << "A warning severity message"; BOOST_LOG_SEV(lg2, error) << "An error severity message"; BOOST_LOG_SEV(lg2, fatal) << "A fatal severity message"; BOOST_LOG_SEV(lg2, out_of_range) << "A message with unhandled severity"; std::cout << "The next message fails, because I use the wrong logger" << std::endl; src::channel_logger<> lg3(keywords::channel = "FirstComponent"); BOOST_LOG(lg3) << "A trace severity message"; }