blob: adfa4fc555679f43152e374f2ffddd9588881301 [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 Inwood4eb56ab2018-08-14 17:24:32 +010019import android.annotation.UnsupportedAppUsage;
Robert Greenwalt470fd722012-01-18 12:51:15 -080020import java.io.FileDescriptor;
21import java.io.PrintWriter;
Narayan Kamath6229d992017-06-09 13:35:48 +010022import java.time.LocalDateTime;
Hugo Benichide310db2016-10-04 11:36:11 +090023import java.util.ArrayDeque;
Narayan Kamath6229d992017-06-09 13:35:48 +010024import java.util.Deque;
25import java.util.Iterator;
Robert Greenwalt470fd722012-01-18 12:51:15 -080026
27/**
28 * @hide
29 */
30public final class LocalLog {
31
Hugo Benichide310db2016-10-04 11:36:11 +090032 private final Deque<String> mLog;
33 private final int mMaxLines;
Robert Greenwalt470fd722012-01-18 12:51:15 -080034
Mathew Inwood4eb56ab2018-08-14 17:24:32 +010035 @UnsupportedAppUsage
Robert Greenwalt470fd722012-01-18 12:51:15 -080036 public LocalLog(int maxLines) {
Hugo Benichide310db2016-10-04 11:36:11 +090037 mMaxLines = Math.max(0, maxLines);
38 mLog = new ArrayDeque<>(mMaxLines);
Robert Greenwalt470fd722012-01-18 12:51:15 -080039 }
40
Mathew Inwood4eb56ab2018-08-14 17:24:32 +010041 @UnsupportedAppUsage
Hugo Benichide310db2016-10-04 11:36:11 +090042 public void log(String msg) {
43 if (mMaxLines <= 0) {
44 return;
Robert Greenwalt470fd722012-01-18 12:51:15 -080045 }
Narayan Kamath6229d992017-06-09 13:35:48 +010046 append(String.format("%s - %s", LocalDateTime.now(), msg));
Hugo Benichide310db2016-10-04 11:36:11 +090047 }
48
49 private synchronized void append(String logLine) {
50 while (mLog.size() >= mMaxLines) {
51 mLog.remove();
52 }
53 mLog.add(logLine);
Robert Greenwalt470fd722012-01-18 12:51:15 -080054 }
55
Mathew Inwood4eb56ab2018-08-14 17:24:32 +010056 @UnsupportedAppUsage
Robert Greenwalt470fd722012-01-18 12:51:15 -080057 public synchronized void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Hugo Benichide310db2016-10-04 11:36:11 +090058 Iterator<String> itr = mLog.iterator();
Robert Greenwalt470fd722012-01-18 12:51:15 -080059 while (itr.hasNext()) {
60 pw.println(itr.next());
61 }
62 }
Robert Greenwalt22b4c6a2015-06-23 15:03:33 -070063
Erik Kline7523eb32015-07-09 18:24:03 +090064 public synchronized void reverseDump(FileDescriptor fd, PrintWriter pw, String[] args) {
Hugo Benichide310db2016-10-04 11:36:11 +090065 Iterator<String> itr = mLog.descendingIterator();
66 while (itr.hasNext()) {
67 pw.println(itr.next());
Erik Kline7523eb32015-07-09 18:24:03 +090068 }
69 }
70
Robert Greenwalt22b4c6a2015-06-23 15:03:33 -070071 public static class ReadOnlyLocalLog {
72 private final LocalLog mLog;
73 ReadOnlyLocalLog(LocalLog log) {
74 mLog = log;
75 }
Mathew Inwood4eb56ab2018-08-14 17:24:32 +010076 @UnsupportedAppUsage
Robert Greenwalt22b4c6a2015-06-23 15:03:33 -070077 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
78 mLog.dump(fd, pw, args);
79 }
Hugo Benichide310db2016-10-04 11:36:11 +090080 public void reverseDump(FileDescriptor fd, PrintWriter pw, String[] args) {
81 mLog.reverseDump(fd, pw, args);
82 }
Robert Greenwalt22b4c6a2015-06-23 15:03:33 -070083 }
84
Mathew Inwood4eb56ab2018-08-14 17:24:32 +010085 @UnsupportedAppUsage
Robert Greenwalt22b4c6a2015-06-23 15:03:33 -070086 public ReadOnlyLocalLog readOnlyLocalLog() {
87 return new ReadOnlyLocalLog(this);
88 }
Robert Greenwalt470fd722012-01-18 12:51:15 -080089}