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 * \file from_settings.hpp
9 * \author Andrey Semashev
10 * \date 11.10.2009
11 *
12 * The header contains definition of facilities that allows to initialize the library from
13 * settings.
14 */
15
16 #ifndef BOOST_LOG_UTILITY_SETUP_FROM_SETTINGS_HPP_INCLUDED_
17 #define BOOST_LOG_UTILITY_SETUP_FROM_SETTINGS_HPP_INCLUDED_
18
19 #include <string>
20 #include <boost/smart_ptr/shared_ptr.hpp>
21 #include <boost/core/enable_if.hpp>
22 #include <boost/type_traits/is_base_and_derived.hpp>
23 #include <boost/log/detail/setup_config.hpp>
24 #include <boost/log/sinks/sink.hpp>
25 #include <boost/log/utility/setup/settings.hpp>
26 #include <boost/log/detail/header.hpp>
27
28 #ifdef BOOST_HAS_PRAGMA_ONCE
29 #pragma once
30 #endif
31
32 namespace boost {
33
34 BOOST_LOG_OPEN_NAMESPACE
35
36 /*!
37 * The function initializes the logging library from a settings container
38 *
39 * \param setts Library settings container
40 *
41 * \b Throws: An <tt>std::exception</tt>-based exception if the provided settings are not valid.
42 */
43 template< typename CharT >
44 BOOST_LOG_SETUP_API void init_from_settings(basic_settings_section< CharT > const& setts);
45
46
47 /*!
48 * Sink factory base interface
49 */
50 template< typename CharT >
51 struct sink_factory
52 {
53 //! Character type
54 typedef CharT char_type;
55 //! String type
56 typedef std::basic_string< char_type > string_type;
57 //! Settings section type
58 typedef basic_settings_section< char_type > settings_section;
59
60 /*!
61 * Default constructor
62 */
sink_factoryboost::sink_factory63 BOOST_DEFAULTED_FUNCTION(sink_factory(), {})
64
65 /*!
66 * Virtual destructor
67 */
68 virtual ~sink_factory() {}
69
70 /*!
71 * The function creates a formatter for the specified attribute.
72 *
73 * \param settings Sink parameters
74 */
75 virtual shared_ptr< sinks::sink > create_sink(settings_section const& settings) = 0;
76
77 BOOST_DELETED_FUNCTION(sink_factory(sink_factory const&))
78 BOOST_DELETED_FUNCTION(sink_factory& operator= (sink_factory const&))
79 };
80
81 /*!
82 * \brief The function registers a factory for a custom sink
83 *
84 * The function registers a factory for a sink. The factory will be called to create sink
85 * instance when the parser discovers the specified sink type in the settings file. The
86 * factory must accept a map of parameters [parameter name -> parameter value] that it
87 * may use to initialize the sink. The factory must return a non-NULL pointer to the
88 * constructed sink instance.
89 *
90 * \param sink_name The custom sink name. Must point to a zero-terminated sequence of characters,
91 * must not be NULL.
92 * \param factory Pointer to the custom sink factory. Must not be NULL.
93 */
94 template< typename CharT >
95 BOOST_LOG_SETUP_API void register_sink_factory(const char* sink_name, shared_ptr< sink_factory< CharT > > const& factory);
96
97 /*!
98 * \brief The function registers a factory for a custom sink
99 *
100 * The function registers a factory for a sink. The factory will be called to create sink
101 * instance when the parser discovers the specified sink type in the settings file. The
102 * factory must accept a map of parameters [parameter name -> parameter value] that it
103 * may use to initialize the sink. The factory must return a non-NULL pointer to the
104 * constructed sink instance.
105 *
106 * \param sink_name The custom sink name
107 * \param factory Pointer to the custom sink factory. Must not be NULL.
108 */
109 template< typename CharT >
register_sink_factory(std::string const & sink_name,shared_ptr<sink_factory<CharT>> const & factory)110 inline void register_sink_factory(std::string const& sink_name, shared_ptr< sink_factory< CharT > > const& factory)
111 {
112 register_sink_factory(sink_name.c_str(), factory);
113 }
114
115 /*!
116 * \brief The function registers a factory for a custom sink
117 *
118 * The function registers a factory for a sink. The factory will be called to create sink
119 * instance when the parser discovers the specified sink type in the settings file. The
120 * factory must accept a map of parameters [parameter name -> parameter value] that it
121 * may use to initialize the sink. The factory must return a non-NULL pointer to the
122 * constructed sink instance.
123 *
124 * \param sink_name The custom sink name. Must point to a zero-terminated sequence of characters,
125 * must not be NULL.
126 * \param factory Pointer to the custom sink factory. Must not be NULL.
127 */
128 template< typename FactoryT >
129 inline typename boost::enable_if_c<
130 is_base_and_derived< sink_factory< typename FactoryT::char_type >, FactoryT >::value
register_sink_factory(const char * sink_name,shared_ptr<FactoryT> const & factory)131 >::type register_sink_factory(const char* sink_name, shared_ptr< FactoryT > const& factory)
132 {
133 typedef sink_factory< typename FactoryT::char_type > factory_base;
134 register_sink_factory(sink_name, boost::static_pointer_cast< factory_base >(factory));
135 }
136
137 /*!
138 * \brief The function registers a factory for a custom sink
139 *
140 * The function registers a factory for a sink. The factory will be called to create sink
141 * instance when the parser discovers the specified sink type in the settings file. The
142 * factory must accept a map of parameters [parameter name -> parameter value] that it
143 * may use to initialize the sink. The factory must return a non-NULL pointer to the
144 * constructed sink instance.
145 *
146 * \param sink_name The custom sink name
147 * \param factory Pointer to the custom sink factory. Must not be NULL.
148 */
149 template< typename FactoryT >
150 inline typename boost::enable_if_c<
151 is_base_and_derived< sink_factory< typename FactoryT::char_type >, FactoryT >::value
register_sink_factory(std::string const & sink_name,shared_ptr<FactoryT> const & factory)152 >::type register_sink_factory(std::string const& sink_name, shared_ptr< FactoryT > const& factory)
153 {
154 typedef sink_factory< typename FactoryT::char_type > factory_base;
155 register_sink_factory(sink_name.c_str(), boost::static_pointer_cast< factory_base >(factory));
156 }
157
158 BOOST_LOG_CLOSE_NAMESPACE // namespace log
159
160 } // namespace boost
161
162 #include <boost/log/detail/footer.hpp>
163
164 #endif // BOOST_LOG_UTILITY_SETUP_FROM_SETTINGS_HPP_INCLUDED_
165