Home | History | Annotate | Download | only in util
      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
     37     public LocalLog(int maxLines) {
     38         mMaxLines = Math.max(0, maxLines);
     39         mLog = new ArrayDeque<>(mMaxLines);
     40     }
     41 
     42     @UnsupportedAppUsage
     43     public void log(String msg) {
     44         if (mMaxLines <= 0) {
     45             return;
     46         }
     47         append(String.format("%s - %s", LocalDateTime.now(), msg));
     48     }
     49 
     50     private synchronized void append(String logLine) {
     51         while (mLog.size() >= mMaxLines) {
     52             mLog.remove();
     53         }
     54         mLog.add(logLine);
     55     }
     56 
     57     @UnsupportedAppUsage
     58     public synchronized void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
     59         dump(pw);
     60     }
     61 
     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 
     69     public synchronized void reverseDump(FileDescriptor fd, PrintWriter pw, String[] args) {
     70         reverseDump(pw);
     71     }
     72 
     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;
     82         ReadOnlyLocalLog(LocalLog log) {
     83             mLog = log;
     84         }
     85         @UnsupportedAppUsage
     86         public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
     87             mLog.dump(pw);
     88         }
     89         public void dump(PrintWriter pw) {
     90             mLog.dump(pw);
     91         }
     92         public void reverseDump(FileDescriptor fd, PrintWriter pw, String[] args) {
     93             mLog.reverseDump(pw);
     94         }
     95         public void reverseDump(PrintWriter pw) {
     96             mLog.reverseDump(pw);
     97         }
     98     }
     99 
    100     @UnsupportedAppUsage
    101     public ReadOnlyLocalLog readOnlyLocalLog() {
    102         return new ReadOnlyLocalLog(this);
    103     }
    104 }
    105