scoped_attribute.hpp 7.9 KB

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