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