Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 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.text.format.Time; |
| 20 | |
| 21 | import java.io.FileDescriptor; |
| 22 | import java.io.PrintWriter; |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 23 | import java.util.Iterator; |
| 24 | import java.util.LinkedList; |
| 25 | |
| 26 | /** |
| 27 | * @hide |
| 28 | */ |
| 29 | public final class LocalLog { |
| 30 | |
| 31 | private LinkedList<String> mLog; |
| 32 | private int mMaxLines; |
| 33 | private Time mNow; |
| 34 | |
| 35 | public LocalLog(int maxLines) { |
| 36 | mLog = new LinkedList<String>(); |
| 37 | mMaxLines = maxLines; |
| 38 | mNow = new Time(); |
| 39 | } |
| 40 | |
| 41 | public synchronized void log(String msg) { |
| 42 | if (mMaxLines > 0) { |
| 43 | mNow.setToNow(); |
| 44 | mLog.add(mNow.format("%H:%M:%S") + " - " + msg); |
| 45 | while (mLog.size() > mMaxLines) mLog.remove(); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | public synchronized void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 50 | Iterator<String> itr = mLog.listIterator(0); |
| 51 | while (itr.hasNext()) { |
| 52 | pw.println(itr.next()); |
| 53 | } |
| 54 | } |
| 55 | } |