OSDN Git Service

Add time string object
authorAndy Hung <hunga@google.com>
Thu, 23 Mar 2017 23:13:53 +0000 (16:13 -0700)
committerAndy Hung <hunga@google.com>
Wed, 29 Mar 2017 05:38:32 +0000 (05:38 +0000)
Test: unit test and audioflinger dumpsys
Bug: 30572472
Change-Id: Ieac2ca4bfd2e32174df3ac362f621cba4ae8cb8f
(cherry picked from commit c1eb186fc1ab8837e95e01be8a87a77e952fc81f)

audio_utils/PowerLog.cpp
audio_utils/include/audio_utils/ErrorLog.h
audio_utils/include/audio_utils/SimpleLog.h
audio_utils/include/audio_utils/clock.h

index c4ccbc4..ab0dcaa 100644 (file)
@@ -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 {
index e27a645..88448b1 100644 (file)
@@ -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();
index 51494ac..d5fd021 100644 (file)
@@ -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();
     }
index 7488e08..31bf1f6 100644 (file)
@@ -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.