log_time.h revision 52c140ca7216a2abdccd3eec2118bc3a87ded376
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 { 37public: 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 { 46 tv_sec = static_cast<uint32_t>(T.tv_sec); 47 tv_nsec = static_cast<uint32_t>(T.tv_nsec); 48 } 49 log_time(uint32_t sec, uint32_t nsec) 50 { 51 tv_sec = sec; 52 tv_nsec = nsec; 53 } 54#ifdef _SYSTEM_CORE_INCLUDE_PRIVATE_ANDROID_LOGGER_H_ 55#define __struct_log_time_private_defined 56 static const timespec EPOCH; 57#endif 58 log_time() 59 { 60 } 61#ifdef __linux__ 62 log_time(clockid_t id) 63 { 64 timespec T; 65 clock_gettime(id, &T); 66 tv_sec = static_cast<uint32_t>(T.tv_sec); 67 tv_nsec = static_cast<uint32_t>(T.tv_nsec); 68 } 69#endif 70 log_time(const char* T) 71 { 72 const uint8_t* c = reinterpret_cast<const uint8_t*>(T); 73 tv_sec = c[0] | 74 (static_cast<uint32_t>(c[1]) << 8) | 75 (static_cast<uint32_t>(c[2]) << 16) | 76 (static_cast<uint32_t>(c[3]) << 24); 77 tv_nsec = c[4] | 78 (static_cast<uint32_t>(c[5]) << 8) | 79 (static_cast<uint32_t>(c[6]) << 16) | 80 (static_cast<uint32_t>(c[7]) << 24); 81 } 82 83 /* timespec */ 84 bool operator== (const timespec& T) const 85 { 86 return (tv_sec == static_cast<uint32_t>(T.tv_sec)) 87 && (tv_nsec == static_cast<uint32_t>(T.tv_nsec)); 88 } 89 bool operator!= (const timespec& T) const 90 { 91 return !(*this == T); 92 } 93 bool operator< (const timespec& T) const 94 { 95 return (tv_sec < static_cast<uint32_t>(T.tv_sec)) 96 || ((tv_sec == static_cast<uint32_t>(T.tv_sec)) 97 && (tv_nsec < static_cast<uint32_t>(T.tv_nsec))); 98 } 99 bool operator>= (const timespec& T) const 100 { 101 return !(*this < T); 102 } 103 bool operator> (const timespec& T) const 104 { 105 return (tv_sec > static_cast<uint32_t>(T.tv_sec)) 106 || ((tv_sec == static_cast<uint32_t>(T.tv_sec)) 107 && (tv_nsec > static_cast<uint32_t>(T.tv_nsec))); 108 } 109 bool operator<= (const timespec& T) const 110 { 111 return !(*this > T); 112 } 113 114#ifdef _SYSTEM_CORE_INCLUDE_PRIVATE_ANDROID_LOGGER_H_ 115 log_time operator-= (const timespec& T); 116 log_time operator- (const timespec& T) const 117 { 118 log_time local(*this); 119 return local -= T; 120 } 121 log_time operator+= (const timespec& T); 122 log_time operator+ (const timespec& T) const 123 { 124 log_time local(*this); 125 return local += T; 126 } 127#endif 128 129 /* log_time */ 130 bool operator== (const log_time& T) const 131 { 132 return (tv_sec == T.tv_sec) && (tv_nsec == T.tv_nsec); 133 } 134 bool operator!= (const log_time& T) const 135 { 136 return !(*this == T); 137 } 138 bool operator< (const log_time& T) const 139 { 140 return (tv_sec < T.tv_sec) 141 || ((tv_sec == T.tv_sec) && (tv_nsec < T.tv_nsec)); 142 } 143 bool operator>= (const log_time& T) const 144 { 145 return !(*this < T); 146 } 147 bool operator> (const log_time& T) const 148 { 149 return (tv_sec > T.tv_sec) 150 || ((tv_sec == T.tv_sec) && (tv_nsec > T.tv_nsec)); 151 } 152 bool operator<= (const log_time& T) const 153 { 154 return !(*this > T); 155 } 156 157#ifdef _SYSTEM_CORE_INCLUDE_PRIVATE_ANDROID_LOGGER_H_ 158 log_time operator-= (const log_time& T); 159 log_time operator- (const log_time& T) const 160 { 161 log_time local(*this); 162 return local -= T; 163 } 164 log_time operator+= (const log_time& T); 165 log_time operator+ (const log_time& T) const 166 { 167 log_time local(*this); 168 return local += T; 169 } 170#endif 171 172 uint64_t nsec() const 173 { 174 return static_cast<uint64_t>(tv_sec) * NS_PER_SEC + tv_nsec; 175 } 176 177#ifdef _SYSTEM_CORE_INCLUDE_PRIVATE_ANDROID_LOGGER_H_ 178 static const char default_format[]; 179 180 /* Add %#q for the fraction of a second to the standard library functions */ 181 char* strptime(const char* s, const char* format = default_format); 182#endif 183} __attribute__((__packed__)); 184 185#else 186 187typedef struct log_time { 188 uint32_t tv_sec; 189 uint32_t tv_nsec; 190} __attribute__((__packed__)) log_time; 191 192#endif 193 194#endif 195 196#endif /* _LIBS_LOG_LOG_TIME_H */ 197