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 current_thread_id.hpp Chris@16: * \author Andrey Semashev Chris@16: * \date 12.09.2009 Chris@16: * Chris@16: * The header contains implementation of a current thread id attribute Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_LOG_ATTRIBUTES_CURRENT_THREAD_ID_HPP_INCLUDED_ Chris@16: #define BOOST_LOG_ATTRIBUTES_CURRENT_THREAD_ID_HPP_INCLUDED_ Chris@16: Chris@16: #include Chris@16: Chris@16: #ifdef BOOST_HAS_PRAGMA_ONCE Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: #if defined(BOOST_LOG_NO_THREADS) Chris@16: #error Boost.Log: The current_thread_id attribute is only available in multithreaded builds Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: BOOST_LOG_OPEN_NAMESPACE Chris@16: Chris@16: //! Thread identifier type Chris@16: typedef boost::log::aux::thread::id thread_id; Chris@16: Chris@16: namespace attributes { Chris@16: Chris@16: /*! Chris@16: * \brief A class of an attribute that always returns the current thread identifier Chris@16: * Chris@16: * \note This attribute can be registered globally, it will still return the correct Chris@16: * thread identifier, no matter which thread emits the log record. Chris@16: */ Chris@16: class current_thread_id : Chris@16: public attribute Chris@16: { Chris@16: public: Chris@16: //! A held attribute value type Chris@16: typedef thread_id value_type; Chris@16: Chris@16: protected: Chris@16: //! Factory implementation Chris@16: class BOOST_SYMBOL_VISIBLE impl : Chris@16: public attribute_value::impl Chris@16: { Chris@16: public: Chris@16: bool dispatch(type_dispatcher& dispatcher) Chris@16: { Chris@16: type_dispatcher::callback< value_type > callback = Chris@16: dispatcher.get_callback< value_type >(); Chris@16: if (callback) Chris@16: { Chris@16: callback(boost::log::aux::this_thread::get_id()); Chris@16: return true; Chris@16: } Chris@16: else Chris@16: return false; Chris@16: } Chris@16: Chris@16: intrusive_ptr< attribute_value::impl > detach_from_thread() Chris@16: { Chris@16: typedef attribute_value_impl< value_type > detached_value; Chris@16: return new detached_value(boost::log::aux::this_thread::get_id()); Chris@16: } Chris@16: Chris@16: type_info_wrapper get_type() const { return type_info_wrapper(typeid(value_type)); } Chris@16: }; Chris@16: Chris@16: public: Chris@16: /*! Chris@16: * Default constructor Chris@16: */ Chris@16: current_thread_id() : attribute(new impl()) Chris@16: { Chris@16: } Chris@16: /*! Chris@16: * Constructor for casting support Chris@16: */ Chris@16: explicit current_thread_id(cast_source const& source) : Chris@16: attribute(source.as< impl >()) Chris@16: { Chris@16: } 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: #include Chris@16: Chris@16: #endif // BOOST_LOG_ATTRIBUTES_CURRENT_THREAD_ID_HPP_INCLUDED_