scoped_attribute.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 scoped_attribute.hpp
  9. * \author Andrey Semashev
  10. * \date 13.05.2007
  11. *
  12. * The header contains definition of facilities to define scoped attributes.
  13. */
  14. #ifndef BOOST_LOG_ATTRIBUTES_SCOPED_ATTRIBUTE_HPP_INCLUDED_
  15. #define BOOST_LOG_ATTRIBUTES_SCOPED_ATTRIBUTE_HPP_INCLUDED_
  16. #include <cstddef>
  17. #include <utility>
  18. #include <boost/move/core.hpp>
  19. #include <boost/move/utility_core.hpp>
  20. #include <boost/core/addressof.hpp>
  21. #include <boost/log/detail/config.hpp>
  22. #include <boost/log/core/core.hpp>
  23. #include <boost/log/sources/basic_logger.hpp>
  24. #include <boost/log/attributes/attribute.hpp>
  25. #include <boost/log/attributes/attribute_set.hpp>
  26. #include <boost/log/attributes/attribute_name.hpp>
  27. #include <boost/log/attributes/constant.hpp>
  28. #include <boost/log/utility/unused_variable.hpp>
  29. #include <boost/log/utility/unique_identifier_name.hpp>
  30. #include <boost/log/detail/header.hpp>
  31. #ifdef BOOST_HAS_PRAGMA_ONCE
  32. #pragma once
  33. #endif
  34. namespace boost {
  35. BOOST_LOG_OPEN_NAMESPACE
  36. namespace aux {
  37. //! A base class for all scoped attribute guards
  38. class attribute_scope_guard
  39. {
  40. };
  41. } // namespace aux
  42. //! Scoped attribute guard type
  43. typedef aux::attribute_scope_guard const& scoped_attribute;
  44. namespace aux {
  45. //! A scoped logger attribute guard
  46. template< typename LoggerT >
  47. class scoped_logger_attribute :
  48. public attribute_scope_guard
  49. {
  50. BOOST_COPYABLE_AND_MOVABLE_ALT(scoped_logger_attribute)
  51. private:
  52. //! Logger type
  53. typedef LoggerT logger_type;
  54. private:
  55. //! A reference to the logger
  56. logger_type* m_pLogger;
  57. //! An iterator to the added attribute
  58. attribute_set::iterator m_itAttribute;
  59. public:
  60. //! Constructor
  61. scoped_logger_attribute(logger_type& l, attribute_name const& name, attribute const& attr) :
  62. m_pLogger(boost::addressof(l))
  63. {
  64. std::pair<
  65. attribute_set::iterator,
  66. bool
  67. > res = l.add_attribute(name, attr);
  68. if (res.second)
  69. m_itAttribute = res.first;
  70. else
  71. m_pLogger = NULL; // if there already is a same-named attribute, don't register anything
  72. }
  73. //! Move constructor
  74. scoped_logger_attribute(BOOST_RV_REF(scoped_logger_attribute) that) BOOST_NOEXCEPT :
  75. m_pLogger(that.m_pLogger),
  76. m_itAttribute(that.m_itAttribute)
  77. {
  78. that.m_pLogger = NULL;
  79. }
  80. //! Destructor
  81. ~scoped_logger_attribute()
  82. {
  83. if (m_pLogger)
  84. m_pLogger->remove_attribute(m_itAttribute);
  85. }
  86. #ifndef BOOST_LOG_BROKEN_REFERENCE_FROM_RVALUE_INIT
  87. BOOST_DELETED_FUNCTION(scoped_logger_attribute(scoped_logger_attribute const&))
  88. #else // BOOST_LOG_BROKEN_REFERENCE_FROM_RVALUE_INIT
  89. scoped_logger_attribute(scoped_logger_attribute const& that) BOOST_NOEXCEPT : m_pLogger(that.m_pLogger), m_itAttribute(that.m_itAttribute)
  90. {
  91. const_cast< scoped_logger_attribute& >(that).m_pLogger = NULL;
  92. }
  93. #endif // BOOST_LOG_BROKEN_REFERENCE_FROM_RVALUE_INIT
  94. BOOST_DELETED_FUNCTION(scoped_logger_attribute& operator= (scoped_logger_attribute const&))
  95. };
  96. } // namespace aux
  97. // Generator helper functions
  98. /*!
  99. * Registers an attribute in the logger
  100. *
  101. * \param l Logger to register the attribute in
  102. * \param name Attribute name
  103. * \param attr The attribute. Must not be NULL.
  104. * \return An unspecified guard object which may be used to initialize a \c scoped_attribute variable.
  105. */
  106. template< typename LoggerT >
  107. BOOST_FORCEINLINE aux::scoped_logger_attribute< LoggerT > add_scoped_logger_attribute(LoggerT& l, attribute_name const& name, attribute const& attr)
  108. {
  109. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  110. return aux::scoped_logger_attribute< LoggerT >(l, name, attr);
  111. #else
  112. aux::scoped_logger_attribute< LoggerT > guard(l, name, attr);
  113. return boost::move(guard);
  114. #endif
  115. }
  116. #ifndef BOOST_LOG_DOXYGEN_PASS
  117. #define BOOST_LOG_SCOPED_LOGGER_ATTR_INTERNAL(logger, attr_name, attr, sentry_var_name)\
  118. BOOST_LOG_UNUSED_VARIABLE(::boost::log::scoped_attribute, sentry_var_name,\
  119. = ::boost::log::add_scoped_logger_attribute(logger, attr_name, (attr)));
  120. #endif // BOOST_LOG_DOXYGEN_PASS
  121. //! The macro sets a scoped logger-wide attribute in a more compact way
  122. #define BOOST_LOG_SCOPED_LOGGER_ATTR(logger, attr_name, attr)\
  123. BOOST_LOG_SCOPED_LOGGER_ATTR_INTERNAL(\
  124. logger,\
  125. attr_name,\
  126. attr,\
  127. BOOST_LOG_UNIQUE_IDENTIFIER_NAME(_boost_log_scoped_logger_attr_sentry_))
  128. //! The macro sets a scoped logger-wide tag in a more compact way
  129. #define BOOST_LOG_SCOPED_LOGGER_TAG(logger, attr_name, attr_value)\
  130. BOOST_LOG_SCOPED_LOGGER_ATTR(logger, attr_name, ::boost::log::attributes::make_constant(attr_value))
  131. namespace aux {
  132. //! A scoped thread-specific attribute guard
  133. class scoped_thread_attribute :
  134. public attribute_scope_guard
  135. {
  136. BOOST_COPYABLE_AND_MOVABLE_ALT(scoped_thread_attribute)
  137. private:
  138. //! A pointer to the logging core
  139. core_ptr m_pCore;
  140. //! An iterator to the added attribute
  141. attribute_set::iterator m_itAttribute;
  142. public:
  143. //! Constructor
  144. scoped_thread_attribute(attribute_name const& name, attribute const& attr) :
  145. m_pCore(core::get())
  146. {
  147. std::pair<
  148. attribute_set::iterator,
  149. bool
  150. > res = m_pCore->add_thread_attribute(name, attr);
  151. if (res.second)
  152. m_itAttribute = res.first;
  153. else
  154. m_pCore.reset(); // if there already is a same-named attribute, don't register anything
  155. }
  156. //! Move constructor
  157. scoped_thread_attribute(BOOST_RV_REF(scoped_thread_attribute) that) : m_itAttribute(that.m_itAttribute)
  158. {
  159. m_pCore.swap(that.m_pCore);
  160. }
  161. //! Destructor
  162. ~scoped_thread_attribute()
  163. {
  164. if (!!m_pCore)
  165. m_pCore->remove_thread_attribute(m_itAttribute);
  166. }
  167. #ifndef BOOST_LOG_BROKEN_REFERENCE_FROM_RVALUE_INIT
  168. BOOST_DELETED_FUNCTION(scoped_thread_attribute(scoped_thread_attribute const&))
  169. #else // BOOST_LOG_BROKEN_REFERENCE_FROM_RVALUE_INIT
  170. scoped_thread_attribute(scoped_thread_attribute const& that) : m_itAttribute(that.m_itAttribute)
  171. {
  172. m_pCore.swap(const_cast< scoped_thread_attribute& >(that).m_pCore);
  173. }
  174. #endif // BOOST_LOG_BROKEN_REFERENCE_FROM_RVALUE_INIT
  175. BOOST_DELETED_FUNCTION(scoped_thread_attribute& operator= (scoped_thread_attribute const&))
  176. };
  177. } // namespace aux
  178. // Generator helper functions
  179. /*!
  180. * Registers a thread-specific attribute
  181. *
  182. * \param name Attribute name
  183. * \param attr The attribute. Must not be NULL.
  184. * \return An unspecified guard object which may be used to initialize a \c scoped_attribute variable.
  185. */
  186. BOOST_FORCEINLINE aux::scoped_thread_attribute add_scoped_thread_attribute(attribute_name const& name, attribute const& attr)
  187. {
  188. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  189. return aux::scoped_thread_attribute(name, attr);
  190. #else
  191. aux::scoped_thread_attribute guard(name, attr);
  192. return boost::move(guard);
  193. #endif
  194. }
  195. #ifndef BOOST_LOG_DOXYGEN_PASS
  196. #define BOOST_LOG_SCOPED_THREAD_ATTR_INTERNAL(attr_name, attr, sentry_var_name)\
  197. BOOST_LOG_UNUSED_VARIABLE(::boost::log::scoped_attribute, sentry_var_name,\
  198. = ::boost::log::add_scoped_thread_attribute(attr_name, (attr)));
  199. #endif // BOOST_LOG_DOXYGEN_PASS
  200. //! The macro sets a scoped thread-wide attribute in a more compact way
  201. #define BOOST_LOG_SCOPED_THREAD_ATTR(attr_name, attr)\
  202. BOOST_LOG_SCOPED_THREAD_ATTR_INTERNAL(\
  203. attr_name,\
  204. attr,\
  205. BOOST_LOG_UNIQUE_IDENTIFIER_NAME(_boost_log_scoped_thread_attr_sentry_))
  206. //! The macro sets a scoped thread-wide tag in a more compact way
  207. #define BOOST_LOG_SCOPED_THREAD_TAG(attr_name, attr_value)\
  208. BOOST_LOG_SCOPED_THREAD_ATTR(attr_name, ::boost::log::attributes::make_constant(attr_value))
  209. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  210. } // namespace boost
  211. #include <boost/log/detail/footer.hpp>
  212. #endif // BOOST_LOG_ATTRIBUTES_SCOPED_ATTRIBUTE_HPP_INCLUDED_