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