clock.hpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 clock.hpp
  9. * \author Andrey Semashev
  10. * \date 01.12.2007
  11. *
  12. * The header contains wall clock attribute implementation and typedefs.
  13. */
  14. #ifndef BOOST_LOG_ATTRIBUTES_CLOCK_HPP_INCLUDED_
  15. #define BOOST_LOG_ATTRIBUTES_CLOCK_HPP_INCLUDED_
  16. #include <boost/log/detail/config.hpp>
  17. #include <boost/log/attributes/attribute.hpp>
  18. #include <boost/log/attributes/attribute_value.hpp>
  19. #include <boost/log/attributes/attribute_cast.hpp>
  20. #include <boost/log/attributes/attribute_value_impl.hpp>
  21. #include <boost/log/attributes/time_traits.hpp>
  22. #include <boost/log/detail/header.hpp>
  23. #ifdef BOOST_HAS_PRAGMA_ONCE
  24. #pragma once
  25. #endif
  26. namespace boost {
  27. BOOST_LOG_OPEN_NAMESPACE
  28. namespace attributes {
  29. /*!
  30. * \brief A class of an attribute that makes an attribute value of the current date and time
  31. *
  32. * The attribute generates current time stamp as a value. The type of the attribute value
  33. * is determined with time traits passed to the class template as a template parameter.
  34. * The time traits provided by the library use \c boost::posix_time::ptime as the time type.
  35. *
  36. * Time traits also determine the way time is acquired. There are two types of time traits
  37. * provided by the library: \c utc_time_traits and \c local_time_traits. The first returns UTC time,
  38. * the second returns local time.
  39. */
  40. template< typename TimeTraitsT >
  41. class basic_clock :
  42. public attribute
  43. {
  44. public:
  45. //! Generated value type
  46. typedef typename TimeTraitsT::time_type value_type;
  47. protected:
  48. //! Attribute factory implementation
  49. struct BOOST_SYMBOL_VISIBLE impl :
  50. public attribute::impl
  51. {
  52. attribute_value get_value()
  53. {
  54. typedef attribute_value_impl< value_type > result_value;
  55. return attribute_value(new result_value(TimeTraitsT::get_clock()));
  56. }
  57. };
  58. public:
  59. /*!
  60. * Default constructor
  61. */
  62. basic_clock() : attribute(new impl())
  63. {
  64. }
  65. /*!
  66. * Constructor for casting support
  67. */
  68. explicit basic_clock(cast_source const& source) : attribute(source.as< impl >())
  69. {
  70. }
  71. };
  72. //! Attribute that returns current UTC time
  73. typedef basic_clock< utc_time_traits > utc_clock;
  74. //! Attribute that returns current local time
  75. typedef basic_clock< local_time_traits > local_clock;
  76. } // namespace attributes
  77. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  78. } // namespace boost
  79. #include <boost/log/detail/footer.hpp>
  80. #endif // BOOST_LOG_ATTRIBUTES_CLOCK_HPP_INCLUDED_