]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/log/example/doc/sinks_syslog.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / log / example / doc / sinks_syslog.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 <boost/config.hpp>
9 #if !defined(BOOST_WINDOWS)
10 #define BOOST_LOG_USE_NATIVE_SYSLOG
11 #endif
12
13 #include <string>
14 #include <boost/smart_ptr/shared_ptr.hpp>
15 #include <boost/smart_ptr/make_shared_object.hpp>
16 #include <boost/log/core.hpp>
17 #include <boost/log/sinks/sync_frontend.hpp>
18 #include <boost/log/sinks/syslog_backend.hpp>
19 #include <boost/log/sources/severity_logger.hpp>
20 #include <boost/log/sources/record_ostream.hpp>
21 #include <boost/log/attributes/scoped_attribute.hpp>
22
23 namespace logging = boost::log;
24 namespace src = boost::log::sources;
25 namespace sinks = boost::log::sinks;
26 namespace keywords = boost::log::keywords;
27
28 //[ example_sinks_syslog
29 // Complete sink type
30 typedef sinks::synchronous_sink< sinks::syslog_backend > sink_t;
31
32 //<-
33 #if defined(BOOST_LOG_USE_NATIVE_SYSLOG)
34 //->
35 void init_native_syslog()
36 {
37 boost::shared_ptr< logging::core > core = logging::core::get();
38
39 // Create a backend
40 boost::shared_ptr< sinks::syslog_backend > backend(new sinks::syslog_backend(
41 keywords::facility = sinks::syslog::user, /*< the logging facility >*/
42 keywords::use_impl = sinks::syslog::native /*< the native syslog API should be used >*/
43 ));
44
45 // Set the straightforward level translator for the "Severity" attribute of type int
46 backend->set_severity_mapper(sinks::syslog::direct_severity_mapping< int >("Severity"));
47
48 // Wrap it into the frontend and register in the core.
49 // The backend requires synchronization in the frontend.
50 core->add_sink(boost::make_shared< sink_t >(backend));
51 }
52 //<-
53 #endif
54 //->
55
56 //<-
57 #if !defined(BOOST_LOG_NO_ASIO)
58 //->
59 void init_builtin_syslog()
60 {
61 boost::shared_ptr< logging::core > core = logging::core::get();
62
63 // Create a new backend
64 boost::shared_ptr< sinks::syslog_backend > backend(new sinks::syslog_backend(
65 keywords::facility = sinks::syslog::local0, /*< the logging facility >*/
66 keywords::use_impl = sinks::syslog::udp_socket_based /*< the built-in socket-based implementation should be used >*/
67 ));
68
69 // Setup the target address and port to send syslog messages to
70 backend->set_target_address("192.164.1.10", 514);
71
72 // Create and fill in another level translator for "MyLevel" attribute of type string
73 sinks::syslog::custom_severity_mapping< std::string > mapping("MyLevel");
74 mapping["debug"] = sinks::syslog::debug;
75 mapping["normal"] = sinks::syslog::info;
76 mapping["warning"] = sinks::syslog::warning;
77 mapping["failure"] = sinks::syslog::critical;
78 backend->set_severity_mapper(mapping);
79
80 // Wrap it into the frontend and register in the core.
81 core->add_sink(boost::make_shared< sink_t >(backend));
82 }
83 //<-
84 #endif
85 //->
86 //]
87
88 int main(int, char*[])
89 {
90 #if defined(BOOST_LOG_USE_NATIVE_SYSLOG)
91 init_native_syslog();
92 #elif !defined(BOOST_LOG_NO_ASIO)
93 init_builtin_syslog();
94 #endif
95
96 BOOST_LOG_SCOPED_THREAD_TAG("MyLevel", "warning");
97 src::severity_logger< > lg;
98 BOOST_LOG_SEV(lg, 3) << "Hello world!";
99
100 return 0;
101 }