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/mpl/if.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/header.hpp>
33 #ifdef BOOST_HAS_PRAGMA_ONCE
39 BOOST_LOG_OPEN_NAMESPACE
41 namespace attributes {
45 //! Double-linked list node
46 struct named_scope_list_node
48 mutable named_scope_list_node* _m_pPrev;
49 mutable named_scope_list_node* _m_pNext;
51 named_scope_list_node() BOOST_NOEXCEPT { _m_pPrev = _m_pNext = this; }
57 * \brief The structure contains all information about a named scope
59 * The named scope entries are stored as elements of \c basic_named_scope_list container, which
60 * in turn can be acquired either from the \c basic_named_scope attribute value or from a thread-local
63 struct named_scope_entry
65 : public aux::named_scope_list_node
69 * \brief Scope entry type
71 * Describes scope name specifics
75 general, //!< The scope name contains some unstructured string that should not be interpreted by the library
76 function //!< The scope name contains a function signature
80 * The scope name (e.g. a function signature)
82 string_literal scope_name;
84 * The source file name
86 string_literal file_name;
88 * The line number in the source file
97 * Initializing constructor
99 * \post <tt>scope_name == sn && file_name == fn && line == ln</tt>
101 * \b Throws: Nothing.
103 named_scope_entry(string_literal const& sn, string_literal const& fn, unsigned int ln, scope_name_type t = general) BOOST_NOEXCEPT :
113 * \brief The class implements the list of scopes
115 * The scope list provides a read-only access to a doubly-linked list of scopes.
117 class named_scope_list
119 : protected std::allocator< named_scope_entry >
124 typedef std::allocator< named_scope_entry > allocator_type;
127 typedef allocator_type::value_type value_type;
128 typedef allocator_type::reference reference;
129 typedef allocator_type::const_reference const_reference;
130 typedef allocator_type::pointer pointer;
131 typedef allocator_type::const_pointer const_pointer;
132 typedef allocator_type::size_type size_type;
133 typedef allocator_type::difference_type difference_type;
135 #ifndef BOOST_LOG_DOXYGEN_PASS
139 #ifndef BOOST_LOG_NO_MEMBER_TEMPLATE_FRIENDS
140 template< bool fConstV > class iter;
141 template< bool fConstV > friend class iter;
143 template< bool fConstV >
146 friend class iter< !fConstV >;
150 typedef named_scope_list::difference_type difference_type;
151 typedef named_scope_list::value_type value_type;
152 typedef typename mpl::if_c<
154 named_scope_list::const_reference,
155 named_scope_list::reference
157 typedef typename mpl::if_c<
159 named_scope_list::const_pointer,
160 named_scope_list::pointer
162 typedef std::bidirectional_iterator_tag iterator_category;
166 iter() : m_pNode(NULL) {}
167 explicit iter(aux::named_scope_list_node* pNode) : m_pNode(pNode) {}
168 iter(iter< false > const& that) : m_pNode(that.m_pNode) {}
172 iter& operator= (iter< f > const& that)
174 m_pNode = that.m_pNode;
180 bool operator== (iter< f > const& that) const { return (m_pNode == that.m_pNode); }
182 bool operator!= (iter< f > const& that) const { return (m_pNode != that.m_pNode); }
187 m_pNode = m_pNode->_m_pNext;
192 m_pNode = m_pNode->_m_pPrev;
195 iter operator++ (int)
198 m_pNode = m_pNode->_m_pNext;
201 iter operator-- (int)
204 m_pNode = m_pNode->_m_pPrev;
209 pointer operator-> () const { return static_cast< pointer >(m_pNode); }
210 reference operator* () const { return *static_cast< pointer >(m_pNode); }
213 aux::named_scope_list_node* m_pNode;
217 typedef iter< true > const_iterator;
218 typedef iter< false > iterator;
219 typedef std::reverse_iterator< const_iterator > const_reverse_iterator;
220 typedef std::reverse_iterator< iterator > reverse_iterator;
223 //! The root node of the container
224 aux::named_scope_list_node m_RootNode;
225 //! The size of the container
227 //! The flag shows if the contained elements are dynamically allocated
228 bool m_fNeedToDeallocate;
230 #else // BOOST_LOG_DOXYGEN_PASS
233 * A constant iterator to the sequence of scopes. Complies to bidirectional iterator requirements.
235 typedef implementation_defined const_iterator;
237 * An iterator to the sequence of scopes. Complies to bidirectional iterator requirements.
239 typedef implementation_defined iterator;
241 * A constant reverse iterator to the sequence of scopes. Complies to bidirectional iterator requirements.
243 typedef implementation_defined const_reverse_iterator;
245 * A reverse iterator to the sequence of scopes. Complies to bidirectional iterator requirements.
247 typedef implementation_defined reverse_iterator;
249 #endif // BOOST_LOG_DOXYGEN_PASS
253 * Default constructor
255 * \post <tt>empty() == true</tt>
257 named_scope_list() : m_Size(0), m_fNeedToDeallocate(false) {}
261 * \post <tt>std::equal(begin(), end(), that.begin()) == true</tt>
263 BOOST_LOG_API named_scope_list(named_scope_list const& that);
265 * Destructor. Destroys the stored entries.
267 BOOST_LOG_API ~named_scope_list();
270 * Assignment operator
272 * \post <tt>std::equal(begin(), end(), that.begin()) == true</tt>
274 named_scope_list& operator= (named_scope_list const& that)
278 named_scope_list tmp(that);
285 * \return Constant iterator to the first element of the container.
287 const_iterator begin() const { return const_iterator(m_RootNode._m_pNext); }
289 * \return Constant iterator to the after-the-last element of the container.
291 const_iterator end() const { return const_iterator(const_cast< aux::named_scope_list_node* >(&m_RootNode)); }
293 * \return Constant iterator to the last element of the container.
295 const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
297 * \return Constant iterator to the before-the-first element of the container.
299 const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
302 * \return The number of elements in the container
304 size_type size() const { return m_Size; }
306 * \return true if the container is empty and false otherwise
308 bool empty() const { return (m_Size == 0); }
311 * Swaps two instances of the container
313 BOOST_LOG_API void swap(named_scope_list& that);
316 * \return Last pushed scope entry
318 const_reference back() const { return *rbegin(); }
320 * \return First pushed scope entry
322 const_reference front() const { return *begin(); }
325 //! Stream output operator
326 template< typename CharT, typename TraitsT >
327 inline std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, named_scope_list const& sl)
331 named_scope_list::const_iterator it = sl.begin(), end = sl.end();
334 strm << it->scope_name.c_str();
335 for (++it; it != end; ++it)
336 strm << "->" << it->scope_name.c_str();
343 * \brief A class of an attribute that holds stack of named scopes of the current thread
345 * The basic_named_scope attribute is essentially a hook to the thread-specific instance of
346 * scope list. This means that the attribute will generate different values if get_value is
347 * called in different threads. The attribute generates value with stored type
348 * <tt>basic_named_scope_list< CharT ></tt>.
350 * The attribute class can also be used to gain access to the scope stack instance, e.g. to
351 * get its copy or to push or pop a scope entry. However, it is highly not recommended to
352 * maintain scope list manually. Use \c BOOST_LOG_NAMED_SCOPE or \c BOOST_LOG_FUNCTION macros instead.
354 class BOOST_LOG_API named_scope :
358 //! Scope names stack (the attribute value type)
359 typedef named_scope_list value_type;
361 typedef value_type::value_type scope_entry;
363 //! Sentry object class to automatically push and pop scopes
367 * Constructor. Pushes the specified scope to the end of the thread-local list of scopes.
369 * \param sn Scope name.
370 * \param fn File name, in which the scope is located.
371 * \param ln Line number in the file.
373 sentry(string_literal const& sn, string_literal const& fn, unsigned int ln, scope_entry::scope_name_type t = scope_entry::general) BOOST_NOEXCEPT :
374 m_Entry(sn, fn, ln, t)
376 named_scope::push_scope(m_Entry);
380 * Destructor. Removes the last pushed scope from the thread-local list of scopes.
382 ~sentry() BOOST_NOEXCEPT
384 named_scope::pop_scope();
387 BOOST_DELETED_FUNCTION(sentry(sentry const&))
388 BOOST_DELETED_FUNCTION(sentry& operator= (sentry const&))
395 //! Attribute implementation class
396 struct BOOST_SYMBOL_VISIBLE impl;
400 * Constructor. Creates an attribute.
404 * Constructor for casting support
406 explicit named_scope(cast_source const& source);
409 * The method pushes the scope to the back of the current thread's scope list
411 * \b Throws: Nothing.
413 static void push_scope(scope_entry const& entry) BOOST_NOEXCEPT;
415 * The method pops the last pushed scope from the current thread's scope list
417 * \b Throws: Nothing.
419 static void pop_scope() BOOST_NOEXCEPT;
422 * \return The current thread's list of scopes
424 * \note The returned reference is only valid until the current thread ends. The scopes in the
425 * returned container may change if the execution scope is changed (i.e. either \c push_scope
426 * or \c pop_scope is called). User has to copy the stack if he wants to keep it intact regardless
427 * of the execution scope.
429 static value_type const& get_scopes();
432 } // namespace attributes
434 BOOST_LOG_CLOSE_NAMESPACE // namespace log
438 #ifndef BOOST_LOG_DOXYGEN_PASS
440 #define BOOST_LOG_NAMED_SCOPE_INTERNAL(var, name, file, line, type)\
441 BOOST_LOG_UNUSED_VARIABLE(::boost::log::attributes::named_scope::sentry, var, (name, file, line, type));
443 #endif // BOOST_LOG_DOXYGEN_PASS
446 * Macro for scope markup. The specified scope name is pushed to the end of the current thread scope list.
448 #define BOOST_LOG_NAMED_SCOPE(name)\
449 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)
452 * Macro for function scope markup. The scope name is constructed with help of compiler and contains the current function signature.
453 * The scope name is pushed to the end of the current thread scope list.
455 * Not all compilers have support for this macro. The exact form of the scope name may vary from one compiler to another.
457 #define BOOST_LOG_FUNCTION()\
458 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)
461 * 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.
462 * The scope name is pushed to the end of the current thread scope list.
464 * Not all compilers have support for this macro. The exact form of the scope name may vary from one compiler to another.
466 #if defined(_MSC_VER) || defined(__GNUC__)
467 #define BOOST_LOG_FUNC() BOOST_LOG_NAMED_SCOPE(__FUNCTION__)
469 #define BOOST_LOG_FUNC() BOOST_LOG_FUNCTION()
472 #include <boost/log/detail/footer.hpp>
474 #endif // BOOST_LOG_ATTRIBUTES_NAMED_SCOPE_HPP_INCLUDED_