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