Chris@16: /* Chris@101: * Copyright Andrey Semashev 2007 - 2015. Chris@16: * Distributed under the Boost Software License, Version 1.0. Chris@16: * (See accompanying file LICENSE_1_0.txt or copy at Chris@16: * http://www.boost.org/LICENSE_1_0.txt) Chris@16: */ Chris@16: /*! Chris@16: * \file syslog_backend.hpp Chris@16: * \author Andrey Semashev Chris@16: * \date 08.01.2008 Chris@16: * Chris@16: * The header contains implementation of a Syslog sink backend along with its setup facilities. Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_LOG_SINKS_SYSLOG_BACKEND_HPP_INCLUDED_ Chris@16: #define BOOST_LOG_SINKS_SYSLOG_BACKEND_HPP_INCLUDED_ Chris@16: Chris@16: #include Chris@16: Chris@16: #ifdef BOOST_HAS_PRAGMA_ONCE Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: #ifndef BOOST_LOG_WITHOUT_SYSLOG Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: BOOST_LOG_OPEN_NAMESPACE Chris@16: Chris@16: namespace sinks { Chris@16: Chris@16: //! Supported IP protocol versions Chris@16: enum ip_versions Chris@16: { Chris@16: v4, Chris@16: v6 Chris@16: }; Chris@16: Chris@16: namespace syslog { Chris@16: Chris@16: //! The enumeration defined the possible implementation types for the syslog backend Chris@16: enum impl_types Chris@16: { Chris@16: #ifdef BOOST_LOG_USE_NATIVE_SYSLOG Chris@16: native = 0 //!< Use native syslog API Chris@16: #ifndef BOOST_LOG_NO_ASIO Chris@16: , Chris@16: #endif Chris@16: #endif Chris@16: #ifndef BOOST_LOG_NO_ASIO Chris@16: udp_socket_based = 1 //!< Use UDP sockets, according to RFC3164 Chris@16: #endif Chris@16: }; Chris@16: Chris@16: /*! Chris@16: * \brief Straightforward severity level mapping Chris@16: * Chris@16: * This type of mapping assumes that attribute with a particular name always Chris@16: * provides values that map directly onto the Syslog levels. The mapping Chris@16: * simply returns the extracted attribute value converted to the Syslog severity level. Chris@16: */ Chris@16: template< typename AttributeValueT = int > Chris@16: class direct_severity_mapping : Chris@16: public basic_direct_mapping< level, AttributeValueT > Chris@16: { Chris@16: //! Base type Chris@16: typedef basic_direct_mapping< level, AttributeValueT > base_type; Chris@16: Chris@16: public: Chris@16: /*! Chris@16: * Constructor Chris@16: * Chris@16: * \param name Attribute name Chris@16: */ Chris@16: explicit direct_severity_mapping(attribute_name const& name) : Chris@16: base_type(name, info) Chris@16: { Chris@16: } Chris@16: }; Chris@16: Chris@16: /*! Chris@16: * \brief Customizable severity level mapping Chris@16: * Chris@16: * The class allows to setup a custom mapping between an attribute and Syslog severity levels. Chris@16: * The mapping should be initialized similarly to the standard \c map container, by using Chris@16: * indexing operator and assignment. Chris@16: */ Chris@16: template< typename AttributeValueT = int > Chris@16: class custom_severity_mapping : Chris@16: public basic_custom_mapping< level, AttributeValueT > Chris@16: { Chris@16: //! Base type Chris@16: typedef basic_custom_mapping< level, AttributeValueT > base_type; Chris@16: Chris@16: public: Chris@16: /*! Chris@16: * Constructor Chris@16: * Chris@16: * \param name Attribute name Chris@16: */ Chris@16: explicit custom_severity_mapping(attribute_name const& name) : Chris@16: base_type(name, info) Chris@16: { Chris@16: } Chris@16: }; Chris@16: Chris@16: } // namespace syslog Chris@16: Chris@16: /*! Chris@16: * \brief An implementation of a syslog sink backend Chris@16: * Chris@16: * The backend provides support for the syslog protocol, defined in RFC3164. Chris@16: * The backend sends log records to a remote host via UDP. The host name can Chris@16: * be specified by calling the \c set_target_address method. By default log Chris@16: * records will be sent to localhost:514. The local address can be specified Chris@16: * as well, by calling the \c set_local_address method. By default syslog Chris@16: * packets will be sent from any local address available. Chris@16: * Chris@16: * It is safe to create several sink backends with the same local addresses - Chris@16: * the backends within the process will share the same socket. The same applies Chris@16: * to different processes that use the syslog backends to send records from Chris@16: * the same socket. However, it is not guaranteed to work if some third party Chris@16: * facility is using the socket. Chris@16: * Chris@16: * On systems with native syslog implementation it may be preferable to utilize Chris@16: * the POSIX syslog API instead of direct socket management in order to bypass Chris@16: * possible security limitations that may be in action. To do so one has to pass Chris@16: * the use_impl = native to the backend constructor. Note, however, Chris@16: * that in that case you will only have one chance to specify syslog facility and Chris@16: * process identification string - on the first native syslog backend construction. Chris@16: * Other native syslog backends will ignore these parameters. Chris@16: * Obviously, the \c set_local_address and \c set_target_address Chris@16: * methods have no effect for native backends. Using use_impl = native Chris@16: * on platforms with no native support for POSIX syslog API will have no effect. Chris@16: */ Chris@16: class syslog_backend : Chris@16: public basic_formatted_sink_backend< char > Chris@16: { Chris@16: //! Base type Chris@16: typedef basic_formatted_sink_backend< char > base_type; Chris@16: //! Implementation type Chris@16: struct implementation; Chris@16: Chris@16: public: Chris@16: //! Character type Chris@16: typedef base_type::char_type char_type; Chris@16: //! String type that is used to pass message test Chris@16: typedef base_type::string_type string_type; Chris@16: Chris@16: //! Syslog severity level mapper type Chris@16: typedef boost::log::aux::light_function< syslog::level (record_view const&) > severity_mapper_type; Chris@16: Chris@16: private: Chris@16: //! Pointer to the implementation Chris@16: implementation* m_pImpl; Chris@16: Chris@16: public: Chris@16: /*! Chris@16: * Constructor. Creates a UDP socket-based backend with syslog::user facility code. Chris@16: * IPv4 protocol will be used. Chris@16: */ Chris@16: BOOST_LOG_API syslog_backend(); Chris@16: /*! Chris@16: * Constructor. Creates a sink backend with the specified named parameters. Chris@16: * The following named parameters are supported: Chris@16: * Chris@16: * \li \c facility - Specifies the facility code. If not specified, syslog::user will be used. Chris@16: * \li \c use_impl - Specifies the backend implementation. Can be one of: Chris@16: * \li \c native - Use the native syslog API, if available. If no native API Chris@16: * is available, it is equivalent to \c udp_socket_based. Chris@16: * \li \c udp_socket_based - Use the UDP socket-based implementation, conforming to Chris@16: * RFC3164 protocol specification. This is the default. Chris@16: * \li \c ip_version - Specifies IP protocol version to use, in case if socket-based implementation Chris@16: * is used. Can be either \c v4 (the default one) or \c v6. Chris@16: * \li \c ident - Process identification string. This parameter is only supported by native syslog implementation. Chris@16: */ Chris@16: #ifndef BOOST_LOG_DOXYGEN_PASS Chris@16: BOOST_LOG_PARAMETRIZED_CONSTRUCTORS_CALL(syslog_backend, construct) Chris@16: #else Chris@16: template< typename... ArgsT > Chris@16: explicit syslog_backend(ArgsT... const& args); Chris@16: #endif Chris@16: Chris@16: /*! Chris@16: * Destructor Chris@16: */ Chris@16: BOOST_LOG_API ~syslog_backend(); Chris@16: Chris@16: /*! Chris@16: * The method installs the function object that maps application severity levels to syslog levels Chris@16: */ Chris@16: BOOST_LOG_API void set_severity_mapper(severity_mapper_type const& mapper); Chris@16: Chris@16: #if !defined(BOOST_LOG_NO_ASIO) Chris@16: Chris@16: /*! Chris@16: * The method sets the local host name which log records will be sent from. The host name Chris@16: * is resolved to obtain the final IP address. Chris@16: * Chris@16: * \note Does not have effect if the backend was constructed to use native syslog API Chris@16: * Chris@16: * \param addr The local address Chris@16: * \param port The local port number Chris@16: */ Chris@16: BOOST_LOG_API void set_local_address(std::string const& addr, unsigned short port = 514); Chris@16: /*! Chris@16: * The method sets the local address which log records will be sent from. Chris@16: * Chris@16: * \note Does not have effect if the backend was constructed to use native syslog API Chris@16: * Chris@16: * \param addr The local address Chris@16: * \param port The local port number Chris@16: */ Chris@16: BOOST_LOG_API void set_local_address(boost::asio::ip::address const& addr, unsigned short port = 514); Chris@16: Chris@16: /*! Chris@16: * The method sets the remote host name where log records will be sent to. The host name Chris@16: * is resolved to obtain the final IP address. Chris@16: * Chris@16: * \note Does not have effect if the backend was constructed to use native syslog API Chris@16: * Chris@16: * \param addr The remote host address Chris@16: * \param port The port number on the remote host Chris@16: */ Chris@16: BOOST_LOG_API void set_target_address(std::string const& addr, unsigned short port = 514); Chris@16: /*! Chris@16: * The method sets the address of the remote host where log records will be sent to. Chris@16: * Chris@16: * \note Does not have effect if the backend was constructed to use native syslog API Chris@16: * Chris@16: * \param addr The remote host address Chris@16: * \param port The port number on the remote host Chris@16: */ Chris@16: BOOST_LOG_API void set_target_address(boost::asio::ip::address const& addr, unsigned short port = 514); Chris@16: Chris@16: #endif // !defined(BOOST_LOG_NO_ASIO) Chris@16: Chris@16: /*! Chris@16: * The method passes the formatted message to the syslog API or sends to a syslog server Chris@16: */ Chris@16: BOOST_LOG_API void consume(record_view const& rec, string_type const& formatted_message); Chris@16: Chris@16: private: Chris@16: #ifndef BOOST_LOG_DOXYGEN_PASS Chris@16: //! The method creates the backend implementation Chris@16: template< typename ArgsT > Chris@16: void construct(ArgsT const& args) Chris@16: { Chris@16: construct( Chris@16: args[keywords::facility | syslog::user], Chris@16: #if !defined(BOOST_LOG_NO_ASIO) Chris@16: args[keywords::use_impl | syslog::udp_socket_based], Chris@16: #else Chris@16: args[keywords::use_impl | syslog::native], Chris@16: #endif Chris@16: args[keywords::ip_version | v4], Chris@16: args[keywords::ident | std::string()]); Chris@16: } Chris@16: BOOST_LOG_API void construct( Chris@16: syslog::facility facility, syslog::impl_types use_impl, ip_versions ip_version, std::string const& ident); Chris@16: #endif // BOOST_LOG_DOXYGEN_PASS Chris@16: }; Chris@16: Chris@16: } // namespace sinks Chris@16: Chris@16: BOOST_LOG_CLOSE_NAMESPACE // namespace log Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_LOG_WITHOUT_SYSLOG Chris@16: Chris@16: #endif // BOOST_LOG_SINKS_SYSLOG_BACKEND_HPP_INCLUDED_