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 <string> 9 #include <fstream> 10 #include <iostream> 11 #include <boost/smart_ptr/shared_ptr.hpp> 12 #include <boost/core/null_deleter.hpp> 13 #include <boost/log/core.hpp> 14 #include <boost/log/sinks/sync_frontend.hpp> 15 #include <boost/log/sinks/text_ostream_backend.hpp> 16 #include <boost/log/sources/severity_channel_logger.hpp> 17 #include <boost/log/sources/record_ostream.hpp> 18 19 namespace logging = boost::log; 20 namespace src = boost::log::sources; 21 namespace sinks = boost::log::sinks; 22 namespace keywords = boost::log::keywords; 23 24 //[ example_sinks_ostream init_logging()25void init_logging() 26 { 27 boost::shared_ptr< logging::core > core = logging::core::get(); 28 29 // Create a backend and attach a couple of streams to it 30 boost::shared_ptr< sinks::text_ostream_backend > backend = 31 boost::make_shared< sinks::text_ostream_backend >(); 32 backend->add_stream( 33 boost::shared_ptr< std::ostream >(&std::clog, boost::null_deleter())); 34 backend->add_stream( 35 boost::shared_ptr< std::ostream >(new std::ofstream("sample.log"))); 36 37 // Enable auto-flushing after each log record written 38 backend->auto_flush(true); 39 40 // Wrap it into the frontend and register in the core. 41 // The backend requires synchronization in the frontend. 42 typedef sinks::synchronous_sink< sinks::text_ostream_backend > sink_t; 43 boost::shared_ptr< sink_t > sink(new sink_t(backend)); 44 core->add_sink(sink); 45 } 46 //] 47 main(int,char * [])48int main(int, char*[]) 49 { 50 init_logging(); 51 52 src::severity_channel_logger< > lg(keywords::channel = "net"); 53 BOOST_LOG_SEV(lg, 3) << "Hello world!"; 54 55 return 0; 56 } 57