named_scope.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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
  9. * \author Andrey Semashev
  10. * \date 24.06.2007
  11. *
  12. * The header contains implementation of named scope container and an attribute that allows to
  13. * put the named scope to log. A number of convenience macros are also provided.
  14. */
  15. #ifndef BOOST_LOG_ATTRIBUTES_NAMED_SCOPE_HPP_INCLUDED_
  16. #define BOOST_LOG_ATTRIBUTES_NAMED_SCOPE_HPP_INCLUDED_
  17. #include <ostream>
  18. #include <memory>
  19. #include <iterator>
  20. #include <cstddef>
  21. #include <boost/log/detail/config.hpp>
  22. #include <boost/current_function.hpp>
  23. #include <boost/mpl/if.hpp>
  24. #include <boost/log/utility/string_literal.hpp>
  25. #include <boost/log/utility/unique_identifier_name.hpp>
  26. #include <boost/log/utility/unused_variable.hpp>
  27. #include <boost/log/attributes/attribute.hpp>
  28. #include <boost/log/attributes/attribute_cast.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 attributes {
  36. namespace aux {
  37. //! Double-linked list node
  38. struct named_scope_list_node
  39. {
  40. mutable named_scope_list_node* _m_pPrev;
  41. mutable named_scope_list_node* _m_pNext;
  42. named_scope_list_node() BOOST_NOEXCEPT { _m_pPrev = _m_pNext = this; }
  43. };
  44. } // namespace aux
  45. /*!
  46. * \brief The structure contains all information about a named scope
  47. *
  48. * The named scope entries are stored as elements of \c basic_named_scope_list container, which
  49. * in turn can be acquired either from the \c basic_named_scope attribute value or from a thread-local
  50. * instance.
  51. */
  52. struct named_scope_entry
  53. //! \cond
  54. : public aux::named_scope_list_node
  55. //! \endcond
  56. {
  57. /*!
  58. * \brief Scope entry type
  59. *
  60. * Describes scope name specifics
  61. */
  62. enum scope_name_type
  63. {
  64. general, //!< The scope name contains some unstructured string that should not be interpreted by the library
  65. function //!< The scope name contains a function signature
  66. };
  67. /*!
  68. * The scope name (e.g. a function signature)
  69. */
  70. string_literal scope_name;
  71. /*!
  72. * The source file name
  73. */
  74. string_literal file_name;
  75. /*!
  76. * The line number in the source file
  77. */
  78. unsigned int line;
  79. /*!
  80. * The scope name type
  81. */
  82. scope_name_type type;
  83. /*!
  84. * Initializing constructor
  85. *
  86. * \post <tt>scope_name == sn && file_name == fn && line == ln</tt>
  87. *
  88. * \b Throws: Nothing.
  89. */
  90. named_scope_entry(string_literal const& sn, string_literal const& fn, unsigned int ln, scope_name_type t = general) BOOST_NOEXCEPT :
  91. scope_name(sn),
  92. file_name(fn),
  93. line(ln),
  94. type(t)
  95. {
  96. }
  97. };
  98. /*!
  99. * \brief The class implements the list of scopes
  100. *
  101. * The scope list provides a read-only access to a doubly-linked list of scopes.
  102. */
  103. class named_scope_list
  104. //! \cond
  105. : protected std::allocator< named_scope_entry >
  106. //! \endcond
  107. {
  108. public:
  109. //! Allocator type
  110. typedef std::allocator< named_scope_entry > allocator_type;
  111. // Standard types
  112. typedef allocator_type::value_type value_type;
  113. typedef allocator_type::reference reference;
  114. typedef allocator_type::const_reference const_reference;
  115. typedef allocator_type::pointer pointer;
  116. typedef allocator_type::const_pointer const_pointer;
  117. typedef allocator_type::size_type size_type;
  118. typedef allocator_type::difference_type difference_type;
  119. #ifndef BOOST_LOG_DOXYGEN_PASS
  120. protected:
  121. //! Iterator class
  122. #ifndef BOOST_LOG_NO_MEMBER_TEMPLATE_FRIENDS
  123. template< bool fConstV > class iter;
  124. template< bool fConstV > friend class iter;
  125. #endif
  126. template< bool fConstV >
  127. class iter
  128. {
  129. friend class iter< !fConstV >;
  130. public:
  131. // Standard typedefs
  132. typedef named_scope_list::difference_type difference_type;
  133. typedef named_scope_list::value_type value_type;
  134. typedef typename mpl::if_c<
  135. fConstV,
  136. named_scope_list::const_reference,
  137. named_scope_list::reference
  138. >::type reference;
  139. typedef typename mpl::if_c<
  140. fConstV,
  141. named_scope_list::const_pointer,
  142. named_scope_list::pointer
  143. >::type pointer;
  144. typedef std::bidirectional_iterator_tag iterator_category;
  145. public:
  146. // Constructors
  147. iter() : m_pNode(NULL) {}
  148. explicit iter(aux::named_scope_list_node* pNode) : m_pNode(pNode) {}
  149. iter(iter< false > const& that) : m_pNode(that.m_pNode) {}
  150. //! Assignment
  151. template< bool f >
  152. iter& operator= (iter< f > const& that)
  153. {
  154. m_pNode = that.m_pNode;
  155. return *this;
  156. }
  157. // Comparison
  158. template< bool f >
  159. bool operator== (iter< f > const& that) const { return (m_pNode == that.m_pNode); }
  160. template< bool f >
  161. bool operator!= (iter< f > const& that) const { return (m_pNode != that.m_pNode); }
  162. // Modification
  163. iter& operator++ ()
  164. {
  165. m_pNode = m_pNode->_m_pNext;
  166. return *this;
  167. }
  168. iter& operator-- ()
  169. {
  170. m_pNode = m_pNode->_m_pPrev;
  171. return *this;
  172. }
  173. iter operator++ (int)
  174. {
  175. iter tmp(*this);
  176. m_pNode = m_pNode->_m_pNext;
  177. return tmp;
  178. }
  179. iter operator-- (int)
  180. {
  181. iter tmp(*this);
  182. m_pNode = m_pNode->_m_pPrev;
  183. return tmp;
  184. }
  185. // Dereferencing
  186. pointer operator-> () const { return static_cast< pointer >(m_pNode); }
  187. reference operator* () const { return *static_cast< pointer >(m_pNode); }
  188. private:
  189. aux::named_scope_list_node* m_pNode;
  190. };
  191. public:
  192. typedef iter< true > const_iterator;
  193. typedef iter< false > iterator;
  194. typedef std::reverse_iterator< const_iterator > const_reverse_iterator;
  195. typedef std::reverse_iterator< iterator > reverse_iterator;
  196. protected:
  197. //! The root node of the container
  198. aux::named_scope_list_node m_RootNode;
  199. //! The size of the container
  200. size_type m_Size;
  201. //! The flag shows if the contained elements are dynamically allocated
  202. bool m_fNeedToDeallocate;
  203. #else // BOOST_LOG_DOXYGEN_PASS
  204. /*!
  205. * A constant iterator to the sequence of scopes. Complies to bidirectional iterator requirements.
  206. */
  207. typedef implementation_defined const_iterator;
  208. /*!
  209. * An iterator to the sequence of scopes. Complies to bidirectional iterator requirements.
  210. */
  211. typedef implementation_defined iterator;
  212. /*!
  213. * A constant reverse iterator to the sequence of scopes. Complies to bidirectional iterator requirements.
  214. */
  215. typedef implementation_defined const_reverse_iterator;
  216. /*!
  217. * A reverse iterator to the sequence of scopes. Complies to bidirectional iterator requirements.
  218. */
  219. typedef implementation_defined reverse_iterator;
  220. #endif // BOOST_LOG_DOXYGEN_PASS
  221. public:
  222. /*!
  223. * Default constructor
  224. *
  225. * \post <tt>empty() == true</tt>
  226. */
  227. named_scope_list() : m_Size(0), m_fNeedToDeallocate(false) {}
  228. /*!
  229. * Copy constructor
  230. *
  231. * \post <tt>std::equal(begin(), end(), that.begin()) == true</tt>
  232. */
  233. BOOST_LOG_API named_scope_list(named_scope_list const& that);
  234. /*!
  235. * Destructor. Destroys the stored entries.
  236. */
  237. BOOST_LOG_API ~named_scope_list();
  238. /*!
  239. * Assignment operator
  240. *
  241. * \post <tt>std::equal(begin(), end(), that.begin()) == true</tt>
  242. */
  243. named_scope_list& operator= (named_scope_list const& that)
  244. {
  245. if (this != &that)
  246. {
  247. named_scope_list tmp(that);
  248. swap(tmp);
  249. }
  250. return *this;
  251. }
  252. /*!
  253. * \return Constant iterator to the first element of the container.
  254. */
  255. const_iterator begin() const { return const_iterator(m_RootNode._m_pNext); }
  256. /*!
  257. * \return Constant iterator to the after-the-last element of the container.
  258. */
  259. const_iterator end() const { return const_iterator(const_cast< aux::named_scope_list_node* >(&m_RootNode)); }
  260. /*!
  261. * \return Constant iterator to the last element of the container.
  262. */
  263. const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
  264. /*!
  265. * \return Constant iterator to the before-the-first element of the container.
  266. */
  267. const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
  268. /*!
  269. * \return The number of elements in the container
  270. */
  271. size_type size() const { return m_Size; }
  272. /*!
  273. * \return true if the container is empty and false otherwise
  274. */
  275. bool empty() const { return (m_Size == 0); }
  276. /*!
  277. * Swaps two instances of the container
  278. */
  279. BOOST_LOG_API void swap(named_scope_list& that);
  280. /*!
  281. * \return Last pushed scope entry
  282. */
  283. const_reference back() const { return *rbegin(); }
  284. /*!
  285. * \return First pushed scope entry
  286. */
  287. const_reference front() const { return *begin(); }
  288. };
  289. //! Stream output operator
  290. template< typename CharT, typename TraitsT >
  291. inline std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, named_scope_list const& sl)
  292. {
  293. if (strm.good())
  294. {
  295. named_scope_list::const_iterator it = sl.begin(), end = sl.end();
  296. if (it != end)
  297. {
  298. strm << it->scope_name.c_str();
  299. for (++it; it != end; ++it)
  300. strm << "->" << it->scope_name.c_str();
  301. }
  302. }
  303. return strm;
  304. }
  305. /*!
  306. * \brief A class of an attribute that holds stack of named scopes of the current thread
  307. *
  308. * The basic_named_scope attribute is essentially a hook to the thread-specific instance of
  309. * scope list. This means that the attribute will generate different values if get_value is
  310. * called in different threads. The attribute generates value with stored type
  311. * <tt>basic_named_scope_list< CharT ></tt>.
  312. *
  313. * The attribute class can also be used to gain access to the scope stack instance, e.g. to
  314. * get its copy or to push or pop a scope entry. However, it is highly not recommended to
  315. * maintain scope list manually. Use \c BOOST_LOG_NAMED_SCOPE or \c BOOST_LOG_FUNCTION macros instead.
  316. */
  317. class BOOST_LOG_API named_scope :
  318. public attribute
  319. {
  320. public:
  321. //! Scope names stack (the attribute value type)
  322. typedef named_scope_list value_type;
  323. //! Scope entry
  324. typedef value_type::value_type scope_entry;
  325. //! Sentry object class to automatically push and pop scopes
  326. struct sentry
  327. {
  328. /*!
  329. * Constructor. Pushes the specified scope to the end of the thread-local list of scopes.
  330. *
  331. * \param sn Scope name.
  332. * \param fn File name, in which the scope is located.
  333. * \param ln Line number in the file.
  334. */
  335. sentry(string_literal const& sn, string_literal const& fn, unsigned int ln, scope_entry::scope_name_type t = scope_entry::general) BOOST_NOEXCEPT :
  336. m_Entry(sn, fn, ln, t)
  337. {
  338. named_scope::push_scope(m_Entry);
  339. }
  340. /*!
  341. * Destructor. Removes the last pushed scope from the thread-local list of scopes.
  342. */
  343. ~sentry() BOOST_NOEXCEPT
  344. {
  345. named_scope::pop_scope();
  346. }
  347. BOOST_DELETED_FUNCTION(sentry(sentry const&))
  348. BOOST_DELETED_FUNCTION(sentry& operator= (sentry const&))
  349. private:
  350. scope_entry m_Entry;
  351. };
  352. private:
  353. //! Attribute implementation class
  354. struct BOOST_SYMBOL_VISIBLE impl;
  355. public:
  356. /*!
  357. * Constructor. Creates an attribute.
  358. */
  359. named_scope();
  360. /*!
  361. * Constructor for casting support
  362. */
  363. explicit named_scope(cast_source const& source);
  364. /*!
  365. * The method pushes the scope to the back of the current thread's scope list
  366. *
  367. * \b Throws: Nothing.
  368. */
  369. static void push_scope(scope_entry const& entry) BOOST_NOEXCEPT;
  370. /*!
  371. * The method pops the last pushed scope from the current thread's scope list
  372. *
  373. * \b Throws: Nothing.
  374. */
  375. static void pop_scope() BOOST_NOEXCEPT;
  376. /*!
  377. * \return The current thread's list of scopes
  378. *
  379. * \note The returned reference is only valid until the current thread ends. The scopes in the
  380. * returned container may change if the execution scope is changed (i.e. either \c push_scope
  381. * or \c pop_scope is called). User has to copy the stack if he wants to keep it intact regardless
  382. * of the execution scope.
  383. */
  384. static value_type const& get_scopes();
  385. };
  386. } // namespace attributes
  387. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  388. } // namespace boost
  389. #ifndef BOOST_LOG_DOXYGEN_PASS
  390. #define BOOST_LOG_NAMED_SCOPE_INTERNAL(var, name, file, line, type)\
  391. BOOST_LOG_UNUSED_VARIABLE(::boost::log::attributes::named_scope::sentry, var, (name, file, line, type));
  392. #endif // BOOST_LOG_DOXYGEN_PASS
  393. /*!
  394. * Macro for scope markup. The specified scope name is pushed to the end of the current thread scope list.
  395. */
  396. #define BOOST_LOG_NAMED_SCOPE(name)\
  397. BOOST_LOG_NAMED_SCOPE_INTERNAL(BOOST_LOG_UNIQUE_IDENTIFIER_NAME(_boost_log_named_scope_sentry_), name, __FILE__, __LINE__, ::boost::log::attributes::named_scope_entry::general)
  398. /*!
  399. * Macro for function scope markup. The scope name is constructed with help of compiler and contains the current function signature.
  400. * The scope name is pushed to the end of the current thread scope list.
  401. *
  402. * Not all compilers have support for this macro. The exact form of the scope name may vary from one compiler to another.
  403. */
  404. #define BOOST_LOG_FUNCTION()\
  405. BOOST_LOG_NAMED_SCOPE_INTERNAL(BOOST_LOG_UNIQUE_IDENTIFIER_NAME(_boost_log_named_scope_sentry_), BOOST_CURRENT_FUNCTION, __FILE__, __LINE__, ::boost::log::attributes::named_scope_entry::function)
  406. /*!
  407. * Macro for function scope markup. The scope name is constructed with help of compiler and contains the current function name. It may be shorter than what \c BOOST_LOG_FUNCTION macro produces.
  408. * The scope name is pushed to the end of the current thread scope list.
  409. *
  410. * Not all compilers have support for this macro. The exact form of the scope name may vary from one compiler to another.
  411. */
  412. #if defined(_MSC_VER) || defined(__GNUC__)
  413. #define BOOST_LOG_FUNC() BOOST_LOG_NAMED_SCOPE(__FUNCTION__)
  414. #else
  415. #define BOOST_LOG_FUNC() BOOST_LOG_FUNCTION()
  416. #endif
  417. #include <boost/log/detail/footer.hpp>
  418. #endif // BOOST_LOG_ATTRIBUTES_NAMED_SCOPE_HPP_INCLUDED_