severity_feature.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 severity_feature.hpp
  9. * \author Andrey Semashev
  10. * \date 08.03.2007
  11. *
  12. * The header contains implementation of a severity level support feature.
  13. */
  14. #ifndef BOOST_LOG_SOURCES_SEVERITY_FEATURE_HPP_INCLUDED_
  15. #define BOOST_LOG_SOURCES_SEVERITY_FEATURE_HPP_INCLUDED_
  16. #include <boost/cstdint.hpp>
  17. #include <boost/static_assert.hpp>
  18. #include <boost/core/swap.hpp>
  19. #include <boost/smart_ptr/intrusive_ptr.hpp>
  20. #include <boost/move/core.hpp>
  21. #include <boost/move/utility_core.hpp>
  22. #include <boost/type_traits/is_nothrow_move_constructible.hpp>
  23. #include <boost/log/detail/config.hpp>
  24. #include <boost/log/detail/locks.hpp>
  25. #include <boost/log/detail/default_attribute_names.hpp>
  26. #include <boost/log/attributes/attribute.hpp>
  27. #include <boost/log/attributes/attribute_cast.hpp>
  28. #include <boost/log/attributes/attribute_value_impl.hpp>
  29. #include <boost/log/utility/strictest_lock.hpp>
  30. #include <boost/log/utility/type_dispatch/type_dispatcher.hpp>
  31. #include <boost/log/keywords/severity.hpp>
  32. #include <boost/log/core/record.hpp>
  33. #include <boost/log/detail/header.hpp>
  34. #ifdef BOOST_HAS_PRAGMA_ONCE
  35. #pragma once
  36. #endif
  37. namespace boost {
  38. BOOST_LOG_OPEN_NAMESPACE
  39. namespace sources {
  40. namespace aux {
  41. //! The method returns the storage for severity level for the current thread
  42. BOOST_LOG_API uintmax_t& get_severity_level();
  43. //! Severity level attribute implementation
  44. template< typename LevelT >
  45. class severity_level :
  46. public attribute
  47. {
  48. typedef severity_level this_type;
  49. BOOST_COPYABLE_AND_MOVABLE(this_type)
  50. public:
  51. //! Stored level type
  52. typedef LevelT value_type;
  53. BOOST_STATIC_ASSERT_MSG(sizeof(value_type) <= sizeof(uintmax_t), "Boost.Log: Unsupported severity level type, the severity level must fit into uintmax_t");
  54. protected:
  55. //! Factory implementation
  56. class BOOST_SYMBOL_VISIBLE impl :
  57. public attribute_value::impl
  58. {
  59. public:
  60. //! The method dispatches the value to the given object
  61. bool dispatch(type_dispatcher& dispatcher) BOOST_OVERRIDE
  62. {
  63. type_dispatcher::callback< value_type > callback = dispatcher.get_callback< value_type >();
  64. if (callback)
  65. {
  66. callback(reinterpret_cast< value_type const& >(get_severity_level()));
  67. return true;
  68. }
  69. else
  70. return false;
  71. }
  72. //! The method is called when the attribute value is passed to another thread
  73. intrusive_ptr< attribute_value::impl > detach_from_thread() BOOST_OVERRIDE
  74. {
  75. #if !defined(BOOST_LOG_NO_THREADS)
  76. return new attributes::attribute_value_impl< value_type >(
  77. reinterpret_cast< value_type const& >(get_severity_level()));
  78. #else
  79. // With multithreading disabled we may safely return this here. This method will not be called anyway.
  80. return this;
  81. #endif
  82. }
  83. };
  84. public:
  85. //! Default constructor
  86. severity_level() : attribute(new impl())
  87. {
  88. }
  89. //! Copy constructor
  90. severity_level(severity_level const& that) BOOST_NOEXCEPT : attribute(static_cast< attribute const& >(that))
  91. {
  92. }
  93. //! Move constructor
  94. severity_level(BOOST_RV_REF(severity_level) that) BOOST_NOEXCEPT : attribute(boost::move(static_cast< attribute& >(that)))
  95. {
  96. }
  97. //! Constructor for casting support
  98. explicit severity_level(attributes::cast_source const& source) :
  99. attribute(source.as< impl >())
  100. {
  101. }
  102. /*!
  103. * Copy assignment
  104. */
  105. severity_level& operator= (BOOST_COPY_ASSIGN_REF(severity_level) that) BOOST_NOEXCEPT
  106. {
  107. attribute::operator= (static_cast< attribute const& >(that));
  108. return *this;
  109. }
  110. /*!
  111. * Move assignment
  112. */
  113. severity_level& operator= (BOOST_RV_REF(severity_level) that) BOOST_NOEXCEPT
  114. {
  115. this->swap(that);
  116. return *this;
  117. }
  118. //! The method sets the actual level
  119. void set_value(value_type level)
  120. {
  121. reinterpret_cast< value_type& >(get_severity_level()) = level;
  122. }
  123. };
  124. } // namespace aux
  125. /*!
  126. * \brief Severity level feature implementation
  127. */
  128. template< typename BaseT, typename LevelT = int >
  129. class basic_severity_logger :
  130. public BaseT
  131. {
  132. //! Base type
  133. typedef BaseT base_type;
  134. typedef basic_severity_logger this_type;
  135. BOOST_COPYABLE_AND_MOVABLE_ALT(this_type)
  136. public:
  137. //! Character type
  138. typedef typename base_type::char_type char_type;
  139. //! Final type
  140. typedef typename base_type::final_type final_type;
  141. //! Threading model being used
  142. typedef typename base_type::threading_model threading_model;
  143. //! Severity level type
  144. typedef LevelT severity_level;
  145. //! Severity attribute type
  146. typedef aux::severity_level< severity_level > severity_attribute;
  147. #if defined(BOOST_LOG_DOXYGEN_PASS)
  148. //! Lock requirement for the \c open_record_unlocked method
  149. typedef typename strictest_lock<
  150. typename base_type::open_record_lock,
  151. no_lock< threading_model >
  152. >::type open_record_lock;
  153. #endif // defined(BOOST_LOG_DOXYGEN_PASS)
  154. //! Lock requirement for the \c swap_unlocked method
  155. typedef typename strictest_lock<
  156. typename base_type::swap_lock,
  157. #ifndef BOOST_LOG_NO_THREADS
  158. boost::log::aux::multiple_unique_lock2< threading_model, threading_model >
  159. #else
  160. no_lock< threading_model >
  161. #endif // !defined(BOOST_LOG_NO_THREADS)
  162. >::type swap_lock;
  163. private:
  164. //! Default severity
  165. severity_level m_DefaultSeverity;
  166. //! Severity attribute
  167. severity_attribute m_SeverityAttr;
  168. public:
  169. /*!
  170. * Default constructor. The constructed logger will have a severity attribute registered.
  171. * The default level for log records will be 0.
  172. */
  173. basic_severity_logger() :
  174. base_type(),
  175. m_DefaultSeverity(static_cast< severity_level >(0))
  176. {
  177. base_type::add_attribute_unlocked(boost::log::aux::default_attribute_names::severity(), m_SeverityAttr);
  178. }
  179. /*!
  180. * Copy constructor
  181. */
  182. basic_severity_logger(basic_severity_logger const& that) :
  183. base_type(static_cast< base_type const& >(that)),
  184. m_DefaultSeverity(that.m_DefaultSeverity),
  185. m_SeverityAttr(that.m_SeverityAttr)
  186. {
  187. // Our attributes must refer to our severity attribute
  188. base_type::attributes()[boost::log::aux::default_attribute_names::severity()] = m_SeverityAttr;
  189. }
  190. /*!
  191. * Move constructor
  192. */
  193. basic_severity_logger(BOOST_RV_REF(basic_severity_logger) that) BOOST_NOEXCEPT_IF(boost::is_nothrow_move_constructible< base_type >::value &&
  194. boost::is_nothrow_move_constructible< severity_level >::value &&
  195. boost::is_nothrow_move_constructible< severity_attribute >::value) :
  196. base_type(boost::move(static_cast< base_type& >(that))),
  197. m_DefaultSeverity(boost::move(that.m_DefaultSeverity)),
  198. m_SeverityAttr(boost::move(that.m_SeverityAttr))
  199. {
  200. }
  201. /*!
  202. * Constructor with named arguments. Allows to setup the default level for log records.
  203. *
  204. * \param args A set of named arguments. The following arguments are supported:
  205. * \li \c severity - default severity value
  206. */
  207. template< typename ArgsT >
  208. explicit basic_severity_logger(ArgsT const& args) :
  209. base_type(args),
  210. m_DefaultSeverity(args[keywords::severity | severity_level()])
  211. {
  212. base_type::add_attribute_unlocked(boost::log::aux::default_attribute_names::severity(), m_SeverityAttr);
  213. }
  214. /*!
  215. * Default severity value getter
  216. */
  217. severity_level default_severity() const { return m_DefaultSeverity; }
  218. protected:
  219. /*!
  220. * Severity attribute accessor
  221. */
  222. severity_attribute const& get_severity_attribute() const { return m_SeverityAttr; }
  223. /*!
  224. * Unlocked \c open_record
  225. */
  226. template< typename ArgsT >
  227. record open_record_unlocked(ArgsT const& args)
  228. {
  229. m_SeverityAttr.set_value(args[keywords::severity | m_DefaultSeverity]);
  230. return base_type::open_record_unlocked(args);
  231. }
  232. //! Unlocked \c swap
  233. void swap_unlocked(basic_severity_logger& that)
  234. {
  235. base_type::swap_unlocked(static_cast< base_type& >(that));
  236. boost::swap(m_DefaultSeverity, that.m_DefaultSeverity);
  237. m_SeverityAttr.swap(that.m_SeverityAttr);
  238. }
  239. };
  240. /*!
  241. * \brief Severity level support feature
  242. *
  243. * The logger with this feature registers a special attribute with an integral value type on construction.
  244. * This attribute will provide severity level for each log record being made through the logger.
  245. * The severity level can be omitted on logging record construction, in which case the default
  246. * level will be used. The default level can also be customized by passing it to the logger constructor.
  247. *
  248. * The type of the severity level attribute can be specified as a template parameter for the feature
  249. * template. By default, \c int will be used.
  250. */
  251. template< typename LevelT = int >
  252. struct severity
  253. {
  254. template< typename BaseT >
  255. struct apply
  256. {
  257. typedef basic_severity_logger<
  258. BaseT,
  259. LevelT
  260. > type;
  261. };
  262. };
  263. } // namespace sources
  264. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  265. } // namespace boost
  266. //! The macro allows to put a record with a specific severity level into log
  267. #define BOOST_LOG_STREAM_SEV(logger, lvl)\
  268. BOOST_LOG_STREAM_WITH_PARAMS((logger), (::boost::log::keywords::severity = (lvl)))
  269. #ifndef BOOST_LOG_NO_SHORTHAND_NAMES
  270. //! An equivalent to BOOST_LOG_STREAM_SEV(logger, lvl)
  271. #define BOOST_LOG_SEV(logger, lvl) BOOST_LOG_STREAM_SEV(logger, lvl)
  272. #endif // BOOST_LOG_NO_SHORTHAND_NAMES
  273. #include <boost/log/detail/footer.hpp>
  274. #endif // BOOST_LOG_SOURCES_SEVERITY_FEATURE_HPP_INCLUDED_