MetricsReader.java revision b62371434c9b63560c78a85123fe9386edac1205
1/*
2 * Copyright (C) 2017 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 */
16package android.metrics;
17
18import android.annotation.SystemApi;
19
20import com.android.internal.logging.legacy.LegacyConversionLogger;
21import com.android.internal.logging.legacy.EventLogCollector;
22
23import java.util.Queue;
24
25/**
26 * Read platform logs.
27 * @hide
28 */
29@SystemApi
30public class MetricsReader {
31    private EventLogCollector mReader;
32    private Queue<LogMaker> mEventQueue;
33    private long mLastEventMs;
34    private long mCheckpointMs;
35
36    /** Open a new session and start reading logs.
37     *
38     * Starts reading from the oldest log not already read by this reader object.
39     * On first invocation starts from the oldest available log ion the system.
40     */
41    public void read(long startMs) {
42        EventLogCollector reader = EventLogCollector.getInstance();
43        LegacyConversionLogger logger = new LegacyConversionLogger();
44        mLastEventMs = reader.collect(logger, startMs);
45        mEventQueue = logger.getEvents();
46    }
47
48    public void checkpoint() {
49        read(0L);
50        mCheckpointMs = mLastEventMs;
51        mEventQueue = null;
52    }
53
54    public void reset() {
55        read(mCheckpointMs);
56    }
57
58    /* Does the current log session have another entry? */
59    public boolean hasNext() {
60        return mEventQueue == null ? false : !mEventQueue.isEmpty();
61    }
62
63    /* Next entry in the current log session. */
64    public LogMaker next() {
65        return mEventQueue == null ? null : mEventQueue.remove();
66    }
67
68}
69