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 
16 #ifndef BOOST_LOG_ATTRIBUTES_NAMED_SCOPE_HPP_INCLUDED_
17 #define BOOST_LOG_ATTRIBUTES_NAMED_SCOPE_HPP_INCLUDED_
18 
19 #include <ostream>
20 #include <memory>
21 #include <iterator>
22 #include <cstddef>
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>
32 
33 #ifdef BOOST_HAS_PRAGMA_ONCE
34 #pragma once
35 #endif
36 
37 namespace boost {
38 
39 BOOST_LOG_OPEN_NAMESPACE
40 
41 namespace attributes {
42 
43 namespace aux {
44 
45     //! Double-linked list node
46     struct named_scope_list_node
47     {
48         mutable named_scope_list_node* _m_pPrev;
49         mutable named_scope_list_node* _m_pNext;
50 
named_scope_list_nodeboost::attributes::aux::named_scope_list_node51         named_scope_list_node() BOOST_NOEXCEPT { _m_pPrev = _m_pNext = this; }
52     };
53 
54 } // namespace aux
55 
56 /*!
57  * \brief The structure contains all information about a named scope
58  *
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
61  * instance.
62  */
63 struct named_scope_entry
64     //! \cond
65     : public aux::named_scope_list_node
66     //! \endcond
67 {
68     /*!
69      * \brief Scope entry type
70      *
71      * Describes scope name specifics
72      */
73     enum scope_name_type
74     {
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
77     };
78 
79     /*!
80      * The scope name (e.g. a function signature)
81      */
82     string_literal scope_name;
83     /*!
84      * The source file name
85      */
86     string_literal file_name;
87     /*!
88      * The line number in the source file
89      */
90     unsigned int line;
91     /*!
92      * The scope name type
93      */
94     scope_name_type type;
95 
96     /*!
97      * Initializing constructor
98      *
99      * \post <tt>scope_name == sn && file_name == fn && line == ln</tt>
100      *
101      * \b Throws: Nothing.
102      */
named_scope_entryboost::attributes::named_scope_entry103     named_scope_entry(string_literal const& sn, string_literal const& fn, unsigned int ln, scope_name_type t = general) BOOST_NOEXCEPT :
104         scope_name(sn),
105         file_name(fn),
106         line(ln),
107         type(t)
108     {
109     }
110 };
111 
112 /*!
113  * \brief The class implements the list of scopes
114  *
115  * The scope list provides a read-only access to a doubly-linked list of scopes.
116  */
117 class named_scope_list
118     //! \cond
119     : protected std::allocator< named_scope_entry >
120     //! \endcond
121 {
122 public:
123     //! Allocator type
124     typedef std::allocator< named_scope_entry > allocator_type;
125 
126     //  Standard types
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;
134 
135 #ifndef BOOST_LOG_DOXYGEN_PASS
136 
137 protected:
138     //! Iterator class
139 #ifndef BOOST_LOG_NO_MEMBER_TEMPLATE_FRIENDS
140     template< bool fConstV > class iter;
141     template< bool fConstV > friend class iter;
142 #endif
143     template< bool fConstV >
144     class iter
145     {
146         friend class iter< !fConstV >;
147 
148     public:
149         //  Standard typedefs
150         typedef named_scope_list::difference_type difference_type;
151         typedef named_scope_list::value_type value_type;
152         typedef typename mpl::if_c<
153             fConstV,
154             named_scope_list::const_reference,
155             named_scope_list::reference
156         >::type reference;
157         typedef typename mpl::if_c<
158             fConstV,
159             named_scope_list::const_pointer,
160             named_scope_list::pointer
161         >::type pointer;
162         typedef std::bidirectional_iterator_tag iterator_category;
163 
164     public:
165         //  Constructors
iter()166         iter() : m_pNode(NULL) {}
iter(aux::named_scope_list_node * pNode)167         explicit iter(aux::named_scope_list_node* pNode) : m_pNode(pNode) {}
iter(iter<false> const & that)168         iter(iter< false > const& that) : m_pNode(that.m_pNode) {}
169 
170         //! Assignment
171         template< bool f >
operator =(iter<f> const & that)172         iter& operator= (iter< f > const& that)
173         {
174             m_pNode = that.m_pNode;
175             return *this;
176         }
177 
178         //  Comparison
179         template< bool f >
operator ==(iter<f> const & that) const180         bool operator== (iter< f > const& that) const { return (m_pNode == that.m_pNode); }
181         template< bool f >
operator !=(iter<f> const & that) const182         bool operator!= (iter< f > const& that) const { return (m_pNode != that.m_pNode); }
183 
184         //  Modification
operator ++()185         iter& operator++ ()
186         {
187             m_pNode = m_pNode->_m_pNext;
188             return *this;
189         }
operator --()190         iter& operator-- ()
191         {
192             m_pNode = m_pNode->_m_pPrev;
193             return *this;
194         }
operator ++(int)195         iter operator++ (int)
196         {
197             iter tmp(*this);
198             m_pNode = m_pNode->_m_pNext;
199             return tmp;
200         }
operator --(int)201         iter operator-- (int)
202         {
203             iter tmp(*this);
204             m_pNode = m_pNode->_m_pPrev;
205             return tmp;
206         }
207 
208         //  Dereferencing
operator ->() const209         pointer operator-> () const { return static_cast< pointer >(m_pNode); }
operator *() const210         reference operator* () const { return *static_cast< pointer >(m_pNode); }
211 
212     private:
213         aux::named_scope_list_node* m_pNode;
214     };
215 
216 public:
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;
221 
222 protected:
223     //! The root node of the container
224     aux::named_scope_list_node m_RootNode;
225     //! The size of the container
226     size_type m_Size;
227     //! The flag shows if the contained elements are dynamically allocated
228     bool m_fNeedToDeallocate;
229 
230 #else // BOOST_LOG_DOXYGEN_PASS
231 
232     /*!
233      * A constant iterator to the sequence of scopes. Complies to bidirectional iterator requirements.
234      */
235     typedef implementation_defined const_iterator;
236     /*!
237      * An iterator to the sequence of scopes. Complies to bidirectional iterator requirements.
238      */
239     typedef implementation_defined iterator;
240     /*!
241      * A constant reverse iterator to the sequence of scopes. Complies to bidirectional iterator requirements.
242      */
243     typedef implementation_defined const_reverse_iterator;
244     /*!
245      * A reverse iterator to the sequence of scopes. Complies to bidirectional iterator requirements.
246      */
247     typedef implementation_defined reverse_iterator;
248 
249 #endif // BOOST_LOG_DOXYGEN_PASS
250 
251 public:
252     /*!
253      * Default constructor
254      *
255      * \post <tt>empty() == true</tt>
256      */
named_scope_list()257     named_scope_list() : m_Size(0), m_fNeedToDeallocate(false) {}
258     /*!
259      * Copy constructor
260      *
261      * \post <tt>std::equal(begin(), end(), that.begin()) == true</tt>
262      */
263     BOOST_LOG_API named_scope_list(named_scope_list const& that);
264     /*!
265      * Destructor. Destroys the stored entries.
266      */
267     BOOST_LOG_API ~named_scope_list();
268 
269     /*!
270      * Assignment operator
271      *
272      * \post <tt>std::equal(begin(), end(), that.begin()) == true</tt>
273      */
operator =(named_scope_list const & that)274     named_scope_list& operator= (named_scope_list const& that)
275     {
276         if (this != &that)
277         {
278             named_scope_list tmp(that);
279             swap(tmp);
280         }
281         return *this;
282     }
283 
284     /*!
285      * \return Constant iterator to the first element of the container.
286      */
begin() const287     const_iterator begin() const { return const_iterator(m_RootNode._m_pNext); }
288     /*!
289      * \return Constant iterator to the after-the-last element of the container.
290      */
end() const291     const_iterator end() const { return const_iterator(const_cast< aux::named_scope_list_node* >(&m_RootNode)); }
292     /*!
293      * \return Constant iterator to the last element of the container.
294      */
rbegin() const295     const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
296     /*!
297      * \return Constant iterator to the before-the-first element of the container.
298      */
rend() const299     const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
300 
301     /*!
302      * \return The number of elements in the container
303      */
size() const304     size_type size() const { return m_Size; }
305     /*!
306      * \return true if the container is empty and false otherwise
307      */
empty() const308     bool empty() const { return (m_Size == 0); }
309 
310     /*!
311      * Swaps two instances of the container
312      */
313     BOOST_LOG_API void swap(named_scope_list& that);
314 
315     /*!
316      * \return Last pushed scope entry
317      */
back() const318     const_reference back() const { return *rbegin(); }
319     /*!
320      * \return First pushed scope entry
321      */
front() const322     const_reference front() const { return *begin(); }
323 };
324 
325 //! Stream output operator
326 template< typename CharT, typename TraitsT >
operator <<(std::basic_ostream<CharT,TraitsT> & strm,named_scope_list const & sl)327 inline std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, named_scope_list const& sl)
328 {
329     if (strm.good())
330     {
331         named_scope_list::const_iterator it = sl.begin(), end = sl.end();
332         if (it != end)
333         {
334             strm << it->scope_name.c_str();
335             for (++it; it != end; ++it)
336                 strm << "->" << it->scope_name.c_str();
337         }
338     }
339     return strm;
340 }
341 
342 /*!
343  * \brief A class of an attribute that holds stack of named scopes of the current thread
344  *
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>.
349  *
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.
353  */
354 class BOOST_LOG_API named_scope :
355     public attribute
356 {
357 public:
358     //! Scope names stack (the attribute value type)
359     typedef named_scope_list value_type;
360     //! Scope entry
361     typedef value_type::value_type scope_entry;
362 
363     //! Sentry object class to automatically push and pop scopes
364     struct sentry
365     {
366         /*!
367          * Constructor. Pushes the specified scope to the end of the thread-local list of scopes.
368          *
369          * \param sn Scope name.
370          * \param fn File name, in which the scope is located.
371          * \param ln Line number in the file.
372          */
sentryboost::attributes::named_scope::sentry373         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)
375         {
376             named_scope::push_scope(m_Entry);
377         }
378 
379         /*!
380          * Destructor. Removes the last pushed scope from the thread-local list of scopes.
381          */
~sentryboost::attributes::named_scope::sentry382         ~sentry() BOOST_NOEXCEPT
383         {
384             named_scope::pop_scope();
385         }
386 
387         BOOST_DELETED_FUNCTION(sentry(sentry const&))
388         BOOST_DELETED_FUNCTION(sentry& operator= (sentry const&))
389 
390     private:
391         scope_entry m_Entry;
392     };
393 
394 private:
395     //! Attribute implementation class
396     struct BOOST_SYMBOL_VISIBLE impl;
397 
398 public:
399     /*!
400      * Constructor. Creates an attribute.
401      */
402     named_scope();
403     /*!
404      * Constructor for casting support
405      */
406     explicit named_scope(cast_source const& source);
407 
408     /*!
409      * The method pushes the scope to the back of the current thread's scope list
410      *
411      * \b Throws: Nothing.
412      */
413     static void push_scope(scope_entry const& entry) BOOST_NOEXCEPT;
414     /*!
415      * The method pops the last pushed scope from the current thread's scope list
416      *
417      * \b Throws: Nothing.
418      */
419     static void pop_scope() BOOST_NOEXCEPT;
420 
421     /*!
422      *  \return The current thread's list of scopes
423      *
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.
428      */
429     static value_type const& get_scopes();
430 };
431 
432 } // namespace attributes
433 
434 BOOST_LOG_CLOSE_NAMESPACE // namespace log
435 
436 } // namespace boost
437 
438 #ifndef BOOST_LOG_DOXYGEN_PASS
439 
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));
442 
443 #endif // BOOST_LOG_DOXYGEN_PASS
444 
445 /*!
446  * Macro for scope markup. The specified scope name is pushed to the end of the current thread scope list.
447  */
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)
450 
451 /*!
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.
454  *
455  * Not all compilers have support for this macro. The exact form of the scope name may vary from one compiler to another.
456  */
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)
459 
460 /*!
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.
463  *
464  * Not all compilers have support for this macro. The exact form of the scope name may vary from one compiler to another.
465  */
466 #if defined(_MSC_VER) || defined(__GNUC__)
467 #define BOOST_LOG_FUNC() BOOST_LOG_NAMED_SCOPE(__FUNCTION__)
468 #else
469 #define BOOST_LOG_FUNC() BOOST_LOG_FUNCTION()
470 #endif
471 
472 #include <boost/log/detail/footer.hpp>
473 
474 #endif // BOOST_LOG_ATTRIBUTES_NAMED_SCOPE_HPP_INCLUDED_
475