record.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 record.hpp
  9. * \author Andrey Semashev
  10. * \date 09.03.2009
  11. *
  12. * This header contains a logging record class definition.
  13. */
  14. #ifndef BOOST_LOG_CORE_RECORD_HPP_INCLUDED_
  15. #define BOOST_LOG_CORE_RECORD_HPP_INCLUDED_
  16. #include <boost/move/core.hpp>
  17. #include <boost/core/explicit_operator_bool.hpp>
  18. #include <boost/log/detail/config.hpp>
  19. #include <boost/log/attributes/attribute_value_set.hpp>
  20. #include <boost/log/expressions/keyword_fwd.hpp>
  21. #include <boost/log/core/record_view.hpp>
  22. #include <boost/log/detail/header.hpp>
  23. #ifdef BOOST_HAS_PRAGMA_ONCE
  24. #pragma once
  25. #endif
  26. namespace boost {
  27. BOOST_LOG_OPEN_NAMESPACE
  28. class core;
  29. /*!
  30. * \brief Logging record class
  31. *
  32. * The logging record encapsulates all information related to a single logging statement,
  33. * in particular, attribute values view and the log message string. The record can be updated before pushing
  34. * for further processing to the logging core.
  35. */
  36. class record
  37. {
  38. BOOST_MOVABLE_BUT_NOT_COPYABLE(record)
  39. friend class core;
  40. #ifndef BOOST_LOG_DOXYGEN_PASS
  41. private:
  42. //! Private data
  43. typedef record_view::public_data public_data;
  44. private:
  45. //! A pointer to the log record implementation
  46. public_data* m_impl;
  47. private:
  48. // A private constructor, accessible from core
  49. BOOST_CONSTEXPR explicit record(public_data* impl) BOOST_NOEXCEPT : m_impl(impl) {}
  50. #endif // BOOST_LOG_DOXYGEN_PASS
  51. public:
  52. /*!
  53. * Default constructor. Creates an empty record that is equivalent to the invalid record handle.
  54. *
  55. * \post <tt>!*this == true</tt>
  56. */
  57. BOOST_CONSTEXPR record() BOOST_NOEXCEPT : m_impl(NULL) {}
  58. /*!
  59. * Move constructor. Source record contents unspecified after the operation.
  60. */
  61. record(BOOST_RV_REF(record) that) BOOST_NOEXCEPT : m_impl(that.m_impl)
  62. {
  63. that.m_impl = NULL;
  64. }
  65. /*!
  66. * Destructor. Destroys the record, releases any sinks and attribute values that were involved in processing this record.
  67. */
  68. ~record() BOOST_NOEXCEPT
  69. {
  70. reset();
  71. }
  72. /*!
  73. * Move assignment. Source record contents unspecified after the operation.
  74. */
  75. record& operator= (BOOST_RV_REF(record) that) BOOST_NOEXCEPT
  76. {
  77. swap(static_cast< record& >(that));
  78. return *this;
  79. }
  80. /*!
  81. * \return A reference to the set of attribute values attached to this record
  82. *
  83. * \pre <tt>!!*this</tt>
  84. */
  85. attribute_value_set& attribute_values() BOOST_NOEXCEPT
  86. {
  87. return m_impl->m_attribute_values;
  88. }
  89. /*!
  90. * \return A reference to the set of attribute values attached to this record
  91. *
  92. * \pre <tt>!!*this</tt>
  93. */
  94. attribute_value_set const& attribute_values() const BOOST_NOEXCEPT
  95. {
  96. return m_impl->m_attribute_values;
  97. }
  98. /*!
  99. * Conversion to an unspecified boolean type
  100. *
  101. * \return \c true, if the <tt>*this</tt> identifies a log record, \c false, if the <tt>*this</tt> is not valid
  102. */
  103. BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()
  104. /*!
  105. * Inverted conversion to an unspecified boolean type
  106. *
  107. * \return \c false, if the <tt>*this</tt> identifies a log record, \c true, if the <tt>*this</tt> is not valid
  108. */
  109. bool operator! () const BOOST_NOEXCEPT
  110. {
  111. return !m_impl;
  112. }
  113. /*!
  114. * Swaps two handles
  115. *
  116. * \param that Another record to swap with
  117. * <b>Throws:</b> Nothing
  118. */
  119. void swap(record& that) BOOST_NOEXCEPT
  120. {
  121. public_data* p = m_impl;
  122. m_impl = that.m_impl;
  123. that.m_impl = p;
  124. }
  125. /*!
  126. * Resets the log record handle. If there are no other handles left, the log record is closed
  127. * and all resources referenced by the record are released.
  128. *
  129. * \post <tt>!*this == true</tt>
  130. */
  131. void reset() BOOST_NOEXCEPT
  132. {
  133. if (m_impl)
  134. {
  135. public_data::destroy(m_impl);
  136. m_impl = NULL;
  137. }
  138. }
  139. /*!
  140. * Attribute value lookup.
  141. *
  142. * \param name Attribute name.
  143. * \return An \c attribute_value, non-empty if it is found, empty otherwise.
  144. */
  145. attribute_value_set::mapped_type operator[] (attribute_value_set::key_type name) const
  146. {
  147. return m_impl->m_attribute_values[name];
  148. }
  149. /*!
  150. * Attribute value lookup.
  151. *
  152. * \param keyword Attribute keyword.
  153. * \return A \c value_ref with extracted attribute value if it is found, empty \c value_ref otherwise.
  154. */
  155. template< typename DescriptorT, template< typename > class ActorT >
  156. typename result_of::extract< typename expressions::attribute_keyword< DescriptorT, ActorT >::value_type, DescriptorT >::type
  157. operator[] (expressions::attribute_keyword< DescriptorT, ActorT > const& keyword) const
  158. {
  159. return m_impl->m_attribute_values[keyword];
  160. }
  161. /*!
  162. * The function ensures that the log record does not depend on any thread-specific data. Then the record contents
  163. * are used to construct a \c record_view which is returned from the function. The record is no longer valid after the call.
  164. *
  165. * \pre <tt>!!*this</tt>
  166. * \post <tt>!*this</tt>
  167. * \returns The record view that contains all attribute values from the original record.
  168. */
  169. BOOST_LOG_API record_view lock();
  170. };
  171. /*!
  172. * A free-standing swap function overload for \c record
  173. */
  174. inline void swap(record& left, record& right) BOOST_NOEXCEPT
  175. {
  176. left.swap(right);
  177. }
  178. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  179. } // namespace boost
  180. #include <boost/log/detail/footer.hpp>
  181. #endif // BOOST_LOG_CORE_RECORD_HPP_INCLUDED_