1470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt/*
2470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt * Copyright (C) 2006 The Android Open Source Project
3470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt *
4470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt * Licensed under the Apache License, Version 2.0 (the "License");
5470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt * you may not use this file except in compliance with the License.
6470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt * You may obtain a copy of the License at
7470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt *
8470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt *      http://www.apache.org/licenses/LICENSE-2.0
9470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt *
10470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt * Unless required by applicable law or agreed to in writing, software
11470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt * distributed under the License is distributed on an "AS IS" BASIS,
12470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt * See the License for the specific language governing permissions and
14470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt * limitations under the License.
15470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt */
16470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt
17470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwaltpackage android.util;
18470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt
19470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwaltimport android.text.format.Time;
20470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt
21470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwaltimport java.io.FileDescriptor;
22470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwaltimport java.io.PrintWriter;
23470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwaltimport java.io.StringWriter;
24470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwaltimport java.util.Iterator;
25470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwaltimport java.util.LinkedList;
26470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt
27470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt/**
28470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt * @hide
29470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt */
30470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwaltpublic final class LocalLog {
31470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt
32470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt    private LinkedList<String> mLog;
33470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt    private int mMaxLines;
34470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt    private Time mNow;
35470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt
36470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt    public LocalLog(int maxLines) {
37470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt        mLog = new LinkedList<String>();
38470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt        mMaxLines = maxLines;
39470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt        mNow = new Time();
40470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt    }
41470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt
42470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt    public synchronized void log(String msg) {
43470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt        if (mMaxLines > 0) {
44470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt            mNow.setToNow();
45470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt            mLog.add(mNow.format("%H:%M:%S") + " - " + msg);
46470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt            while (mLog.size() > mMaxLines) mLog.remove();
47470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt        }
48470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt    }
49470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt
50470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt    public synchronized void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
51470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt        Iterator<String> itr = mLog.listIterator(0);
52470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt        while (itr.hasNext()) {
53470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt            pw.println(itr.next());
54470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt        }
55470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt    }
56470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt}
57