diff options
| author | Hugo Benichi <hugobenichi@google.com> | 2016-10-04 11:36:11 +0900 |
|---|---|---|
| committer | Hugo Benichi <hugobenichi@google.com> | 2016-10-11 09:54:19 +0900 |
| commit | de310dba78fa7cd60ad01cefa7fadb6d48fffcaa (patch) | |
| tree | 327bc0ca20c0d5dff317b11d1808833d42252dc9 /core/java/android/util/LocalLog.java | |
| parent | 9cf75061b143196c97c31726655c7e5c4ada8814 (diff) | |
Better LocalLog
This patch fixes the following issues in LocalLog:
- reverseDump() uses a descending iterator with linear complexity
instead of a quadratic loop using get(index) on a linked list.
- reverseDump() is added to ReadOnlyLocalLog.
- synchronized section in log() is restricted to mutation of internal
list.
- formatting of the log message does not create an internal
StringBuilder.
- the instance variable mNow is removed: it was only used inside log()
as a local variable.
- remaining instance variables are qualified with final.
- the linked list is replaced by a fixed capacity array-backed queue.
Test: added unit tests
Change-Id: I1a54f0ad26dd35448d3297ea24df1fd626d20ef3
Diffstat (limited to 'core/java/android/util/LocalLog.java')
| -rw-r--r-- | core/java/android/util/LocalLog.java | 44 |
1 files changed, 26 insertions, 18 deletions
diff --git a/core/java/android/util/LocalLog.java b/core/java/android/util/LocalLog.java index 39f66a5f18fc..665c583a56ab 100644 --- a/core/java/android/util/LocalLog.java +++ b/core/java/android/util/LocalLog.java @@ -20,44 +20,49 @@ import java.io.FileDescriptor; import java.io.PrintWriter; import java.util.Calendar; import java.util.Iterator; -import java.util.LinkedList; +import java.util.Deque; +import java.util.ArrayDeque; /** * @hide */ public final class LocalLog { - private LinkedList<String> mLog; - private int mMaxLines; - private long mNow; + private final Deque<String> mLog; + private final int mMaxLines; public LocalLog(int maxLines) { - mLog = new LinkedList<String>(); - mMaxLines = maxLines; + mMaxLines = Math.max(0, maxLines); + mLog = new ArrayDeque<>(mMaxLines); } - public synchronized void log(String msg) { - if (mMaxLines > 0) { - mNow = System.currentTimeMillis(); - StringBuilder sb = new StringBuilder(); - Calendar c = Calendar.getInstance(); - c.setTimeInMillis(mNow); - sb.append(String.format("%tm-%td %tH:%tM:%tS.%tL", c, c, c, c, c, c)); - mLog.add(sb.toString() + " - " + msg); - while (mLog.size() > mMaxLines) mLog.remove(); + public void log(String msg) { + if (mMaxLines <= 0) { + return; } + Calendar c = Calendar.getInstance(); + c.setTimeInMillis(System.currentTimeMillis()); + append(String.format("%tm-%td %tH:%tM:%tS.%tL - %s", c, c, c, c, c, c, msg)); + } + + private synchronized void append(String logLine) { + while (mLog.size() >= mMaxLines) { + mLog.remove(); + } + mLog.add(logLine); } public synchronized void dump(FileDescriptor fd, PrintWriter pw, String[] args) { - Iterator<String> itr = mLog.listIterator(0); + Iterator<String> itr = mLog.iterator(); while (itr.hasNext()) { pw.println(itr.next()); } } public synchronized void reverseDump(FileDescriptor fd, PrintWriter pw, String[] args) { - for (int i = mLog.size() - 1; i >= 0; i--) { - pw.println(mLog.get(i)); + Iterator<String> itr = mLog.descendingIterator(); + while (itr.hasNext()) { + pw.println(itr.next()); } } @@ -69,6 +74,9 @@ public final class LocalLog { public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { mLog.dump(fd, pw, args); } + public void reverseDump(FileDescriptor fd, PrintWriter pw, String[] args) { + mLog.reverseDump(fd, pw, args); + } } public ReadOnlyLocalLog readOnlyLocalLog() { |
