]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/log/example/doc/sinks_syslog.cpp
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)
8 #include <boost/config.hpp>
9 #if !defined(BOOST_WINDOWS)
10 #define BOOST_LOG_USE_NATIVE_SYSLOG
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>
23 namespace logging
= boost::log
;
24 namespace src
= boost::log::sources
;
25 namespace sinks
= boost::log::sinks
;
26 namespace keywords
= boost::log::keywords
;
28 //[ example_sinks_syslog
30 typedef sinks::synchronous_sink
< sinks::syslog_backend
> sink_t
;
33 #if defined(BOOST_LOG_USE_NATIVE_SYSLOG)
35 void init_native_syslog()
37 boost::shared_ptr
< logging::core
> core
= logging::core::get();
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 >*/
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"));
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
));
57 #if !defined(BOOST_LOG_NO_ASIO)
59 void init_builtin_syslog()
61 boost::shared_ptr
< logging::core
> core
= logging::core::get();
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 >*/
69 // Setup the target address and port to send syslog messages to
70 backend
->set_target_address("192.164.1.10", 514);
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
);
80 // Wrap it into the frontend and register in the core.
81 core
->add_sink(boost::make_shared
< sink_t
>(backend
));
88 int main(int, char*[])
90 #if defined(BOOST_LOG_USE_NATIVE_SYSLOG)
92 #elif !defined(BOOST_LOG_NO_ASIO)
93 init_builtin_syslog();
96 BOOST_LOG_SCOPED_THREAD_TAG("MyLevel", "warning");
97 src::severity_logger
< > lg
;
98 BOOST_LOG_SEV(lg
, 3) << "Hello world!";