severity_feature.hpp 9.7 KB

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