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)
8 * \file current_thread_id.hpp
9 * \author Andrey Semashev
12 * The header contains implementation of a current thread id attribute
15 #ifndef BOOST_LOG_ATTRIBUTES_CURRENT_THREAD_ID_HPP_INCLUDED_
16 #define BOOST_LOG_ATTRIBUTES_CURRENT_THREAD_ID_HPP_INCLUDED_
18 #include <boost/log/detail/config.hpp>
20 #ifdef BOOST_HAS_PRAGMA_ONCE
24 #if defined(BOOST_LOG_NO_THREADS)
25 #error Boost.Log: The current_thread_id attribute is only available in multithreaded builds
28 #include <boost/smart_ptr/intrusive_ptr.hpp>
29 #include <boost/log/detail/thread_id.hpp>
30 #include <boost/log/attributes/attribute.hpp>
31 #include <boost/log/attributes/attribute_cast.hpp>
32 #include <boost/log/attributes/attribute_value_impl.hpp>
33 #include <boost/log/detail/header.hpp>
37 BOOST_LOG_OPEN_NAMESPACE
39 //! Thread identifier type
40 typedef boost::log::aux::thread::id thread_id;
42 namespace attributes {
45 * \brief A class of an attribute that always returns the current thread identifier
47 * \note This attribute can be registered globally, it will still return the correct
48 * thread identifier, no matter which thread emits the log record.
50 class current_thread_id :
54 //! A held attribute value type
55 typedef thread_id value_type;
58 //! Factory implementation
59 class BOOST_SYMBOL_VISIBLE impl :
60 public attribute_value::impl
63 bool dispatch(type_dispatcher& dispatcher)
65 type_dispatcher::callback< value_type > callback =
66 dispatcher.get_callback< value_type >();
69 callback(boost::log::aux::this_thread::get_id());
76 intrusive_ptr< attribute_value::impl > detach_from_thread()
78 typedef attribute_value_impl< value_type > detached_value;
79 return new detached_value(boost::log::aux::this_thread::get_id());
82 typeindex::type_index get_type() const { return typeindex::type_id< value_type >(); }
89 current_thread_id() : attribute(new impl())
93 * Constructor for casting support
95 explicit current_thread_id(cast_source const& source) :
96 attribute(source.as< impl >())
101 } // namespace attributes
103 BOOST_LOG_CLOSE_NAMESPACE // namespace log
107 #include <boost/log/detail/footer.hpp>
109 #endif // BOOST_LOG_ATTRIBUTES_CURRENT_THREAD_ID_HPP_INCLUDED_