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;
23c520aa89a33ddb7c065ef06bcd2d2dbf96e1f989vandwalleimport java.util.Calendar;
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;
34c520aa89a33ddb7c065ef06bcd2d2dbf96e1f989vandwalle    private long mNow;
35470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt
36470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt    public LocalLog(int maxLines) {
37470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt        mLog = new LinkedList<String>();
38470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt        mMaxLines = maxLines;
39470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt    }
40470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt
41470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt    public synchronized void log(String msg) {
42470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt        if (mMaxLines > 0) {
43c520aa89a33ddb7c065ef06bcd2d2dbf96e1f989vandwalle            mNow = System.currentTimeMillis();
44c520aa89a33ddb7c065ef06bcd2d2dbf96e1f989vandwalle            StringBuilder sb = new StringBuilder();
45c520aa89a33ddb7c065ef06bcd2d2dbf96e1f989vandwalle            Calendar c = Calendar.getInstance();
46c520aa89a33ddb7c065ef06bcd2d2dbf96e1f989vandwalle            c.setTimeInMillis(mNow);
47c520aa89a33ddb7c065ef06bcd2d2dbf96e1f989vandwalle            sb.append(String.format("%tm-%td %tH:%tM:%tS.%tL", c, c, c, c, c, c));
48c520aa89a33ddb7c065ef06bcd2d2dbf96e1f989vandwalle            mLog.add(sb.toString() + " - " + msg);
49470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt            while (mLog.size() > mMaxLines) mLog.remove();
50470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt        }
51470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt    }
52470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt
53470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt    public synchronized void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
54470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt        Iterator<String> itr = mLog.listIterator(0);
55470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt        while (itr.hasNext()) {
56470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt            pw.println(itr.next());
57470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt        }
58470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt    }
59470fd72a06390d7a6b854583afd0ed76ce0a03eeRobert Greenwalt}
60