blob: 8b5659b3fa31fe5896c327e60aa0232ec09fddad [file] [log] [blame]
Robert Greenwalt470fd722012-01-18 12:51:15 -08001/*
2 * Copyright (C) 2006 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
17package android.util;
18
Mathew Inwoodb4075682018-08-14 17:32:44 +010019import android.annotation.UnsupportedAppUsage;
Hugo Benichid7be4c72018-12-17 10:52:17 +090020
Robert Greenwalt470fd722012-01-18 12:51:15 -080021import java.io.FileDescriptor;
22import java.io.PrintWriter;
Narayan Kamath6229d992017-06-09 13:35:48 +010023import java.time.LocalDateTime;
Hugo Benichide310db2016-10-04 11:36:11 +090024import java.util.ArrayDeque;
Narayan Kamath6229d992017-06-09 13:35:48 +010025import java.util.Deque;
26import java.util.Iterator;
Robert Greenwalt470fd722012-01-18 12:51:15 -080027
28/**
29 * @hide
30 */
31public final class LocalLog {
32
Hugo Benichide310db2016-10-04 11:36:11 +090033 private final Deque<String> mLog;
34 private final int mMaxLines;
Robert Greenwalt470fd722012-01-18 12:51:15 -080035
Mathew Inwoodb4075682018-08-14 17:32:44 +010036 @UnsupportedAppUsage
Robert Greenwalt470fd722012-01-18 12:51:15 -080037 public LocalLog(int maxLines) {
Hugo Benichide310db2016-10-04 11:36:11 +090038 mMaxLines = Math.max(0, maxLines);
39 mLog = new ArrayDeque<>(mMaxLines);
Robert Greenwalt470fd722012-01-18 12:51:15 -080040 }
41
Mathew Inwoodb4075682018-08-14 17:32:44 +010042 @UnsupportedAppUsage
Hugo Benichide310db2016-10-04 11:36:11 +090043 public void log(String msg) {
44 if (mMaxLines <= 0) {
45 return;
Robert Greenwalt470fd722012-01-18 12:51:15 -080046 }
Narayan Kamath6229d992017-06-09 13:35:48 +010047 append(String.format("%s - %s", LocalDateTime.now(), msg));
Hugo Benichide310db2016-10-04 11:36:11 +090048 }
49
50 private synchronized void append(String logLine) {
51 while (mLog.size() >= mMaxLines) {
52 mLog.remove();
53 }
54 mLog.add(logLine);
Robert Greenwalt470fd722012-01-18 12:51:15 -080055 }
56
Mathew Inwoodb4075682018-08-14 17:32:44 +010057 @UnsupportedAppUsage
Robert Greenwalt470fd722012-01-18 12:51:15 -080058 public synchronized void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Hugo Benichid7be4c72018-12-17 10:52:17 +090059 dump(pw);
60 }
61
62 public synchronized void dump(PrintWriter pw) {
Hugo Benichide310db2016-10-04 11:36:11 +090063 Iterator<String> itr = mLog.iterator();
Robert Greenwalt470fd722012-01-18 12:51:15 -080064 while (itr.hasNext()) {
65 pw.println(itr.next());
66 }
67 }
Robert Greenwalt22b4c6a2015-06-23 15:03:33 -070068
Erik Kline7523eb32015-07-09 18:24:03 +090069 public synchronized void reverseDump(FileDescriptor fd, PrintWriter pw, String[] args) {
Hugo Benichid7be4c72018-12-17 10:52:17 +090070 reverseDump(pw);
71 }
72
73 public synchronized void reverseDump(PrintWriter pw) {
Hugo Benichide310db2016-10-04 11:36:11 +090074 Iterator<String> itr = mLog.descendingIterator();
75 while (itr.hasNext()) {
76 pw.println(itr.next());
Erik Kline7523eb32015-07-09 18:24:03 +090077 }
78 }
79
Robert Greenwalt22b4c6a2015-06-23 15:03:33 -070080 public static class ReadOnlyLocalLog {
81 private final LocalLog mLog;
82 ReadOnlyLocalLog(LocalLog log) {
83 mLog = log;
84 }
Mathew Inwoodb4075682018-08-14 17:32:44 +010085 @UnsupportedAppUsage
Robert Greenwalt22b4c6a2015-06-23 15:03:33 -070086 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Hugo Benichid7be4c72018-12-17 10:52:17 +090087 mLog.dump(pw);
88 }
89 public void dump(PrintWriter pw) {
90 mLog.dump(pw);
Robert Greenwalt22b4c6a2015-06-23 15:03:33 -070091 }
Hugo Benichide310db2016-10-04 11:36:11 +090092 public void reverseDump(FileDescriptor fd, PrintWriter pw, String[] args) {
Hugo Benichid7be4c72018-12-17 10:52:17 +090093 mLog.reverseDump(pw);
94 }
95 public void reverseDump(PrintWriter pw) {
96 mLog.reverseDump(pw);
Hugo Benichide310db2016-10-04 11:36:11 +090097 }
Robert Greenwalt22b4c6a2015-06-23 15:03:33 -070098 }
99
Mathew Inwoodb4075682018-08-14 17:32:44 +0100100 @UnsupportedAppUsage
Robert Greenwalt22b4c6a2015-06-23 15:03:33 -0700101 public ReadOnlyLocalLog readOnlyLocalLog() {
102 return new ReadOnlyLocalLog(this);
103 }
Robert Greenwalt470fd722012-01-18 12:51:15 -0800104}