Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:39:19

0001 /*
0002  *          Copyright Andrey Semashev 2007 - 2015.
0003  * Distributed under the Boost Software License, Version 1.0.
0004  *    (See accompanying file LICENSE_1_0.txt or copy at
0005  *          http://www.boost.org/LICENSE_1_0.txt)
0006  */
0007 /*!
0008  * \file   scoped_attribute.hpp
0009  * \author Andrey Semashev
0010  * \date   13.05.2007
0011  *
0012  * The header contains definition of facilities to define scoped attributes.
0013  */
0014 
0015 #ifndef BOOST_LOG_ATTRIBUTES_SCOPED_ATTRIBUTE_HPP_INCLUDED_
0016 #define BOOST_LOG_ATTRIBUTES_SCOPED_ATTRIBUTE_HPP_INCLUDED_
0017 
0018 #include <cstddef>
0019 #include <utility>
0020 #include <boost/move/core.hpp>
0021 #include <boost/move/utility_core.hpp>
0022 #include <boost/core/addressof.hpp>
0023 #include <boost/log/detail/config.hpp>
0024 #include <boost/log/core/core.hpp>
0025 #include <boost/log/sources/basic_logger.hpp>
0026 #include <boost/log/attributes/attribute.hpp>
0027 #include <boost/log/attributes/attribute_set.hpp>
0028 #include <boost/log/attributes/attribute_name.hpp>
0029 #include <boost/log/attributes/constant.hpp>
0030 #include <boost/log/utility/unused_variable.hpp>
0031 #include <boost/log/utility/unique_identifier_name.hpp>
0032 #include <boost/log/detail/header.hpp>
0033 
0034 #ifdef BOOST_HAS_PRAGMA_ONCE
0035 #pragma once
0036 #endif
0037 
0038 namespace boost {
0039 
0040 BOOST_LOG_OPEN_NAMESPACE
0041 
0042 namespace aux {
0043 
0044 //! A base class for all scoped attribute guards
0045 class attribute_scope_guard
0046 {
0047 };
0048 
0049 } // namespace aux
0050 
0051 //! Scoped attribute guard type
0052 typedef aux::attribute_scope_guard const& scoped_attribute;
0053 
0054 namespace aux {
0055 
0056 //! A scoped logger attribute guard
0057 template< typename LoggerT >
0058 class scoped_logger_attribute :
0059     public attribute_scope_guard
0060 {
0061     BOOST_COPYABLE_AND_MOVABLE_ALT(scoped_logger_attribute)
0062 
0063 private:
0064     //! Logger type
0065     typedef LoggerT logger_type;
0066 
0067 private:
0068     //! A reference to the logger
0069     logger_type* m_pLogger;
0070     //! An iterator to the added attribute
0071     attribute_set::iterator m_itAttribute;
0072 
0073 public:
0074     //! Constructor
0075     scoped_logger_attribute(logger_type& l, attribute_name const& name, attribute const& attr) :
0076         m_pLogger(boost::addressof(l))
0077     {
0078         std::pair<
0079             attribute_set::iterator,
0080             bool
0081         > res = l.add_attribute(name, attr);
0082         if (res.second)
0083             m_itAttribute = res.first;
0084         else
0085             m_pLogger = NULL; // if there already is a same-named attribute, don't register anything
0086     }
0087     //! Move constructor
0088     scoped_logger_attribute(BOOST_RV_REF(scoped_logger_attribute) that) BOOST_NOEXCEPT :
0089         m_pLogger(that.m_pLogger),
0090         m_itAttribute(that.m_itAttribute)
0091     {
0092         that.m_pLogger = NULL;
0093     }
0094 
0095     //! Destructor
0096     ~scoped_logger_attribute()
0097     {
0098         if (m_pLogger)
0099             m_pLogger->remove_attribute(m_itAttribute);
0100     }
0101 
0102 #ifndef BOOST_LOG_BROKEN_REFERENCE_FROM_RVALUE_INIT
0103     BOOST_DELETED_FUNCTION(scoped_logger_attribute(scoped_logger_attribute const&))
0104 #else // BOOST_LOG_BROKEN_REFERENCE_FROM_RVALUE_INIT
0105     scoped_logger_attribute(scoped_logger_attribute const& that) BOOST_NOEXCEPT : m_pLogger(that.m_pLogger), m_itAttribute(that.m_itAttribute)
0106     {
0107         const_cast< scoped_logger_attribute& >(that).m_pLogger = NULL;
0108     }
0109 #endif // BOOST_LOG_BROKEN_REFERENCE_FROM_RVALUE_INIT
0110 
0111     BOOST_DELETED_FUNCTION(scoped_logger_attribute& operator= (scoped_logger_attribute const&))
0112 };
0113 
0114 } // namespace aux
0115 
0116 //  Generator helper functions
0117 /*!
0118  * Registers an attribute in the logger
0119  *
0120  * \param l Logger to register the attribute in
0121  * \param name Attribute name
0122  * \param attr The attribute. Must not be NULL.
0123  * \return An unspecified guard object which may be used to initialize a \c scoped_attribute variable.
0124  */
0125 template< typename LoggerT >
0126 BOOST_FORCEINLINE aux::scoped_logger_attribute< LoggerT > add_scoped_logger_attribute(LoggerT& l, attribute_name const& name, attribute const& attr)
0127 {
0128 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
0129     return aux::scoped_logger_attribute< LoggerT >(l, name, attr);
0130 #else
0131     aux::scoped_logger_attribute< LoggerT > guard(l, name, attr);
0132     return boost::move(guard);
0133 #endif
0134 }
0135 
0136 #ifndef BOOST_LOG_DOXYGEN_PASS
0137 
0138 #define BOOST_LOG_SCOPED_LOGGER_ATTR_INTERNAL(logger, attr_name, attr, sentry_var_name)\
0139     BOOST_LOG_UNUSED_VARIABLE(::boost::log::scoped_attribute, sentry_var_name,\
0140         = ::boost::log::add_scoped_logger_attribute(logger, attr_name, (attr)));
0141 
0142 #endif // BOOST_LOG_DOXYGEN_PASS
0143 
0144 //! The macro sets a scoped logger-wide attribute in a more compact way
0145 #define BOOST_LOG_SCOPED_LOGGER_ATTR(logger, attr_name, attr)\
0146     BOOST_LOG_SCOPED_LOGGER_ATTR_INTERNAL(\
0147         logger,\
0148         attr_name,\
0149         attr,\
0150         BOOST_LOG_UNIQUE_IDENTIFIER_NAME(_boost_log_scoped_logger_attr_sentry_))
0151 
0152 //! The macro sets a scoped logger-wide tag in a more compact way
0153 #define BOOST_LOG_SCOPED_LOGGER_TAG(logger, attr_name, attr_value)\
0154     BOOST_LOG_SCOPED_LOGGER_ATTR(logger, attr_name, ::boost::log::attributes::make_constant(attr_value))
0155 
0156 namespace aux {
0157 
0158 //! A scoped thread-specific attribute guard
0159 class scoped_thread_attribute :
0160     public attribute_scope_guard
0161 {
0162     BOOST_COPYABLE_AND_MOVABLE_ALT(scoped_thread_attribute)
0163 
0164 private:
0165     //! A pointer to the logging core
0166     core_ptr m_pCore;
0167     //! An iterator to the added attribute
0168     attribute_set::iterator m_itAttribute;
0169 
0170 public:
0171     //! Constructor
0172     scoped_thread_attribute(attribute_name const& name, attribute const& attr) :
0173         m_pCore(core::get())
0174     {
0175         std::pair<
0176             attribute_set::iterator,
0177             bool
0178         > res = m_pCore->add_thread_attribute(name, attr);
0179         if (res.second)
0180             m_itAttribute = res.first;
0181         else
0182             m_pCore.reset(); // if there already is a same-named attribute, don't register anything
0183     }
0184     //! Move constructor
0185     scoped_thread_attribute(BOOST_RV_REF(scoped_thread_attribute) that) : m_itAttribute(that.m_itAttribute)
0186     {
0187         m_pCore.swap(that.m_pCore);
0188     }
0189 
0190     //! Destructor
0191     ~scoped_thread_attribute()
0192     {
0193         if (!!m_pCore)
0194             m_pCore->remove_thread_attribute(m_itAttribute);
0195     }
0196 
0197 #ifndef BOOST_LOG_BROKEN_REFERENCE_FROM_RVALUE_INIT
0198     BOOST_DELETED_FUNCTION(scoped_thread_attribute(scoped_thread_attribute const&))
0199 #else // BOOST_LOG_BROKEN_REFERENCE_FROM_RVALUE_INIT
0200     scoped_thread_attribute(scoped_thread_attribute const& that) : m_itAttribute(that.m_itAttribute)
0201     {
0202         m_pCore.swap(const_cast< scoped_thread_attribute& >(that).m_pCore);
0203     }
0204 #endif // BOOST_LOG_BROKEN_REFERENCE_FROM_RVALUE_INIT
0205 
0206     BOOST_DELETED_FUNCTION(scoped_thread_attribute& operator= (scoped_thread_attribute const&))
0207 };
0208 
0209 } // namespace aux
0210 
0211 //  Generator helper functions
0212 /*!
0213  * Registers a thread-specific attribute
0214  *
0215  * \param name Attribute name
0216  * \param attr The attribute. Must not be NULL.
0217  * \return An unspecified guard object which may be used to initialize a \c scoped_attribute variable.
0218  */
0219 BOOST_FORCEINLINE aux::scoped_thread_attribute add_scoped_thread_attribute(attribute_name const& name, attribute const& attr)
0220 {
0221 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
0222     return aux::scoped_thread_attribute(name, attr);
0223 #else
0224     aux::scoped_thread_attribute guard(name, attr);
0225     return boost::move(guard);
0226 #endif
0227 }
0228 
0229 #ifndef BOOST_LOG_DOXYGEN_PASS
0230 
0231 #define BOOST_LOG_SCOPED_THREAD_ATTR_INTERNAL(attr_name, attr, sentry_var_name)\
0232     BOOST_LOG_UNUSED_VARIABLE(::boost::log::scoped_attribute, sentry_var_name,\
0233         = ::boost::log::add_scoped_thread_attribute(attr_name, (attr)));
0234 
0235 #endif // BOOST_LOG_DOXYGEN_PASS
0236 
0237 //! The macro sets a scoped thread-wide attribute in a more compact way
0238 #define BOOST_LOG_SCOPED_THREAD_ATTR(attr_name, attr)\
0239     BOOST_LOG_SCOPED_THREAD_ATTR_INTERNAL(\
0240         attr_name,\
0241         attr,\
0242         BOOST_LOG_UNIQUE_IDENTIFIER_NAME(_boost_log_scoped_thread_attr_sentry_))
0243 
0244 //! The macro sets a scoped thread-wide tag in a more compact way
0245 #define BOOST_LOG_SCOPED_THREAD_TAG(attr_name, attr_value)\
0246     BOOST_LOG_SCOPED_THREAD_ATTR(attr_name, ::boost::log::attributes::make_constant(attr_value))
0247 
0248 BOOST_LOG_CLOSE_NAMESPACE // namespace log
0249 
0250 } // namespace boost
0251 
0252 #include <boost/log/detail/footer.hpp>
0253 
0254 #endif // BOOST_LOG_ATTRIBUTES_SCOPED_ATTRIBUTE_HPP_INCLUDED_