timestamp.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 timestamp.hpp
  9. * \author Andrey Semashev
  10. * \date 31.07.2011
  11. *
  12. * \brief This header is the Boost.Log library implementation, see the library documentation
  13. * at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html.
  14. */
  15. #ifndef BOOST_LOG_DETAIL_TIMESTAMP_HPP_INCLUDED_
  16. #define BOOST_LOG_DETAIL_TIMESTAMP_HPP_INCLUDED_
  17. #include <boost/cstdint.hpp>
  18. #include <boost/log/detail/config.hpp>
  19. #if defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
  20. #include <boost/winapi/basic_types.hpp>
  21. #endif
  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 aux {
  29. /*!
  30. * Duration between two timestamps
  31. */
  32. class duration
  33. {
  34. int64_t m_ticks;
  35. public:
  36. explicit duration(int64_t ticks = 0) BOOST_NOEXCEPT : m_ticks(ticks) {}
  37. #if defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
  38. int64_t milliseconds() const { return m_ticks; }
  39. #else
  40. BOOST_LOG_API int64_t milliseconds() const;
  41. #endif
  42. };
  43. /*!
  44. * Opaque timestamp class
  45. */
  46. class timestamp
  47. {
  48. uint64_t m_ticks;
  49. public:
  50. explicit timestamp(uint64_t ticks = 0) BOOST_NOEXCEPT : m_ticks(ticks) {}
  51. duration operator- (timestamp that) const
  52. {
  53. return duration(m_ticks - that.m_ticks);
  54. }
  55. };
  56. /*!
  57. * \fn get_timestamp
  58. *
  59. * The function returns a timestamp, in opaque units since an unspecified
  60. * time point. This timer is guaranteed to be monotonic, it should not
  61. * be affected by clock changes, either manual or seasonal. Also, it
  62. * should be as fast as possible.
  63. */
  64. #if defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
  65. typedef uint64_t (BOOST_WINAPI_WINAPI_CC* get_tick_count_t)();
  66. extern BOOST_LOG_API get_tick_count_t get_tick_count;
  67. inline timestamp get_timestamp()
  68. {
  69. return timestamp(get_tick_count());
  70. }
  71. #else
  72. typedef timestamp (*get_timestamp_t)();
  73. extern BOOST_LOG_API get_timestamp_t get_timestamp;
  74. #endif
  75. } // namespace aux
  76. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  77. } // namespace boost
  78. #include <boost/log/detail/footer.hpp>
  79. #endif // BOOST_LOG_DETAIL_TIMESTAMP_HPP_INCLUDED_