1 /* 2 * Copyright Andrey Semashev 2007 - 2015. 3 * Distributed under the Boost Software License, Version 1.0. 4 * (See accompanying file LICENSE_1_0.txt or copy at 5 * http://www.boost.org/LICENSE_1_0.txt) 6 */ 7 8 #include <fstream> 9 #include <boost/smart_ptr/shared_ptr.hpp> 10 #include <boost/smart_ptr/make_shared_object.hpp> 11 #include <boost/log/core.hpp> 12 #include <boost/log/trivial.hpp> 13 #include <boost/log/expressions.hpp> 14 #include <boost/log/sinks/sync_frontend.hpp> 15 #include <boost/log/sinks/text_ostream_backend.hpp> 16 #include <boost/log/sources/severity_logger.hpp> 17 #include <boost/log/sources/record_ostream.hpp> 18 #include <boost/log/utility/setup/common_attributes.hpp> 19 20 namespace logging = boost::log; 21 namespace src = boost::log::sources; 22 namespace expr = boost::log::expressions; 23 namespace sinks = boost::log::sinks; 24 namespace keywords = boost::log::keywords; 25 26 //[ example_tutorial_formatters_format init()27void init() 28 { 29 typedef sinks::synchronous_sink< sinks::text_ostream_backend > text_sink; 30 boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >(); 31 32 sink->locked_backend()->add_stream( 33 boost::make_shared< std::ofstream >("sample.log")); 34 35 // This makes the sink to write log records that look like this: 36 // 1: <normal> A normal severity message 37 // 2: <error> An error severity message 38 sink->set_formatter 39 ( 40 expr::format("%1%: <%2%> %3%") 41 % expr::attr< unsigned int >("LineID") 42 % logging::trivial::severity 43 % expr::smessage 44 ); 45 46 logging::core::get()->add_sink(sink); 47 } 48 //] 49 main(int,char * [])50int main(int, char*[]) 51 { 52 init(); 53 logging::add_common_attributes(); 54 55 using namespace logging::trivial; 56 src::severity_logger< severity_level > lg; 57 58 BOOST_LOG_SEV(lg, trace) << "A trace severity message"; 59 BOOST_LOG_SEV(lg, debug) << "A debug severity message"; 60 BOOST_LOG_SEV(lg, info) << "An informational severity message"; 61 BOOST_LOG_SEV(lg, warning) << "A warning severity message"; 62 BOOST_LOG_SEV(lg, error) << "An error severity message"; 63 BOOST_LOG_SEV(lg, fatal) << "A fatal severity message"; 64 65 return 0; 66 } 67