diff --git a/liblog/include/log/log_time.h b/liblog/include/log/log_time.h index 6b4458cae..f50764de3 100644 --- a/liblog/include/log/log_time.h +++ b/liblog/include/log/log_time.h @@ -37,9 +37,7 @@ struct log_time { uint32_t tv_sec = 0; /* good to Feb 5 2106 */ uint32_t tv_nsec = 0; - static const uint32_t tv_sec_max = 0xFFFFFFFFUL; - static const uint32_t tv_nsec_max = 999999999UL; - static const timespec EPOCH; + static constexpr timespec EPOCH = {0, 0}; log_time() {} explicit log_time(const timespec& T) @@ -55,16 +53,6 @@ struct log_time { tv_nsec = static_cast(T.tv_nsec); } #endif - explicit log_time(const char* T) { - const uint8_t* c = reinterpret_cast(T); - tv_sec = c[0] | (static_cast(c[1]) << 8) | - (static_cast(c[2]) << 16) | - (static_cast(c[3]) << 24); - tv_nsec = c[4] | (static_cast(c[5]) << 8) | - (static_cast(c[6]) << 16) | - (static_cast(c[7]) << 24); - } - /* timespec */ bool operator==(const timespec& T) const { return (tv_sec == static_cast(T.tv_sec)) && @@ -90,17 +78,6 @@ struct log_time { return !(*this > T); } - log_time operator-=(const timespec& T); - log_time operator-(const timespec& T) const { - log_time local(*this); - return local -= T; - } - log_time operator+=(const timespec& T); - log_time operator+(const timespec& T) const { - log_time local(*this); - return local += T; - } - /* log_time */ bool operator==(const log_time& T) const { return (tv_sec == T.tv_sec) && (tv_nsec == T.tv_nsec); @@ -123,12 +100,36 @@ struct log_time { return !(*this > T); } - log_time operator-=(const log_time& T); + log_time operator-=(const log_time& T) { + // No concept of negative time, clamp to EPOCH + if (*this <= T) { + return *this = log_time(EPOCH); + } + + if (this->tv_nsec < T.tv_nsec) { + --this->tv_sec; + this->tv_nsec = NS_PER_SEC + this->tv_nsec - T.tv_nsec; + } else { + this->tv_nsec -= T.tv_nsec; + } + this->tv_sec -= T.tv_sec; + + return *this; + } log_time operator-(const log_time& T) const { log_time local(*this); return local -= T; } - log_time operator+=(const log_time& T); + log_time operator+=(const log_time& T) { + this->tv_nsec += T.tv_nsec; + if (this->tv_nsec >= NS_PER_SEC) { + this->tv_nsec -= NS_PER_SEC; + ++this->tv_sec; + } + this->tv_sec += T.tv_sec; + + return *this; + } log_time operator+(const log_time& T) const { log_time local(*this); return local += T; @@ -146,10 +147,8 @@ struct log_time { tv_nsec / (NS_PER_SEC / MS_PER_SEC); } - static const char default_format[]; - /* Add %#q for the fraction of a second to the standard library functions */ - char* strptime(const char* s, const char* format = default_format); + char* strptime(const char* s, const char* format); } __attribute__((__packed__)); } diff --git a/liblog/log_time.cpp b/liblog/log_time.cpp index 3fbe1cbc5..14c408c1c 100644 --- a/liblog/log_time.cpp +++ b/liblog/log_time.cpp @@ -21,11 +21,7 @@ #include -const char log_time::default_format[] = "%m-%d %H:%M:%S.%q"; -const timespec log_time::EPOCH = {0, 0}; - // Add %#q for fractional seconds to standard strptime function - char* log_time::strptime(const char* s, const char* format) { time_t now; #ifdef __linux__ @@ -131,59 +127,3 @@ char* log_time::strptime(const char* s, const char* format) { #endif return ret; } - -log_time log_time::operator-=(const timespec& T) { - // No concept of negative time, clamp to EPOCH - if (*this <= T) { - return *this = log_time(EPOCH); - } - - if (this->tv_nsec < (unsigned long int)T.tv_nsec) { - --this->tv_sec; - this->tv_nsec = NS_PER_SEC + this->tv_nsec - T.tv_nsec; - } else { - this->tv_nsec -= T.tv_nsec; - } - this->tv_sec -= T.tv_sec; - - return *this; -} - -log_time log_time::operator+=(const timespec& T) { - this->tv_nsec += (unsigned long int)T.tv_nsec; - if (this->tv_nsec >= NS_PER_SEC) { - this->tv_nsec -= NS_PER_SEC; - ++this->tv_sec; - } - this->tv_sec += T.tv_sec; - - return *this; -} - -log_time log_time::operator-=(const log_time& T) { - // No concept of negative time, clamp to EPOCH - if (*this <= T) { - return *this = log_time(EPOCH); - } - - if (this->tv_nsec < T.tv_nsec) { - --this->tv_sec; - this->tv_nsec = NS_PER_SEC + this->tv_nsec - T.tv_nsec; - } else { - this->tv_nsec -= T.tv_nsec; - } - this->tv_sec -= T.tv_sec; - - return *this; -} - -log_time log_time::operator+=(const log_time& T) { - this->tv_nsec += T.tv_nsec; - if (this->tv_nsec >= NS_PER_SEC) { - this->tv_nsec -= NS_PER_SEC; - ++this->tv_sec; - } - this->tv_sec += T.tv_sec; - - return *this; -} diff --git a/liblog/tests/liblog_benchmark.cpp b/liblog/tests/liblog_benchmark.cpp index a4e4def9c..3bd5cf20f 100644 --- a/liblog/tests/liblog_benchmark.cpp +++ b/liblog/tests/liblog_benchmark.cpp @@ -684,8 +684,8 @@ static void BM_log_latency(benchmark::State& state) { if (!eventData || (eventData[4] != EVENT_TYPE_LONG)) { continue; } - log_time tx(eventData + 4 + 1); - if (ts != tx) { + log_time* tx = reinterpret_cast(eventData + 4 + 1); + if (ts != *tx) { if (0xDEADBEEFA55A5AA5ULL == caught_convert(eventData + 4 + 1)) { state.SkipWithError("signal"); break; @@ -757,8 +757,8 @@ static void BM_log_delay(benchmark::State& state) { if (!eventData || (eventData[4] != EVENT_TYPE_LONG)) { continue; } - log_time tx(eventData + 4 + 1); - if (ts != tx) { + log_time* tx = reinterpret_cast(eventData + 4 + 1); + if (ts != *tx) { if (0xDEADBEEFA55A5AA6ULL == caught_convert(eventData + 4 + 1)) { state.SkipWithError("signal"); break; diff --git a/liblog/tests/liblog_test.cpp b/liblog/tests/liblog_test.cpp index bbc985a79..fbc3d7a5a 100644 --- a/liblog/tests/liblog_test.cpp +++ b/liblog/tests/liblog_test.cpp @@ -270,10 +270,10 @@ TEST(liblog, __android_log_btwrite__android_logger_list_read) { return; } - log_time tx(reinterpret_cast(&eventData->payload.data)); - if (ts == tx) { + log_time* tx = reinterpret_cast(&eventData->payload.data); + if (ts == *tx) { ++count; - } else if (ts1 == tx) { + } else if (ts1 == *tx) { ++second_count; } diff --git a/logcat/tests/logcat_test.cpp b/logcat/tests/logcat_test.cpp index 3a55c4e93..f3fdfd714 100644 --- a/logcat/tests/logcat_test.cpp +++ b/logcat/tests/logcat_test.cpp @@ -475,8 +475,8 @@ TEST(logcat, End_to_End) { continue; } - log_time tx((const char*)&t); - if (ts == tx) { + log_time* tx = reinterpret_cast(&t); + if (ts == *tx) { ++count; } } @@ -521,8 +521,8 @@ TEST(logcat, End_to_End_multitude) { continue; } - log_time tx((const char*)&t); - if (ts == tx) { + log_time* tx = reinterpret_cast(&t); + if (ts == *tx) { ++count; } } diff --git a/logd/ChattyLogBuffer.cpp b/logd/ChattyLogBuffer.cpp index c6c9a7c37..1196c837d 100644 --- a/logd/ChattyLogBuffer.cpp +++ b/logd/ChattyLogBuffer.cpp @@ -660,7 +660,7 @@ bool ChattyLogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_u if (leading) { it = GetOldest(id); } - static const timespec too_old = {EXPIRE_HOUR_THRESHOLD * 60 * 60, 0}; + static const log_time too_old{EXPIRE_HOUR_THRESHOLD * 60 * 60, 0}; LogBufferElementCollection::iterator lastt; lastt = mLogElements.end(); --lastt; diff --git a/logd/tests/logd_test.cpp b/logd/tests/logd_test.cpp index 55737e9e9..570ff2a24 100644 --- a/logd/tests/logd_test.cpp +++ b/logd/tests/logd_test.cpp @@ -904,10 +904,10 @@ void __android_log_btwrite_multiple__helper(int count) { (log_msg.entry.len == (4 + 1 + 8))) { if (tag != 0) continue; - log_time tx(eventData + 4 + 1); - if (ts == tx) { + log_time* tx = reinterpret_cast(eventData + 4 + 1); + if (ts == *tx) { ++count; - } else if (ts1 == tx) { + } else if (ts1 == *tx) { ++second_count; } } else if (eventData[4] == EVENT_TYPE_STRING) {