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 java.io.PrintWriter; 20 import java.util.ArrayList; 21 22 import android.util.Slog; 23 24 /** 25 * Helper class for logging serious issues, which also keeps a small 26 * snapshot of the logged events that can be printed later, such as part 27 * of a system service's dumpsys output. 28 * @hide 29 */ 30 public class LocalLog { 31 private final String mTag; 32 private final int mMaxLines = 20; 33 private final ArrayList<String> mLines = new ArrayList<String>(mMaxLines); 34 35 public LocalLog(String tag) { 36 mTag = tag; 37 } 38 39 public void w(String msg) { 40 synchronized (mLines) { 41 Slog.w(mTag, msg); 42 if (mLines.size() >= mMaxLines) { 43 mLines.remove(0); 44 } 45 mLines.add(msg); 46 } 47 } 48 49 public boolean dump(PrintWriter pw, String header, String prefix) { 50 synchronized (mLines) { 51 if (mLines.size() <= 0) { 52 return false; 53 } 54 if (header != null) { 55 pw.println(header); 56 } 57 for (int i=0; i<mLines.size(); i++) { 58 if (prefix != null) { 59 pw.print(prefix); 60 } 61 pw.println(mLines.get(i)); 62 } 63 return true; 64 } 65 } 66 } 67