Chris@16
|
1 /*
|
Chris@101
|
2 * Copyright Andrey Semashev 2007 - 2015.
|
Chris@16
|
3 * Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
4 * (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
5 * http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6 */
|
Chris@16
|
7 /*!
|
Chris@16
|
8 * \file current_thread_id.hpp
|
Chris@16
|
9 * \author Andrey Semashev
|
Chris@16
|
10 * \date 12.09.2009
|
Chris@16
|
11 *
|
Chris@16
|
12 * The header contains implementation of a current thread id attribute
|
Chris@16
|
13 */
|
Chris@16
|
14
|
Chris@16
|
15 #ifndef BOOST_LOG_ATTRIBUTES_CURRENT_THREAD_ID_HPP_INCLUDED_
|
Chris@16
|
16 #define BOOST_LOG_ATTRIBUTES_CURRENT_THREAD_ID_HPP_INCLUDED_
|
Chris@16
|
17
|
Chris@16
|
18 #include <boost/log/detail/config.hpp>
|
Chris@16
|
19
|
Chris@16
|
20 #ifdef BOOST_HAS_PRAGMA_ONCE
|
Chris@16
|
21 #pragma once
|
Chris@16
|
22 #endif
|
Chris@16
|
23
|
Chris@16
|
24 #if defined(BOOST_LOG_NO_THREADS)
|
Chris@16
|
25 #error Boost.Log: The current_thread_id attribute is only available in multithreaded builds
|
Chris@16
|
26 #endif
|
Chris@16
|
27
|
Chris@16
|
28 #include <boost/smart_ptr/intrusive_ptr.hpp>
|
Chris@16
|
29 #include <boost/log/detail/thread_id.hpp>
|
Chris@16
|
30 #include <boost/log/attributes/attribute.hpp>
|
Chris@16
|
31 #include <boost/log/attributes/attribute_cast.hpp>
|
Chris@16
|
32 #include <boost/log/attributes/attribute_value_impl.hpp>
|
Chris@16
|
33 #include <boost/log/detail/header.hpp>
|
Chris@16
|
34
|
Chris@16
|
35 namespace boost {
|
Chris@16
|
36
|
Chris@16
|
37 BOOST_LOG_OPEN_NAMESPACE
|
Chris@16
|
38
|
Chris@16
|
39 //! Thread identifier type
|
Chris@16
|
40 typedef boost::log::aux::thread::id thread_id;
|
Chris@16
|
41
|
Chris@16
|
42 namespace attributes {
|
Chris@16
|
43
|
Chris@16
|
44 /*!
|
Chris@16
|
45 * \brief A class of an attribute that always returns the current thread identifier
|
Chris@16
|
46 *
|
Chris@16
|
47 * \note This attribute can be registered globally, it will still return the correct
|
Chris@16
|
48 * thread identifier, no matter which thread emits the log record.
|
Chris@16
|
49 */
|
Chris@16
|
50 class current_thread_id :
|
Chris@16
|
51 public attribute
|
Chris@16
|
52 {
|
Chris@16
|
53 public:
|
Chris@16
|
54 //! A held attribute value type
|
Chris@16
|
55 typedef thread_id value_type;
|
Chris@16
|
56
|
Chris@16
|
57 protected:
|
Chris@16
|
58 //! Factory implementation
|
Chris@16
|
59 class BOOST_SYMBOL_VISIBLE impl :
|
Chris@16
|
60 public attribute_value::impl
|
Chris@16
|
61 {
|
Chris@16
|
62 public:
|
Chris@16
|
63 bool dispatch(type_dispatcher& dispatcher)
|
Chris@16
|
64 {
|
Chris@16
|
65 type_dispatcher::callback< value_type > callback =
|
Chris@16
|
66 dispatcher.get_callback< value_type >();
|
Chris@16
|
67 if (callback)
|
Chris@16
|
68 {
|
Chris@16
|
69 callback(boost::log::aux::this_thread::get_id());
|
Chris@16
|
70 return true;
|
Chris@16
|
71 }
|
Chris@16
|
72 else
|
Chris@16
|
73 return false;
|
Chris@16
|
74 }
|
Chris@16
|
75
|
Chris@16
|
76 intrusive_ptr< attribute_value::impl > detach_from_thread()
|
Chris@16
|
77 {
|
Chris@16
|
78 typedef attribute_value_impl< value_type > detached_value;
|
Chris@16
|
79 return new detached_value(boost::log::aux::this_thread::get_id());
|
Chris@16
|
80 }
|
Chris@16
|
81
|
Chris@16
|
82 type_info_wrapper get_type() const { return type_info_wrapper(typeid(value_type)); }
|
Chris@16
|
83 };
|
Chris@16
|
84
|
Chris@16
|
85 public:
|
Chris@16
|
86 /*!
|
Chris@16
|
87 * Default constructor
|
Chris@16
|
88 */
|
Chris@16
|
89 current_thread_id() : attribute(new impl())
|
Chris@16
|
90 {
|
Chris@16
|
91 }
|
Chris@16
|
92 /*!
|
Chris@16
|
93 * Constructor for casting support
|
Chris@16
|
94 */
|
Chris@16
|
95 explicit current_thread_id(cast_source const& source) :
|
Chris@16
|
96 attribute(source.as< impl >())
|
Chris@16
|
97 {
|
Chris@16
|
98 }
|
Chris@16
|
99 };
|
Chris@16
|
100
|
Chris@16
|
101 } // namespace attributes
|
Chris@16
|
102
|
Chris@16
|
103 BOOST_LOG_CLOSE_NAMESPACE // namespace log
|
Chris@16
|
104
|
Chris@16
|
105 } // namespace boost
|
Chris@16
|
106
|
Chris@16
|
107 #include <boost/log/detail/footer.hpp>
|
Chris@16
|
108
|
Chris@16
|
109 #endif // BOOST_LOG_ATTRIBUTES_CURRENT_THREAD_ID_HPP_INCLUDED_
|