log_read.h revision 3cb54987857690fa5b73f0192cc052eb55b562f7
1/* 2 * Copyright (C) 2013-2014 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_READ_H 18#define _LIBS_LOG_LOG_READ_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#ifdef __cplusplus 27 28// NB: do NOT define a copy constructor. This will result in structure 29// no longer being compatible with pass-by-value which is desired 30// efficient behavior. Also, pass-by-reference breaks C/C++ ABI. 31struct log_time { 32public: 33 uint32_t tv_sec; // good to Feb 5 2106 34 uint32_t tv_nsec; 35 36 static const uint32_t tv_sec_max = 0xFFFFFFFFUL; 37 static const uint32_t tv_nsec_max = 999999999UL; 38 39 log_time(const timespec &T) 40 { 41 tv_sec = T.tv_sec; 42 tv_nsec = T.tv_nsec; 43 } 44 log_time(uint32_t sec, uint32_t nsec) 45 { 46 tv_sec = sec; 47 tv_nsec = nsec; 48 } 49 static const timespec EPOCH; 50 log_time() 51 { 52 } 53 log_time(clockid_t id) 54 { 55 timespec T; 56 clock_gettime(id, &T); 57 tv_sec = T.tv_sec; 58 tv_nsec = T.tv_nsec; 59 } 60 log_time(const char *T) 61 { 62 const uint8_t *c = (const uint8_t *) T; 63 tv_sec = c[0] | (c[1] << 8) | (c[2] << 16) | (c[3] << 24); 64 tv_nsec = c[4] | (c[5] << 8) | (c[6] << 16) | (c[7] << 24); 65 } 66 67 // timespec 68 bool operator== (const timespec &T) const 69 { 70 return (tv_sec == T.tv_sec) && (tv_nsec == T.tv_nsec); 71 } 72 bool operator!= (const timespec &T) const 73 { 74 return !(*this == T); 75 } 76 bool operator< (const timespec &T) const 77 { 78 return (tv_sec < T.tv_sec) 79 || ((tv_sec == T.tv_sec) && (tv_nsec < T.tv_nsec)); 80 } 81 bool operator>= (const timespec &T) const 82 { 83 return !(*this < T); 84 } 85 bool operator> (const timespec &T) const 86 { 87 return (tv_sec > T.tv_sec) 88 || ((tv_sec == T.tv_sec) && (tv_nsec > T.tv_nsec)); 89 } 90 bool operator<= (const timespec &T) const 91 { 92 return !(*this > T); 93 } 94 log_time operator-= (const timespec &T); 95 log_time operator- (const timespec &T) const 96 { 97 log_time local(*this); 98 return local -= T; 99 } 100 101 // log_time 102 bool operator== (const log_time &T) const 103 { 104 return (tv_sec == T.tv_sec) && (tv_nsec == T.tv_nsec); 105 } 106 bool operator!= (const log_time &T) const 107 { 108 return !(*this == T); 109 } 110 bool operator< (const log_time &T) const 111 { 112 return (tv_sec < T.tv_sec) 113 || ((tv_sec == T.tv_sec) && (tv_nsec < T.tv_nsec)); 114 } 115 bool operator>= (const log_time &T) const 116 { 117 return !(*this < T); 118 } 119 bool operator> (const log_time &T) const 120 { 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 { 126 return !(*this > T); 127 } 128 log_time operator-= (const log_time &T); 129 log_time operator- (const log_time &T) const 130 { 131 log_time local(*this); 132 return local -= T; 133 } 134 135 uint64_t nsec() const 136 { 137 return static_cast<uint64_t>(tv_sec) * NS_PER_SEC + tv_nsec; 138 } 139 140 static const char default_format[]; 141 142 // Add %#q for the fraction of a second to the standard library functions 143 char *strptime(const char *s, const char *format = default_format); 144} __attribute__((__packed__)); 145 146#else 147 148typedef struct log_time { 149 uint32_t tv_sec; 150 uint32_t tv_nsec; 151} __attribute__((__packed__)) log_time; 152 153#endif 154 155#endif /* define _LIBS_LOG_LOG_READ_H */ 156