From 6b711100b6ca72660da6c1db1a5af5997d08432f Mon Sep 17 00:00:00 2001 From: Andy Hung Date: Thu, 23 Mar 2017 16:13:53 -0700 Subject: [PATCH] Add time string object Test: unit test and audioflinger dumpsys Bug: 30572472 Change-Id: Ieac2ca4bfd2e32174df3ac362f621cba4ae8cb8f (cherry picked from commit c1eb186fc1ab8837e95e01be8a87a77e952fc81f) --- audio_utils/PowerLog.cpp | 5 ++--- audio_utils/include/audio_utils/ErrorLog.h | 9 +++------ audio_utils/include/audio_utils/SimpleLog.h | 5 ++--- audio_utils/include/audio_utils/clock.h | 23 +++++++++++++++++++++++ 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/audio_utils/PowerLog.cpp b/audio_utils/PowerLog.cpp index c4ccbc4f..ab0dcaa3 100644 --- a/audio_utils/PowerLog.cpp +++ b/audio_utils/PowerLog.cpp @@ -186,12 +186,11 @@ std::string PowerLog::dumpToString(const char *prefix, size_t lines, int64_t lim } if (column == 0) { // print time if at start of column - char timeinfo[32]; - audio_utils_ns_to_string(time, timeinfo, array_size(timeinfo)); if (!first) { ss << "\n"; } - ss << prefix << " " << timeinfo << (start ? ": [ ": ": "); + ss << prefix << " " << audio_utils_time_string_from_ns(time).time + << (start ? ": [ ": ": "); first = false; start = false; } else { diff --git a/audio_utils/include/audio_utils/ErrorLog.h b/audio_utils/include/audio_utils/ErrorLog.h index e27a6459..88448b14 100644 --- a/audio_utils/include/audio_utils/ErrorLog.h +++ b/audio_utils/include/audio_utils/ErrorLog.h @@ -128,14 +128,11 @@ public: for (; offset >= 0; --offset) { const auto &entry = mEntries[(mIdx + numberOfEntries - offset) % numberOfEntries]; - char firstTime[32]; - char lastTime[32]; - audio_utils_ns_to_string(entry.mFirstTime, firstTime, sizeof(firstTime)); - audio_utils_ns_to_string(entry.mLastTime, lastTime, sizeof(lastTime)); + ss << prefix << std::setw(5) << entry.mCode << " " << std::setw(5) << entry.mCount - << " " << firstTime - << " " << lastTime << "\n"; + << " " << audio_utils_time_string_from_ns(entry.mFirstTime).time + << " " << audio_utils_time_string_from_ns(entry.mLastTime).time << "\n"; } } return ss.str(); diff --git a/audio_utils/include/audio_utils/SimpleLog.h b/audio_utils/include/audio_utils/SimpleLog.h index 51494acc..d5fd0210 100644 --- a/audio_utils/include/audio_utils/SimpleLog.h +++ b/audio_utils/include/audio_utils/SimpleLog.h @@ -169,9 +169,8 @@ public: for (; it != mLog.end(); ++it) { const int64_t time = it->first; if (time < limitNs) continue; // too old - char timeinfo[32]; - audio_utils_ns_to_string(time, timeinfo, sizeof(timeinfo)); - ss << prefix << timeinfo << " " << it->second.c_str() << "\n"; + ss << prefix << audio_utils_time_string_from_ns(time).time + << " " << it->second.c_str() << "\n"; } return ss.str(); } diff --git a/audio_utils/include/audio_utils/clock.h b/audio_utils/include/audio_utils/clock.h index 7488e08b..31bf1f60 100644 --- a/audio_utils/include/audio_utils/clock.h +++ b/audio_utils/include/audio_utils/clock.h @@ -55,6 +55,29 @@ static inline void audio_utils_ns_to_string(int64_t ns, char *buffer, size_t buf } /** + * An object that contains the formatted time string. + * + * The time string is 19 characters (including null termination). + * Example: "03-27 16:47:06.187" + * MM DD HH MM SS MS + */ +typedef struct audio_utils_time_string { + char time[19]; /* minimum size buffer */ +} audio_utils_time_string_t; + +/** + * \brief Converts time in ns to a time string object, with format similar to logcat. + * \param ns input time in nanoseconds to convert. + */ +static inline audio_utils_time_string_t audio_utils_time_string_from_ns(int64_t ns) +{ + audio_utils_time_string_t ts; + + audio_utils_ns_to_string(ns, ts.time, sizeof(ts.time)); + return ts; +} + +/** * \brief Converts a timespec to nanoseconds. * \param ts input timespec to convert. * \return timespec converted to nanoseconds. -- 2.11.0