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 
16 #ifndef BOOST_LOG_DETAIL_TIMESTAMP_HPP_INCLUDED_
17 #define BOOST_LOG_DETAIL_TIMESTAMP_HPP_INCLUDED_
18 
19 #include <boost/cstdint.hpp>
20 #include <boost/log/detail/config.hpp>
21 #include <boost/log/detail/header.hpp>
22 
23 #ifdef BOOST_HAS_PRAGMA_ONCE
24 #pragma once
25 #endif
26 
27 namespace boost {
28 
29 BOOST_LOG_OPEN_NAMESPACE
30 
31 namespace aux {
32 
33 /*!
34  * Duration between two timestamps
35  */
36 class duration
37 {
38     int64_t m_ticks;
39 
40 public:
duration(int64_t ticks=0)41     explicit duration(int64_t ticks = 0) : m_ticks(ticks) {}
42 
43 #if defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
milliseconds() const44     int64_t milliseconds() const { return m_ticks; }
45 #else
46     BOOST_LOG_API int64_t milliseconds() const;
47 #endif
48 };
49 
50 /*!
51  * Opaque timestamp class
52  */
53 class timestamp
54 {
55     uint64_t m_ticks;
56 
57 public:
timestamp(uint64_t ticks=0)58     explicit timestamp(uint64_t ticks = 0) : m_ticks(ticks) {}
59 
operator -(timestamp that) const60     duration operator- (timestamp that) const
61     {
62         return duration(m_ticks - that.m_ticks);
63     }
64 };
65 
66 /*!
67  * \fn get_timestamp
68  *
69  * The function returns a timestamp, in opaque units since an unspecified
70  * time point. This timer is guaranteed to be monotonic, it should not
71  * be affected by clock changes, either manual or seasonal. Also, it
72  * should be as fast as possible.
73  */
74 #if defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
75 
76 typedef uint64_t (__stdcall* get_tick_count_t)();
77 extern BOOST_LOG_API get_tick_count_t get_tick_count;
78 
get_timestamp()79 inline timestamp get_timestamp()
80 {
81     return timestamp(get_tick_count());
82 }
83 
84 #else
85 
86 typedef timestamp (*get_timestamp_t)();
87 extern BOOST_LOG_API get_timestamp_t get_timestamp;
88 
89 #endif
90 
91 } // namespace aux
92 
93 BOOST_LOG_CLOSE_NAMESPACE // namespace log
94 
95 } // namespace boost
96 
97 #include <boost/log/detail/footer.hpp>
98 
99 #endif // BOOST_LOG_DETAIL_TIMESTAMP_HPP_INCLUDED_
100