Chris@16: /* Chris@101: * Copyright Andrey Semashev 2007 - 2015. Chris@16: * Distributed under the Boost Software License, Version 1.0. Chris@16: * (See accompanying file LICENSE_1_0.txt or copy at Chris@16: * http://www.boost.org/LICENSE_1_0.txt) Chris@16: */ Chris@16: /*! Chris@16: * \file Chris@16: * \author Andrey Semashev Chris@16: * \date 24.06.2007 Chris@16: * Chris@16: * The header contains implementation of named scope container and an attribute that allows to Chris@16: * put the named scope to log. A number of convenience macros are also provided. Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_LOG_ATTRIBUTES_NAMED_SCOPE_HPP_INCLUDED_ Chris@16: #define BOOST_LOG_ATTRIBUTES_NAMED_SCOPE_HPP_INCLUDED_ Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #ifdef BOOST_HAS_PRAGMA_ONCE Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: BOOST_LOG_OPEN_NAMESPACE Chris@16: Chris@16: namespace attributes { Chris@16: Chris@16: namespace aux { Chris@16: Chris@16: //! Double-linked list node Chris@16: struct named_scope_list_node Chris@16: { Chris@16: mutable named_scope_list_node* _m_pPrev; Chris@16: mutable named_scope_list_node* _m_pNext; Chris@16: Chris@16: named_scope_list_node() BOOST_NOEXCEPT { _m_pPrev = _m_pNext = this; } Chris@16: }; Chris@16: Chris@16: } // namespace aux Chris@16: Chris@16: /*! Chris@16: * \brief The structure contains all information about a named scope Chris@16: * Chris@16: * The named scope entries are stored as elements of \c basic_named_scope_list container, which Chris@16: * in turn can be acquired either from the \c basic_named_scope attribute value or from a thread-local Chris@16: * instance. Chris@16: */ Chris@16: struct named_scope_entry Chris@16: //! \cond Chris@16: : public aux::named_scope_list_node Chris@16: //! \endcond Chris@16: { Chris@16: /*! Chris@101: * \brief Scope entry type Chris@101: * Chris@101: * Describes scope name specifics Chris@101: */ Chris@101: enum scope_name_type Chris@101: { Chris@101: general, //!< The scope name contains some unstructured string that should not be interpreted by the library Chris@101: function //!< The scope name contains a function signature Chris@101: }; Chris@101: Chris@101: /*! Chris@16: * The scope name (e.g. a function signature) Chris@16: */ Chris@16: string_literal scope_name; Chris@16: /*! Chris@16: * The source file name Chris@16: */ Chris@16: string_literal file_name; Chris@16: /*! Chris@16: * The line number in the source file Chris@16: */ Chris@16: unsigned int line; Chris@101: /*! Chris@101: * The scope name type Chris@101: */ Chris@101: scope_name_type type; Chris@16: Chris@16: /*! Chris@16: * Initializing constructor Chris@16: * Chris@16: * \post scope_name == sn && file_name == fn && line == ln Chris@16: * Chris@16: * \b Throws: Nothing. Chris@16: */ Chris@101: named_scope_entry(string_literal const& sn, string_literal const& fn, unsigned int ln, scope_name_type t = general) BOOST_NOEXCEPT : Chris@16: scope_name(sn), Chris@16: file_name(fn), Chris@101: line(ln), Chris@101: type(t) Chris@16: { Chris@16: } Chris@16: }; Chris@16: Chris@16: /*! Chris@16: * \brief The class implements the list of scopes Chris@16: * Chris@16: * The scope list provides a read-only access to a doubly-linked list of scopes. Chris@16: */ Chris@16: class named_scope_list Chris@16: //! \cond Chris@16: : protected std::allocator< named_scope_entry > Chris@16: //! \endcond Chris@16: { Chris@16: public: Chris@16: //! Allocator type Chris@16: typedef std::allocator< named_scope_entry > allocator_type; Chris@16: Chris@16: // Standard types Chris@16: typedef allocator_type::value_type value_type; Chris@16: typedef allocator_type::reference reference; Chris@16: typedef allocator_type::const_reference const_reference; Chris@16: typedef allocator_type::pointer pointer; Chris@16: typedef allocator_type::const_pointer const_pointer; Chris@16: typedef allocator_type::size_type size_type; Chris@16: typedef allocator_type::difference_type difference_type; Chris@16: Chris@16: #ifndef BOOST_LOG_DOXYGEN_PASS Chris@16: Chris@16: protected: Chris@16: //! Iterator class Chris@16: #ifndef BOOST_LOG_NO_MEMBER_TEMPLATE_FRIENDS Chris@16: template< bool fConstV > class iter; Chris@16: template< bool fConstV > friend class iter; Chris@16: #endif Chris@16: template< bool fConstV > Chris@16: class iter Chris@16: { Chris@16: friend class iter< !fConstV >; Chris@16: Chris@16: public: Chris@16: // Standard typedefs Chris@16: typedef named_scope_list::difference_type difference_type; Chris@16: typedef named_scope_list::value_type value_type; Chris@16: typedef typename mpl::if_c< Chris@16: fConstV, Chris@16: named_scope_list::const_reference, Chris@16: named_scope_list::reference Chris@16: >::type reference; Chris@16: typedef typename mpl::if_c< Chris@16: fConstV, Chris@16: named_scope_list::const_pointer, Chris@16: named_scope_list::pointer Chris@16: >::type pointer; Chris@16: typedef std::bidirectional_iterator_tag iterator_category; Chris@16: Chris@16: public: Chris@16: // Constructors Chris@16: iter() : m_pNode(NULL) {} Chris@16: explicit iter(aux::named_scope_list_node* pNode) : m_pNode(pNode) {} Chris@16: iter(iter< false > const& that) : m_pNode(that.m_pNode) {} Chris@16: Chris@16: //! Assignment Chris@16: template< bool f > Chris@16: iter& operator= (iter< f > const& that) Chris@16: { Chris@16: m_pNode = that.m_pNode; Chris@16: return *this; Chris@16: } Chris@16: Chris@16: // Comparison Chris@16: template< bool f > Chris@16: bool operator== (iter< f > const& that) const { return (m_pNode == that.m_pNode); } Chris@16: template< bool f > Chris@16: bool operator!= (iter< f > const& that) const { return (m_pNode != that.m_pNode); } Chris@16: Chris@16: // Modification Chris@16: iter& operator++ () Chris@16: { Chris@16: m_pNode = m_pNode->_m_pNext; Chris@16: return *this; Chris@16: } Chris@16: iter& operator-- () Chris@16: { Chris@16: m_pNode = m_pNode->_m_pPrev; Chris@16: return *this; Chris@16: } Chris@16: iter operator++ (int) Chris@16: { Chris@16: iter tmp(*this); Chris@16: m_pNode = m_pNode->_m_pNext; Chris@16: return tmp; Chris@16: } Chris@16: iter operator-- (int) Chris@16: { Chris@16: iter tmp(*this); Chris@16: m_pNode = m_pNode->_m_pPrev; Chris@16: return tmp; Chris@16: } Chris@16: Chris@16: // Dereferencing Chris@16: pointer operator-> () const { return static_cast< pointer >(m_pNode); } Chris@16: reference operator* () const { return *static_cast< pointer >(m_pNode); } Chris@16: Chris@16: private: Chris@16: aux::named_scope_list_node* m_pNode; Chris@16: }; Chris@16: Chris@16: public: Chris@16: typedef iter< true > const_iterator; Chris@16: typedef iter< false > iterator; Chris@16: typedef std::reverse_iterator< const_iterator > const_reverse_iterator; Chris@16: typedef std::reverse_iterator< iterator > reverse_iterator; Chris@16: Chris@16: protected: Chris@16: //! The root node of the container Chris@16: aux::named_scope_list_node m_RootNode; Chris@16: //! The size of the container Chris@16: size_type m_Size; Chris@16: //! The flag shows if the contained elements are dynamically allocated Chris@16: bool m_fNeedToDeallocate; Chris@16: Chris@16: #else // BOOST_LOG_DOXYGEN_PASS Chris@16: Chris@16: /*! Chris@16: * A constant iterator to the sequence of scopes. Complies to bidirectional iterator requirements. Chris@16: */ Chris@16: typedef implementation_defined const_iterator; Chris@16: /*! Chris@16: * An iterator to the sequence of scopes. Complies to bidirectional iterator requirements. Chris@16: */ Chris@16: typedef implementation_defined iterator; Chris@16: /*! Chris@16: * A constant reverse iterator to the sequence of scopes. Complies to bidirectional iterator requirements. Chris@16: */ Chris@16: typedef implementation_defined const_reverse_iterator; Chris@16: /*! Chris@16: * A reverse iterator to the sequence of scopes. Complies to bidirectional iterator requirements. Chris@16: */ Chris@16: typedef implementation_defined reverse_iterator; Chris@16: Chris@16: #endif // BOOST_LOG_DOXYGEN_PASS Chris@16: Chris@16: public: Chris@16: /*! Chris@16: * Default constructor Chris@16: * Chris@16: * \post empty() == true Chris@16: */ Chris@16: named_scope_list() : m_Size(0), m_fNeedToDeallocate(false) {} Chris@16: /*! Chris@16: * Copy constructor Chris@16: * Chris@16: * \post std::equal(begin(), end(), that.begin()) == true Chris@16: */ Chris@16: BOOST_LOG_API named_scope_list(named_scope_list const& that); Chris@16: /*! Chris@16: * Destructor. Destroys the stored entries. Chris@16: */ Chris@16: BOOST_LOG_API ~named_scope_list(); Chris@16: Chris@16: /*! Chris@16: * Assignment operator Chris@16: * Chris@16: * \post std::equal(begin(), end(), that.begin()) == true Chris@16: */ Chris@16: named_scope_list& operator= (named_scope_list const& that) Chris@16: { Chris@16: if (this != &that) Chris@16: { Chris@16: named_scope_list tmp(that); Chris@16: swap(tmp); Chris@16: } Chris@16: return *this; Chris@16: } Chris@16: Chris@16: /*! Chris@16: * \return Constant iterator to the first element of the container. Chris@16: */ Chris@16: const_iterator begin() const { return const_iterator(m_RootNode._m_pNext); } Chris@16: /*! Chris@16: * \return Constant iterator to the after-the-last element of the container. Chris@16: */ Chris@16: const_iterator end() const { return const_iterator(const_cast< aux::named_scope_list_node* >(&m_RootNode)); } Chris@16: /*! Chris@16: * \return Constant iterator to the last element of the container. Chris@16: */ Chris@16: const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); } Chris@16: /*! Chris@16: * \return Constant iterator to the before-the-first element of the container. Chris@16: */ Chris@16: const_reverse_iterator rend() const { return const_reverse_iterator(begin()); } Chris@16: Chris@16: /*! Chris@16: * \return The number of elements in the container Chris@16: */ Chris@16: size_type size() const { return m_Size; } Chris@16: /*! Chris@16: * \return true if the container is empty and false otherwise Chris@16: */ Chris@16: bool empty() const { return (m_Size == 0); } Chris@16: Chris@16: /*! Chris@16: * Swaps two instances of the container Chris@16: */ Chris@16: BOOST_LOG_API void swap(named_scope_list& that); Chris@16: Chris@16: /*! Chris@16: * \return Last pushed scope entry Chris@16: */ Chris@16: const_reference back() const { return *rbegin(); } Chris@16: /*! Chris@16: * \return First pushed scope entry Chris@16: */ Chris@16: const_reference front() const { return *begin(); } Chris@16: }; Chris@16: Chris@16: //! Stream output operator Chris@16: template< typename CharT, typename TraitsT > Chris@16: inline std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, named_scope_list const& sl) Chris@16: { Chris@16: if (strm.good()) Chris@16: { Chris@16: named_scope_list::const_iterator it = sl.begin(), end = sl.end(); Chris@16: if (it != end) Chris@16: { Chris@16: strm << it->scope_name.c_str(); Chris@16: for (++it; it != end; ++it) Chris@16: strm << "->" << it->scope_name.c_str(); Chris@16: } Chris@16: } Chris@16: return strm; Chris@16: } Chris@16: Chris@16: /*! Chris@16: * \brief A class of an attribute that holds stack of named scopes of the current thread Chris@16: * Chris@16: * The basic_named_scope attribute is essentially a hook to the thread-specific instance of Chris@16: * scope list. This means that the attribute will generate different values if get_value is Chris@16: * called in different threads. The attribute generates value with stored type Chris@16: * basic_named_scope_list< CharT >. Chris@16: * Chris@16: * The attribute class can also be used to gain access to the scope stack instance, e.g. to Chris@16: * get its copy or to push or pop a scope entry. However, it is highly not recommended to Chris@16: * maintain scope list manually. Use \c BOOST_LOG_NAMED_SCOPE or \c BOOST_LOG_FUNCTION macros instead. Chris@16: */ Chris@16: class BOOST_LOG_API named_scope : Chris@16: public attribute Chris@16: { Chris@16: public: Chris@16: //! Scope names stack (the attribute value type) Chris@16: typedef named_scope_list value_type; Chris@16: //! Scope entry Chris@16: typedef value_type::value_type scope_entry; Chris@16: Chris@16: //! Sentry object class to automatically push and pop scopes Chris@16: struct sentry Chris@16: { Chris@16: /*! Chris@16: * Constructor. Pushes the specified scope to the end of the thread-local list of scopes. Chris@16: * Chris@16: * \param sn Scope name. Chris@16: * \param fn File name, in which the scope is located. Chris@16: * \param ln Line number in the file. Chris@16: */ Chris@101: sentry(string_literal const& sn, string_literal const& fn, unsigned int ln, scope_entry::scope_name_type t = scope_entry::general) BOOST_NOEXCEPT : Chris@101: m_Entry(sn, fn, ln, t) Chris@16: { Chris@16: named_scope::push_scope(m_Entry); Chris@16: } Chris@16: Chris@16: /*! Chris@16: * Destructor. Removes the last pushed scope from the thread-local list of scopes. Chris@16: */ Chris@16: ~sentry() BOOST_NOEXCEPT Chris@16: { Chris@16: named_scope::pop_scope(); Chris@16: } Chris@16: Chris@16: BOOST_DELETED_FUNCTION(sentry(sentry const&)) Chris@16: BOOST_DELETED_FUNCTION(sentry& operator= (sentry const&)) Chris@16: Chris@16: private: Chris@16: scope_entry m_Entry; Chris@16: }; Chris@16: Chris@16: private: Chris@16: //! Attribute implementation class Chris@16: struct BOOST_SYMBOL_VISIBLE impl; Chris@16: Chris@16: public: Chris@16: /*! Chris@16: * Constructor. Creates an attribute. Chris@16: */ Chris@16: named_scope(); Chris@16: /*! Chris@16: * Constructor for casting support Chris@16: */ Chris@16: explicit named_scope(cast_source const& source); Chris@16: Chris@16: /*! Chris@16: * The method pushes the scope to the back of the current thread's scope list Chris@16: * Chris@16: * \b Throws: Nothing. Chris@16: */ Chris@16: static void push_scope(scope_entry const& entry) BOOST_NOEXCEPT; Chris@16: /*! Chris@16: * The method pops the last pushed scope from the current thread's scope list Chris@16: * Chris@16: * \b Throws: Nothing. Chris@16: */ Chris@16: static void pop_scope() BOOST_NOEXCEPT; Chris@16: Chris@16: /*! Chris@16: * \return The current thread's list of scopes Chris@16: * Chris@16: * \note The returned reference is only valid until the current thread ends. The scopes in the Chris@16: * returned container may change if the execution scope is changed (i.e. either \c push_scope Chris@16: * or \c pop_scope is called). User has to copy the stack if he wants to keep it intact regardless Chris@16: * of the execution scope. Chris@16: */ Chris@16: static value_type const& get_scopes(); Chris@16: }; Chris@16: Chris@16: } // namespace attributes Chris@16: Chris@16: BOOST_LOG_CLOSE_NAMESPACE // namespace log Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #ifndef BOOST_LOG_DOXYGEN_PASS Chris@16: Chris@101: #define BOOST_LOG_NAMED_SCOPE_INTERNAL(var, name, file, line, type)\ Chris@101: BOOST_LOG_UNUSED_VARIABLE(::boost::log::attributes::named_scope::sentry, var, (name, file, line, type)); Chris@16: Chris@16: #endif // BOOST_LOG_DOXYGEN_PASS Chris@16: Chris@16: /*! Chris@16: * Macro for scope markup. The specified scope name is pushed to the end of the current thread scope list. Chris@16: */ Chris@16: #define BOOST_LOG_NAMED_SCOPE(name)\ Chris@101: 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) Chris@16: Chris@16: /*! Chris@101: * Macro for function scope markup. The scope name is constructed with help of compiler and contains the current function signature. Chris@16: * The scope name is pushed to the end of the current thread scope list. Chris@16: * Chris@16: * Not all compilers have support for this macro. The exact form of the scope name may vary from one compiler to another. Chris@16: */ Chris@101: #define BOOST_LOG_FUNCTION()\ Chris@101: 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) Chris@101: Chris@101: /*! Chris@101: * 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. Chris@101: * The scope name is pushed to the end of the current thread scope list. Chris@101: * Chris@101: * Not all compilers have support for this macro. The exact form of the scope name may vary from one compiler to another. Chris@101: */ Chris@101: #if defined(_MSC_VER) || defined(__GNUC__) Chris@101: #define BOOST_LOG_FUNC() BOOST_LOG_NAMED_SCOPE(__FUNCTION__) Chris@101: #else Chris@101: #define BOOST_LOG_FUNC() BOOST_LOG_FUNCTION() Chris@101: #endif Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_LOG_ATTRIBUTES_NAMED_SCOPE_HPP_INCLUDED_