1 /* 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 17 package android.util; 18 19 import android.annotation.UnsupportedAppUsage; 20 21 import java.io.FileDescriptor; 22 import java.io.PrintWriter; 23 import java.time.LocalDateTime; 24 import java.util.ArrayDeque; 25 import java.util.Deque; 26 import java.util.Iterator; 27 28 /** 29 * @hide 30 */ 31 public final class LocalLog { 32 33 private final Deque<String> mLog; 34 private final int mMaxLines; 35 36 @UnsupportedAppUsage LocalLog(int maxLines)37 public LocalLog(int maxLines) { 38 mMaxLines = Math.max(0, maxLines); 39 mLog = new ArrayDeque<>(mMaxLines); 40 } 41 42 @UnsupportedAppUsage log(String msg)43 public void log(String msg) { 44 if (mMaxLines <= 0) { 45 return; 46 } 47 append(String.format("%s - %s", LocalDateTime.now(), msg)); 48 } 49 append(String logLine)50 private synchronized void append(String logLine) { 51 while (mLog.size() >= mMaxLines) { 52 mLog.remove(); 53 } 54 mLog.add(logLine); 55 } 56 57 @UnsupportedAppUsage dump(FileDescriptor fd, PrintWriter pw, String[] args)58 public synchronized void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 59 dump(pw); 60 } 61 dump(PrintWriter pw)62 public synchronized void dump(PrintWriter pw) { 63 Iterator<String> itr = mLog.iterator(); 64 while (itr.hasNext()) { 65 pw.println(itr.next()); 66 } 67 } 68 reverseDump(FileDescriptor fd, PrintWriter pw, String[] args)69 public synchronized void reverseDump(FileDescriptor fd, PrintWriter pw, String[] args) { 70 reverseDump(pw); 71 } 72 reverseDump(PrintWriter pw)73 public synchronized void reverseDump(PrintWriter pw) { 74 Iterator<String> itr = mLog.descendingIterator(); 75 while (itr.hasNext()) { 76 pw.println(itr.next()); 77 } 78 } 79 80 public static class ReadOnlyLocalLog { 81 private final LocalLog mLog; ReadOnlyLocalLog(LocalLog log)82 ReadOnlyLocalLog(LocalLog log) { 83 mLog = log; 84 } 85 @UnsupportedAppUsage dump(FileDescriptor fd, PrintWriter pw, String[] args)86 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 87 mLog.dump(pw); 88 } dump(PrintWriter pw)89 public void dump(PrintWriter pw) { 90 mLog.dump(pw); 91 } reverseDump(FileDescriptor fd, PrintWriter pw, String[] args)92 public void reverseDump(FileDescriptor fd, PrintWriter pw, String[] args) { 93 mLog.reverseDump(pw); 94 } reverseDump(PrintWriter pw)95 public void reverseDump(PrintWriter pw) { 96 mLog.reverseDump(pw); 97 } 98 } 99 100 @UnsupportedAppUsage readOnlyLocalLog()101 public ReadOnlyLocalLog readOnlyLocalLog() { 102 return new ReadOnlyLocalLog(this); 103 } 104 } 105