1/*
2 * Copyright (C) 2005-2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _LIBS_LOG_LOG_TIME_H
18#define _LIBS_LOG_LOG_TIME_H
19
20#include <stdint.h>
21#include <time.h>
22
23/* struct log_time is a wire-format variant of struct timespec */
24#define NS_PER_SEC 1000000000ULL
25
26#ifndef __struct_log_time_defined
27#define __struct_log_time_defined
28
29#ifdef __cplusplus
30
31/*
32 * NB: we did NOT define a copy constructor. This will result in structure
33 * no longer being compatible with pass-by-value which is desired
34 * efficient behavior. Also, pass-by-reference breaks C/C++ ABI.
35 */
36struct log_time {
37 public:
38  uint32_t tv_sec; /* good to Feb 5 2106 */
39  uint32_t tv_nsec;
40
41  static const uint32_t tv_sec_max = 0xFFFFFFFFUL;
42  static const uint32_t tv_nsec_max = 999999999UL;
43
44  log_time(const timespec& T)
45      : tv_sec(static_cast<uint32_t>(T.tv_sec)),
46        tv_nsec(static_cast<uint32_t>(T.tv_nsec)) {
47  }
48  explicit log_time(uint32_t sec, uint32_t nsec = 0)
49      : tv_sec(sec), tv_nsec(nsec) {
50  }
51#ifdef _SYSTEM_CORE_INCLUDE_PRIVATE_ANDROID_LOGGER_H_
52#define __struct_log_time_private_defined
53  static const timespec EPOCH;
54#endif
55  log_time() {
56  }
57#ifdef __linux__
58  explicit log_time(clockid_t id) {
59    timespec T;
60    clock_gettime(id, &T);
61    tv_sec = static_cast<uint32_t>(T.tv_sec);
62    tv_nsec = static_cast<uint32_t>(T.tv_nsec);
63  }
64#endif
65  explicit log_time(const char* T) {
66    const uint8_t* c = reinterpret_cast<const uint8_t*>(T);
67    tv_sec = c[0] | (static_cast<uint32_t>(c[1]) << 8) |
68             (static_cast<uint32_t>(c[2]) << 16) |
69             (static_cast<uint32_t>(c[3]) << 24);
70    tv_nsec = c[4] | (static_cast<uint32_t>(c[5]) << 8) |
71              (static_cast<uint32_t>(c[6]) << 16) |
72              (static_cast<uint32_t>(c[7]) << 24);
73  }
74
75  /* timespec */
76  bool operator==(const timespec& T) const {
77    return (tv_sec == static_cast<uint32_t>(T.tv_sec)) &&
78           (tv_nsec == static_cast<uint32_t>(T.tv_nsec));
79  }
80  bool operator!=(const timespec& T) const {
81    return !(*this == T);
82  }
83  bool operator<(const timespec& T) const {
84    return (tv_sec < static_cast<uint32_t>(T.tv_sec)) ||
85           ((tv_sec == static_cast<uint32_t>(T.tv_sec)) &&
86            (tv_nsec < static_cast<uint32_t>(T.tv_nsec)));
87  }
88  bool operator>=(const timespec& T) const {
89    return !(*this < T);
90  }
91  bool operator>(const timespec& T) const {
92    return (tv_sec > static_cast<uint32_t>(T.tv_sec)) ||
93           ((tv_sec == static_cast<uint32_t>(T.tv_sec)) &&
94            (tv_nsec > static_cast<uint32_t>(T.tv_nsec)));
95  }
96  bool operator<=(const timespec& T) const {
97    return !(*this > T);
98  }
99
100#ifdef _SYSTEM_CORE_INCLUDE_PRIVATE_ANDROID_LOGGER_H_
101  log_time operator-=(const timespec& T);
102  log_time operator-(const timespec& T) const {
103    log_time local(*this);
104    return local -= T;
105  }
106  log_time operator+=(const timespec& T);
107  log_time operator+(const timespec& T) const {
108    log_time local(*this);
109    return local += T;
110  }
111#endif
112
113  /* log_time */
114  bool operator==(const log_time& T) const {
115    return (tv_sec == T.tv_sec) && (tv_nsec == T.tv_nsec);
116  }
117  bool operator!=(const log_time& T) const {
118    return !(*this == T);
119  }
120  bool operator<(const log_time& T) const {
121    return (tv_sec < T.tv_sec) ||
122           ((tv_sec == T.tv_sec) && (tv_nsec < T.tv_nsec));
123  }
124  bool operator>=(const log_time& T) const {
125    return !(*this < T);
126  }
127  bool operator>(const log_time& T) const {
128    return (tv_sec > T.tv_sec) ||
129           ((tv_sec == T.tv_sec) && (tv_nsec > T.tv_nsec));
130  }
131  bool operator<=(const log_time& T) const {
132    return !(*this > T);
133  }
134
135#ifdef _SYSTEM_CORE_INCLUDE_PRIVATE_ANDROID_LOGGER_H_
136  log_time operator-=(const log_time& T);
137  log_time operator-(const log_time& T) const {
138    log_time local(*this);
139    return local -= T;
140  }
141  log_time operator+=(const log_time& T);
142  log_time operator+(const log_time& T) const {
143    log_time local(*this);
144    return local += T;
145  }
146#endif
147
148  uint64_t nsec() const {
149    return static_cast<uint64_t>(tv_sec) * NS_PER_SEC + tv_nsec;
150  }
151
152#ifdef _SYSTEM_CORE_INCLUDE_PRIVATE_ANDROID_LOGGER_H_
153  static const char default_format[];
154
155  /* Add %#q for the fraction of a second to the standard library functions */
156  char* strptime(const char* s, const char* format = default_format);
157#endif
158} __attribute__((__packed__));
159
160#else
161
162typedef struct log_time {
163  uint32_t tv_sec;
164  uint32_t tv_nsec;
165} __attribute__((__packed__)) log_time;
166
167#endif
168
169#endif
170
171#endif /* _LIBS_LOG_LOG_TIME_H */
172