]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/log/example/doc/sinks_async_ordering.cpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / log / example / doc / sinks_async_ordering.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 <functional>
12 #include <boost/smart_ptr/shared_ptr.hpp>
13 #include <boost/core/null_deleter.hpp>
14 #include <boost/date_time/posix_time/posix_time_types.hpp>
15 #include <boost/log/core.hpp>
16 #include <boost/log/expressions.hpp>
17 #include <boost/log/sinks/async_frontend.hpp>
18 #include <boost/log/sinks/unbounded_ordering_queue.hpp>
19 #include <boost/log/sinks/text_ostream_backend.hpp>
20 #include <boost/log/sources/severity_channel_logger.hpp>
21 #include <boost/log/sources/record_ostream.hpp>
22 #include <boost/log/utility/record_ordering.hpp>
23 #include <boost/log/utility/setup/common_attributes.hpp>
24
25 namespace logging = boost::log;
26 namespace src = boost::log::sources;
27 namespace expr = boost::log::expressions;
28 namespace sinks = boost::log::sinks;
29 namespace keywords = boost::log::keywords;
30
31 enum severity_level
32 {
33 normal,
34 warning,
35 error
36 };
37
38 //[ example_sinks_ordering_async_init
39 // Complete sink type
40 typedef sinks::asynchronous_sink<
41 sinks::text_ostream_backend,
42 sinks::unbounded_ordering_queue< /*< log record queueing strategy >*/
43 logging::attribute_value_ordering< /*< log record ordering predicate type >*/
44 unsigned int, /*< attribute value type >*/
45 std::less< unsigned int > /*< optional, attribute value comparison predicate; `std::less` equivalent is used by default >*/
46 >
47 >
48 > sink_t;
49
50 boost::shared_ptr< sink_t > init_logging()
51 {
52 boost::shared_ptr< logging::core > core = logging::core::get();
53
54 // Create a backend and initialize it with a stream
55 boost::shared_ptr< sinks::text_ostream_backend > backend =
56 boost::make_shared< sinks::text_ostream_backend >();
57 backend->add_stream(
58 boost::shared_ptr< std::ostream >(&std::clog, boost::null_deleter()));
59
60 // Wrap it into the frontend and register in the core
61 boost::shared_ptr< sink_t > sink(new sink_t(
62 backend, /*< pointer to the pre-initialized backend >*/
63 keywords::order =
64 logging::make_attr_ordering("LineID", std::less< unsigned int >()), /*< log record ordering predicate >*/
65 keywords::ordering_window = boost::posix_time::seconds(1) /*< latency of log record processing >*/
66 ));
67 core->add_sink(sink);
68
69 // You can manage filtering and formatting through the sink interface
70 sink->set_filter(expr::attr< severity_level >("Severity") >= warning);
71 sink->set_formatter
72 (
73 expr::stream
74 << "Level: " << expr::attr< severity_level >("Severity")
75 << " Message: " << expr::smessage
76 );
77
78 // You can also manage backend in a thread-safe manner
79 {
80 sink_t::locked_backend_ptr p = sink->locked_backend();
81 p->add_stream(boost::make_shared< std::ofstream >("sample.log"));
82 } // the backend gets released here
83
84 return sink;
85 }
86 //]
87
88 //[ example_sinks_ordering_async_stop
89 void stop_logging(boost::shared_ptr< sink_t >& sink)
90 {
91 boost::shared_ptr< logging::core > core = logging::core::get();
92
93 // Remove the sink from the core, so that no records are passed to it
94 core->remove_sink(sink);
95
96 // Break the feeding loop
97 sink->stop();
98
99 // Flush all log records that may have left buffered
100 sink->flush();
101
102 sink.reset();
103 }
104 //]
105
106 int main(int, char*[])
107 {
108 boost::shared_ptr< sink_t > sink = init_logging();
109 logging::add_common_attributes();
110
111 src::severity_channel_logger< severity_level > lg(keywords::channel = "net");
112 BOOST_LOG_SEV(lg, warning) << "Hello world!";
113
114 stop_logging(sink);
115
116 return 0;
117 }