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