current_thread_id.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 current_thread_id.hpp
  9. * \author Andrey Semashev
  10. * \date 12.09.2009
  11. *
  12. * The header contains implementation of a current thread id attribute
  13. */
  14. #ifndef BOOST_LOG_ATTRIBUTES_CURRENT_THREAD_ID_HPP_INCLUDED_
  15. #define BOOST_LOG_ATTRIBUTES_CURRENT_THREAD_ID_HPP_INCLUDED_
  16. #include <boost/log/detail/config.hpp>
  17. #ifdef BOOST_HAS_PRAGMA_ONCE
  18. #pragma once
  19. #endif
  20. #if defined(BOOST_LOG_NO_THREADS)
  21. #error Boost.Log: The current_thread_id attribute is only available in multithreaded builds
  22. #endif
  23. #include <boost/smart_ptr/intrusive_ptr.hpp>
  24. #include <boost/log/detail/thread_id.hpp>
  25. #include <boost/log/attributes/attribute.hpp>
  26. #include <boost/log/attributes/attribute_cast.hpp>
  27. #include <boost/log/attributes/attribute_value_impl.hpp>
  28. #include <boost/log/detail/header.hpp>
  29. namespace boost {
  30. BOOST_LOG_OPEN_NAMESPACE
  31. //! Thread identifier type
  32. typedef boost::log::aux::thread::id thread_id;
  33. namespace attributes {
  34. /*!
  35. * \brief A class of an attribute that always returns the current thread identifier
  36. *
  37. * \note This attribute can be registered globally, it will still return the correct
  38. * thread identifier, no matter which thread emits the log record.
  39. */
  40. class current_thread_id :
  41. public attribute
  42. {
  43. public:
  44. //! A held attribute value type
  45. typedef thread_id value_type;
  46. protected:
  47. //! Factory implementation
  48. class BOOST_SYMBOL_VISIBLE impl :
  49. public attribute_value::impl
  50. {
  51. public:
  52. bool dispatch(type_dispatcher& dispatcher)
  53. {
  54. type_dispatcher::callback< value_type > callback =
  55. dispatcher.get_callback< value_type >();
  56. if (callback)
  57. {
  58. callback(boost::log::aux::this_thread::get_id());
  59. return true;
  60. }
  61. else
  62. return false;
  63. }
  64. intrusive_ptr< attribute_value::impl > detach_from_thread()
  65. {
  66. typedef attribute_value_impl< value_type > detached_value;
  67. return new detached_value(boost::log::aux::this_thread::get_id());
  68. }
  69. typeindex::type_index get_type() const { return typeindex::type_id< value_type >(); }
  70. };
  71. public:
  72. /*!
  73. * Default constructor
  74. */
  75. current_thread_id() : attribute(new impl())
  76. {
  77. }
  78. /*!
  79. * Constructor for casting support
  80. */
  81. explicit current_thread_id(cast_source const& source) :
  82. attribute(source.as< impl >())
  83. {
  84. }
  85. };
  86. } // namespace attributes
  87. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  88. } // namespace boost
  89. #include <boost/log/detail/footer.hpp>
  90. #endif // BOOST_LOG_ATTRIBUTES_CURRENT_THREAD_ID_HPP_INCLUDED_