timestamp.hpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2013.
  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. #include <boost/log/detail/header.hpp>
  20. #ifdef BOOST_HAS_PRAGMA_ONCE
  21. #pragma once
  22. #endif
  23. namespace boost {
  24. BOOST_LOG_OPEN_NAMESPACE
  25. namespace aux {
  26. /*!
  27. * Duration between two timestamps
  28. */
  29. class duration
  30. {
  31. int64_t m_ticks;
  32. public:
  33. explicit duration(int64_t ticks = 0) : m_ticks(ticks) {}
  34. #if defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
  35. int64_t milliseconds() const { return m_ticks; }
  36. #else
  37. BOOST_LOG_API int64_t milliseconds() const;
  38. #endif
  39. };
  40. /*!
  41. * Opaque timestamp class
  42. */
  43. class timestamp
  44. {
  45. uint64_t m_ticks;
  46. public:
  47. explicit timestamp(uint64_t ticks = 0) : m_ticks(ticks) {}
  48. duration operator- (timestamp that) const
  49. {
  50. return duration(m_ticks - that.m_ticks);
  51. }
  52. };
  53. /*!
  54. * \fn get_timestamp
  55. *
  56. * The function returns a timestamp, in opaque units since an unspecified
  57. * time point. This timer is guaranteed to be monotonic, it should not
  58. * be affected by clock changes, either manual or seasonal. Also, it
  59. * should be as fast as possible.
  60. */
  61. #if defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
  62. typedef uint64_t (__stdcall* get_tick_count_t)();
  63. extern BOOST_LOG_API get_tick_count_t get_tick_count;
  64. inline timestamp get_timestamp()
  65. {
  66. return timestamp(get_tick_count());
  67. }
  68. #else
  69. typedef timestamp (*get_timestamp_t)();
  70. extern BOOST_LOG_API get_timestamp_t get_timestamp;
  71. #endif
  72. } // namespace aux
  73. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  74. } // namespace boost
  75. #include <boost/log/detail/footer.hpp>
  76. #endif // BOOST_LOG_DETAIL_TIMESTAMP_HPP_INCLUDED_