]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/log/example/doc/sinks_async.cpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / log / example / doc / sinks_async.cpp
CommitLineData
7c673cae
FG
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/expressions.hpp>
15#include <boost/log/sinks/async_frontend.hpp>
16#include <boost/log/sinks/text_ostream_backend.hpp>
17#include <boost/log/sources/severity_channel_logger.hpp>
18#include <boost/log/sources/record_ostream.hpp>
19
20namespace logging = boost::log;
21namespace src = boost::log::sources;
22namespace expr = boost::log::expressions;
23namespace sinks = boost::log::sinks;
24namespace keywords = boost::log::keywords;
25
26//[ example_sinks_async_init
27enum severity_level
28{
29 normal,
30 warning,
31 error
32};
33
34// Complete sink type
35typedef sinks::asynchronous_sink< sinks::text_ostream_backend > sink_t;
36
37boost::shared_ptr< sink_t > init_logging()
38{
39 boost::shared_ptr< logging::core > core = logging::core::get();
40
41 // Create a backend and initialize it with a stream
42 boost::shared_ptr< sinks::text_ostream_backend > backend =
43 boost::make_shared< sinks::text_ostream_backend >();
44 backend->add_stream(
45 boost::shared_ptr< std::ostream >(&std::clog, boost::null_deleter()));
46
47 // Wrap it into the frontend and register in the core
48 boost::shared_ptr< sink_t > sink(new sink_t(backend));
49 core->add_sink(sink);
50
51 // You can manage filtering and formatting through the sink interface
52 sink->set_filter(expr::attr< severity_level >("Severity") >= warning);
53 sink->set_formatter
54 (
55 expr::stream
56 << "Level: " << expr::attr< severity_level >("Severity")
57 << " Message: " << expr::message
58 );
59
60 // You can also manage backend in a thread-safe manner
61 {
62 sink_t::locked_backend_ptr p = sink->locked_backend();
63 p->add_stream(boost::make_shared< std::ofstream >("sample.log"));
64 } // the backend gets released here
65
66 return sink;
67}
68//]
69
70//[ example_sinks_async_stop
71void stop_logging(boost::shared_ptr< sink_t >& sink)
72{
73 boost::shared_ptr< logging::core > core = logging::core::get();
74
75 // Remove the sink from the core, so that no records are passed to it
76 core->remove_sink(sink);
77
78 // Break the feeding loop
79 sink->stop();
80
81 // Flush all log records that may have left buffered
82 sink->flush();
83
84 sink.reset();
85}
86//]
87
88int main(int, char*[])
89{
90 boost::shared_ptr< sink_t > sink = init_logging();
91
92 src::severity_channel_logger< severity_level > lg(keywords::channel = "net");
93 BOOST_LOG_SEV(lg, warning) << "Hello world!";
94
95 stop_logging(sink);
96
97 return 0;
98}