global_logger_storage.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2013.
  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 global_logger_storage.hpp
  9. * \author Andrey Semashev
  10. * \date 21.04.2008
  11. *
  12. * The header contains implementation of facilities to declare global loggers.
  13. */
  14. #ifndef BOOST_LOG_SOURCES_GLOBAL_LOGGER_STORAGE_HPP_INCLUDED_
  15. #define BOOST_LOG_SOURCES_GLOBAL_LOGGER_STORAGE_HPP_INCLUDED_
  16. #include <typeinfo>
  17. #include <stdexcept>
  18. #include <boost/smart_ptr/shared_ptr.hpp>
  19. #include <boost/smart_ptr/make_shared_object.hpp>
  20. #include <boost/preprocessor/seq/enum.hpp>
  21. #include <boost/log/detail/config.hpp>
  22. #include <boost/log/detail/singleton.hpp>
  23. #include <boost/log/detail/visible_type.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. namespace sources {
  31. namespace aux {
  32. //! The base class for logger holders
  33. struct BOOST_LOG_NO_VTABLE BOOST_SYMBOL_VISIBLE logger_holder_base
  34. {
  35. //! The source file name where the logger was registered
  36. const char* m_RegistrationFile;
  37. //! The line number where the logger was registered
  38. unsigned int m_RegistrationLine;
  39. logger_holder_base(const char* file, unsigned int line) :
  40. m_RegistrationFile(file),
  41. m_RegistrationLine(line)
  42. {
  43. }
  44. virtual ~logger_holder_base() {}
  45. virtual std::type_info const& logger_type() const = 0;
  46. };
  47. //! The actual logger holder class
  48. template< typename LoggerT >
  49. struct BOOST_SYMBOL_VISIBLE logger_holder :
  50. public logger_holder_base
  51. {
  52. //! The logger instance
  53. LoggerT m_Logger;
  54. logger_holder(const char* file, unsigned int line, LoggerT const& logger) :
  55. logger_holder_base(file, line),
  56. m_Logger(logger)
  57. {
  58. }
  59. std::type_info const& logger_type() const { return typeid(LoggerT); }
  60. };
  61. //! The class implements a global repository of tagged loggers
  62. struct global_storage
  63. {
  64. typedef shared_ptr< logger_holder_base >(*initializer_t)();
  65. //! Finds or creates the logger and returns its holder
  66. BOOST_LOG_API static shared_ptr< logger_holder_base > get_or_init(std::type_info const& key, initializer_t initializer);
  67. // Non-constructible, non-copyable, non-assignable
  68. BOOST_DELETED_FUNCTION(global_storage())
  69. BOOST_DELETED_FUNCTION(global_storage(global_storage const&))
  70. BOOST_DELETED_FUNCTION(global_storage& operator= (global_storage const&))
  71. };
  72. //! Throws the \c odr_violation exception
  73. BOOST_LOG_API BOOST_LOG_NORETURN void throw_odr_violation(
  74. std::type_info const& tag_type,
  75. std::type_info const& logger_type,
  76. logger_holder_base const& registered);
  77. //! The class implements a logger singleton
  78. template< typename TagT >
  79. struct logger_singleton :
  80. public boost::log::aux::lazy_singleton<
  81. logger_singleton< TagT >,
  82. shared_ptr< logger_holder< typename TagT::logger_type > >
  83. >
  84. {
  85. //! Base type
  86. typedef boost::log::aux::lazy_singleton<
  87. logger_singleton< TagT >,
  88. shared_ptr< logger_holder< typename TagT::logger_type > >
  89. > base_type;
  90. //! Logger type
  91. typedef typename TagT::logger_type logger_type;
  92. //! Returns the logger instance
  93. static logger_type& get()
  94. {
  95. return base_type::get()->m_Logger;
  96. }
  97. //! Initializes the logger instance (called only once)
  98. static void init_instance()
  99. {
  100. shared_ptr< logger_holder< logger_type > >& instance = base_type::get_instance();
  101. shared_ptr< logger_holder_base > holder = global_storage::get_or_init(
  102. typeid(boost::log::aux::visible_type< TagT >),
  103. &logger_singleton::construct_logger);
  104. instance = boost::dynamic_pointer_cast< logger_holder< logger_type > >(holder);
  105. if (!instance)
  106. {
  107. // In pure C++ this should never happen, since there cannot be two
  108. // different tag types that have equal type_infos. In real life it can
  109. // happen if the same-named tag is defined differently in two or more
  110. // dlls. This check is intended to detect such ODR violations. However, there
  111. // is no protection against different definitions of the logger type itself.
  112. throw_odr_violation(typeid(TagT), typeid(logger_type), *holder);
  113. }
  114. }
  115. private:
  116. //! Constructs a logger holder
  117. static shared_ptr< logger_holder_base > construct_logger()
  118. {
  119. return boost::make_shared< logger_holder< logger_type > >(
  120. TagT::registration_file(),
  121. static_cast< unsigned int >(TagT::registration_line),
  122. TagT::construct_logger());
  123. }
  124. };
  125. } // namespace aux
  126. //! The macro forward-declares a global logger with a custom initialization
  127. #define BOOST_LOG_GLOBAL_LOGGER(tag_name, logger)\
  128. struct tag_name\
  129. {\
  130. typedef logger logger_type;\
  131. enum registration_line_t { registration_line = __LINE__ };\
  132. static const char* registration_file() { return __FILE__; }\
  133. static logger_type construct_logger();\
  134. static inline logger_type& get()\
  135. {\
  136. return ::boost::log::sources::aux::logger_singleton< tag_name >::get();\
  137. }\
  138. };
  139. //! The macro defines a global logger initialization routine
  140. #define BOOST_LOG_GLOBAL_LOGGER_INIT(tag_name, logger)\
  141. tag_name::logger_type tag_name::construct_logger()
  142. //! The macro defines a global logger initializer that will default-construct the logger
  143. #define BOOST_LOG_GLOBAL_LOGGER_DEFAULT(tag_name, logger)\
  144. BOOST_LOG_GLOBAL_LOGGER_INIT(tag_name, logger)\
  145. {\
  146. return logger_type();\
  147. }
  148. //! The macro defines a global logger initializer that will construct the logger with the specified constructor arguments
  149. #define BOOST_LOG_GLOBAL_LOGGER_CTOR_ARGS(tag_name, logger, args)\
  150. BOOST_LOG_GLOBAL_LOGGER_INIT(tag_name, logger)\
  151. {\
  152. return logger_type(BOOST_PP_SEQ_ENUM(args));\
  153. }
  154. //! The macro declares a global logger with a custom initialization
  155. #define BOOST_LOG_INLINE_GLOBAL_LOGGER_INIT(tag_name, logger)\
  156. BOOST_LOG_GLOBAL_LOGGER(tag_name, logger)\
  157. inline BOOST_LOG_GLOBAL_LOGGER_INIT(tag_name, logger)
  158. //! The macro declares a global logger that will be default-constructed
  159. #define BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(tag_name, logger)\
  160. BOOST_LOG_INLINE_GLOBAL_LOGGER_INIT(tag_name, logger)\
  161. {\
  162. return logger_type();\
  163. }
  164. //! The macro declares a global logger that will be constructed with the specified arguments
  165. #define BOOST_LOG_INLINE_GLOBAL_LOGGER_CTOR_ARGS(tag_name, logger, args)\
  166. BOOST_LOG_INLINE_GLOBAL_LOGGER_INIT(tag_name, logger)\
  167. {\
  168. return logger_type(BOOST_PP_SEQ_ENUM(args));\
  169. }
  170. } // namespace sources
  171. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  172. } // namespace boost
  173. #include <boost/log/detail/footer.hpp>
  174. #endif // BOOST_LOG_SOURCES_GLOBAL_LOGGER_STORAGE_HPP_INCLUDED_