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)
9 * \author Andrey Semashev
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.
16 #ifndef BOOST_LOG_ATTRIBUTES_NAMED_SCOPE_HPP_INCLUDED_
17 #define BOOST_LOG_ATTRIBUTES_NAMED_SCOPE_HPP_INCLUDED_
23 #include <boost/log/detail/config.hpp>
24 #include <boost/current_function.hpp>
25 #include <boost/type_traits/conditional.hpp>
26 #include <boost/log/utility/string_literal.hpp>
27 #include <boost/log/utility/unique_identifier_name.hpp>
28 #include <boost/log/utility/unused_variable.hpp>
29 #include <boost/log/attributes/attribute.hpp>
30 #include <boost/log/attributes/attribute_cast.hpp>
31 #include <boost/log/detail/allocator_traits.hpp>
32 #include <boost/log/detail/header.hpp>
34 #ifdef BOOST_HAS_PRAGMA_ONCE
40 BOOST_LOG_OPEN_NAMESPACE
42 namespace attributes {
46 //! Double-linked list node
47 struct named_scope_list_node
49 mutable named_scope_list_node* _m_pPrev;
50 mutable named_scope_list_node* _m_pNext;
52 named_scope_list_node() BOOST_NOEXCEPT { _m_pPrev = _m_pNext = this; }
58 * \brief The structure contains all information about a named scope
60 * The named scope entries are stored as elements of \c basic_named_scope_list container, which
61 * in turn can be acquired either from the \c basic_named_scope attribute value or from a thread-local
64 struct named_scope_entry
66 : public aux::named_scope_list_node
70 * \brief Scope entry type
72 * Describes scope name specifics
76 general, //!< The scope name contains some unstructured string that should not be interpreted by the library
77 function //!< The scope name contains a function signature
81 * The scope name (e.g. a function signature)
83 string_literal scope_name;
85 * The source file name
87 string_literal file_name;
89 * The line number in the source file
98 * Initializing constructor
100 * \post <tt>scope_name == sn && file_name == fn && line == ln</tt>
102 * \b Throws: Nothing.
104 named_scope_entry(string_literal const& sn, string_literal const& fn, unsigned int ln, scope_name_type t = general) BOOST_NOEXCEPT :
114 * \brief The class implements the list of scopes
116 * The scope list provides a read-only access to a doubly-linked list of scopes.
118 class named_scope_list
120 : protected std::allocator< named_scope_entry >
125 typedef std::allocator< named_scope_entry > allocator_type;
128 typedef log::aux::allocator_traits< allocator_type >::value_type value_type;
129 typedef log::aux::allocator_traits< allocator_type >::size_type size_type;
130 typedef log::aux::allocator_traits< allocator_type >::difference_type difference_type;
131 typedef log::aux::allocator_traits< allocator_type >::pointer pointer;
132 typedef log::aux::allocator_traits< allocator_type >::const_pointer const_pointer;
133 typedef value_type& reference;
134 typedef value_type const& const_reference;
136 #ifndef BOOST_LOG_DOXYGEN_PASS
140 #ifndef BOOST_LOG_NO_MEMBER_TEMPLATE_FRIENDS
141 template< bool fConstV > class iter;
142 template< bool fConstV > friend class iter;
144 template< bool fConstV >
147 friend class iter< !fConstV >;
151 typedef named_scope_list::difference_type difference_type;
152 typedef named_scope_list::value_type value_type;
153 typedef typename boost::conditional<
155 named_scope_list::const_reference,
156 named_scope_list::reference
158 typedef typename boost::conditional<
160 named_scope_list::const_pointer,
161 named_scope_list::pointer
163 typedef std::bidirectional_iterator_tag iterator_category;
167 iter() : m_pNode(NULL) {}
168 explicit iter(aux::named_scope_list_node* pNode) : m_pNode(pNode) {}
169 iter(iter< false > const& that) : m_pNode(that.m_pNode) {}
173 iter& operator= (iter< f > const& that)
175 m_pNode = that.m_pNode;
181 bool operator== (iter< f > const& that) const { return (m_pNode == that.m_pNode); }
183 bool operator!= (iter< f > const& that) const { return (m_pNode != that.m_pNode); }
188 m_pNode = m_pNode->_m_pNext;
193 m_pNode = m_pNode->_m_pPrev;
196 iter operator++ (int)
199 m_pNode = m_pNode->_m_pNext;
202 iter operator-- (int)
205 m_pNode = m_pNode->_m_pPrev;
210 pointer operator-> () const { return static_cast< pointer >(m_pNode); }
211 reference operator* () const { return *static_cast< pointer >(m_pNode); }
214 aux::named_scope_list_node* m_pNode;
218 typedef iter< true > const_iterator;
219 typedef iter< false > iterator;
220 typedef std::reverse_iterator< const_iterator > const_reverse_iterator;
221 typedef std::reverse_iterator< iterator > reverse_iterator;
224 //! The root node of the container
225 aux::named_scope_list_node m_RootNode;
226 //! The size of the container
228 //! The flag shows if the contained elements are dynamically allocated
229 bool m_fNeedToDeallocate;
231 #else // BOOST_LOG_DOXYGEN_PASS
234 * A constant iterator to the sequence of scopes. Complies to bidirectional iterator requirements.
236 typedef implementation_defined const_iterator;
238 * An iterator to the sequence of scopes. Complies to bidirectional iterator requirements.
240 typedef implementation_defined iterator;
242 * A constant reverse iterator to the sequence of scopes. Complies to bidirectional iterator requirements.
244 typedef implementation_defined const_reverse_iterator;
246 * A reverse iterator to the sequence of scopes. Complies to bidirectional iterator requirements.
248 typedef implementation_defined reverse_iterator;
250 #endif // BOOST_LOG_DOXYGEN_PASS
254 * Default constructor
256 * \post <tt>empty() == true</tt>
258 named_scope_list() : m_Size(0), m_fNeedToDeallocate(false) {}
262 * \post <tt>std::equal(begin(), end(), that.begin()) == true</tt>
264 BOOST_LOG_API named_scope_list(named_scope_list const& that);
266 * Destructor. Destroys the stored entries.
268 BOOST_LOG_API ~named_scope_list();
271 * Assignment operator
273 * \post <tt>std::equal(begin(), end(), that.begin()) == true</tt>
275 named_scope_list& operator= (named_scope_list const& that)
279 named_scope_list tmp(that);
286 * \return Constant iterator to the first element of the container.
288 const_iterator begin() const { return const_iterator(m_RootNode._m_pNext); }
290 * \return Constant iterator to the after-the-last element of the container.
292 const_iterator end() const { return const_iterator(const_cast< aux::named_scope_list_node* >(&m_RootNode)); }
294 * \return Constant iterator to the last element of the container.
296 const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
298 * \return Constant iterator to the before-the-first element of the container.
300 const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
303 * \return The number of elements in the container
305 size_type size() const { return m_Size; }
307 * \return true if the container is empty and false otherwise
309 bool empty() const { return (m_Size == 0); }
312 * Swaps two instances of the container
314 BOOST_LOG_API void swap(named_scope_list& that);
317 * \return Last pushed scope entry
319 const_reference back() const { return *rbegin(); }
321 * \return First pushed scope entry
323 const_reference front() const { return *begin(); }
326 //! Stream output operator
327 template< typename CharT, typename TraitsT >
328 inline std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, named_scope_list const& sl)
332 named_scope_list::const_iterator it = sl.begin(), end = sl.end();
335 strm << it->scope_name.c_str();
336 for (++it; it != end; ++it)
337 strm << "->" << it->scope_name.c_str();
344 * \brief A class of an attribute that holds stack of named scopes of the current thread
346 * The basic_named_scope attribute is essentially a hook to the thread-specific instance of
347 * scope list. This means that the attribute will generate different values if get_value is
348 * called in different threads. The attribute generates value with stored type
349 * <tt>basic_named_scope_list< CharT ></tt>.
351 * The attribute class can also be used to gain access to the scope stack instance, e.g. to
352 * get its copy or to push or pop a scope entry. However, it is highly not recommended to
353 * maintain scope list manually. Use \c BOOST_LOG_NAMED_SCOPE or \c BOOST_LOG_FUNCTION macros instead.
355 class BOOST_LOG_API named_scope :
359 //! Scope names stack (the attribute value type)
360 typedef named_scope_list value_type;
362 typedef value_type::value_type scope_entry;
364 //! Sentry object class to automatically push and pop scopes
368 * Constructor. Pushes the specified scope to the end of the thread-local list of scopes.
370 * \param sn Scope name.
371 * \param fn File name, in which the scope is located.
372 * \param ln Line number in the file.
374 sentry(string_literal const& sn, string_literal const& fn, unsigned int ln, scope_entry::scope_name_type t = scope_entry::general) BOOST_NOEXCEPT :
375 m_Entry(sn, fn, ln, t)
377 named_scope::push_scope(m_Entry);
381 * Destructor. Removes the last pushed scope from the thread-local list of scopes.
383 ~sentry() BOOST_NOEXCEPT
385 named_scope::pop_scope();
388 BOOST_DELETED_FUNCTION(sentry(sentry const&))
389 BOOST_DELETED_FUNCTION(sentry& operator= (sentry const&))
396 //! Attribute implementation class
397 struct BOOST_SYMBOL_VISIBLE impl;
401 * Constructor. Creates an attribute.
405 * Constructor for casting support
407 explicit named_scope(cast_source const& source);
410 * The method pushes the scope to the back of the current thread's scope list
412 * \b Throws: Nothing.
414 static void push_scope(scope_entry const& entry) BOOST_NOEXCEPT;
416 * The method pops the last pushed scope from the current thread's scope list
418 * \b Throws: Nothing.
420 static void pop_scope() BOOST_NOEXCEPT;
423 * \return The current thread's list of scopes
425 * \note The returned reference is only valid until the current thread ends. The scopes in the
426 * returned container may change if the execution scope is changed (i.e. either \c push_scope
427 * or \c pop_scope is called). User has to copy the stack if he wants to keep it intact regardless
428 * of the execution scope.
430 static value_type const& get_scopes();
433 } // namespace attributes
435 BOOST_LOG_CLOSE_NAMESPACE // namespace log
439 #ifndef BOOST_LOG_DOXYGEN_PASS
441 #define BOOST_LOG_NAMED_SCOPE_INTERNAL(var, name, file, line, type)\
442 BOOST_LOG_UNUSED_VARIABLE(::boost::log::attributes::named_scope::sentry, var, (name, file, line, type));
444 #endif // BOOST_LOG_DOXYGEN_PASS
447 * Macro for scope markup. The specified scope name is pushed to the end of the current thread scope list.
449 #define BOOST_LOG_NAMED_SCOPE(name)\
450 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)
453 * Macro for function scope markup. The scope name is constructed with help of compiler and contains the current function signature.
454 * The scope name is pushed to the end of the current thread scope list.
456 * Not all compilers have support for this macro. The exact form of the scope name may vary from one compiler to another.
458 #define BOOST_LOG_FUNCTION()\
459 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)
462 * 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.
463 * The scope name is pushed to the end of the current thread scope list.
465 * Not all compilers have support for this macro. The exact form of the scope name may vary from one compiler to another.
467 #if defined(_MSC_VER) || defined(__GNUC__)
468 #define BOOST_LOG_FUNC() BOOST_LOG_NAMED_SCOPE(__FUNCTION__)
470 #define BOOST_LOG_FUNC() BOOST_LOG_FUNCTION()
473 #include <boost/log/detail/footer.hpp>
475 #endif // BOOST_LOG_ATTRIBUTES_NAMED_SCOPE_HPP_INCLUDED_