from_settings.hpp 5.8 KB

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