1 /* 2 * Copyright (C) 2012 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 com.android.internal.util; 18 19 import android.util.IndentingPrintWriter; 20 import android.util.Slog; 21 import android.util.proto.ProtoOutputStream; 22 23 import java.util.ArrayList; 24 25 /** 26 * Helper class for logging serious issues, which also keeps a small 27 * snapshot of the logged events that can be printed later, such as part 28 * of a system service's dumpsys output. 29 * @hide 30 */ 31 public class LocalLog { 32 private final String mTag; 33 private final int mMaxLines = 20; 34 private final ArrayList<String> mLines = new ArrayList<String>(mMaxLines); 35 LocalLog(String tag)36 public LocalLog(String tag) { 37 mTag = tag; 38 } 39 w(String msg)40 public void w(String msg) { 41 synchronized (mLines) { 42 Slog.w(mTag, msg); 43 if (mLines.size() >= mMaxLines) { 44 mLines.remove(0); 45 } 46 mLines.add(msg); 47 } 48 } 49 dump(IndentingPrintWriter pw, String header)50 public boolean dump(IndentingPrintWriter pw, String header) { 51 synchronized (mLines) { 52 if (mLines.size() <= 0) { 53 return false; 54 } 55 if (header != null) { 56 pw.println(header); 57 pw.increaseIndent(); 58 } 59 for (int i=0; i<mLines.size(); i++) { 60 pw.println(mLines.get(i)); 61 } 62 if (header != null) { 63 pw.decreaseIndent(); 64 } 65 return true; 66 } 67 } 68 dumpDebug(ProtoOutputStream proto, long fieldId)69 public void dumpDebug(ProtoOutputStream proto, long fieldId) { 70 final long token = proto.start(fieldId); 71 72 synchronized (mLines) { 73 for (int i = 0; i < mLines.size(); ++i) { 74 proto.write(LocalLogProto.LINES, mLines.get(i)); 75 } 76 } 77 78 proto.end(token); 79 } 80 } 81