2
0
mirror of https://github.com/boostorg/log.git synced 2026-01-26 18:42:17 +00:00
Files
log/example/doc/sinks_ostream.cpp
Andrey Semashev a7c4e0e319 Boost.Log merged to trunk.
[SVN r83860]
2013-04-13 12:30:25 +00:00

57 lines
1.8 KiB
C++

/*
* Copyright Andrey Semashev 2007 - 2013.
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*/
#include <string>
#include <fstream>
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/log/core.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/sources/severity_channel_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/utility/empty_deleter.hpp>
namespace logging = boost::log;
namespace src = boost::log::sources;
namespace sinks = boost::log::sinks;
namespace keywords = boost::log::keywords;
//[ example_sinks_ostream
void init_logging()
{
boost::shared_ptr< logging::core > core = logging::core::get();
// Create a backend and attach a couple of streams to it
boost::shared_ptr< sinks::text_ostream_backend > backend =
boost::make_shared< sinks::text_ostream_backend >();
backend->add_stream(
boost::shared_ptr< std::ostream >(&std::clog, logging::empty_deleter()));
backend->add_stream(
boost::shared_ptr< std::ostream >(new std::ofstream("sample.log")));
// Enable auto-flushing after each log record written
backend->auto_flush(true);
// Wrap it into the frontend and register in the core.
// The backend requires synchronization in the frontend.
typedef sinks::synchronous_sink< sinks::text_ostream_backend > sink_t;
boost::shared_ptr< sink_t > sink(new sink_t(backend));
core->add_sink(sink);
}
//]
int main(int, char*[])
{
init_logging();
src::severity_channel_logger< > lg(keywords::channel = "net");
BOOST_LOG_SEV(lg, 3) << "Hello world!";
return 0;
}