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.util.Calendar;
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 long mNow;
35
36    public LocalLog(int maxLines) {
37        mLog = new LinkedList<String>();
38        mMaxLines = maxLines;
39    }
40
41    public synchronized void log(String msg) {
42        if (mMaxLines > 0) {
43            mNow = System.currentTimeMillis();
44            StringBuilder sb = new StringBuilder();
45            Calendar c = Calendar.getInstance();
46            c.setTimeInMillis(mNow);
47            sb.append(String.format("%tm-%td %tH:%tM:%tS.%tL", c, c, c, c, c, c));
48            mLog.add(sb.toString() + " - " + msg);
49            while (mLog.size() > mMaxLines) mLog.remove();
50        }
51    }
52
53    public synchronized void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
54        Iterator<String> itr = mLog.listIterator(0);
55        while (itr.hasNext()) {
56            pw.println(itr.next());
57        }
58    }
59}
60