blob: fc19585ac6f04a47c2ce6560a4ea5f89fa2b5129 [file] [log] [blame]
Joe Onorato5dcbc6c2017-08-29 15:13:58 -07001/*
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 */
16
17#ifndef LOGREADER_H
18#define LOGREADER_H
19
20#include <log/log_read.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070021#include <utils/RefBase.h>
22
23#include <vector>
24
Bookatz906a35c2017-09-20 15:26:44 -070025namespace android {
26namespace os {
27namespace statsd {
28
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070029/**
Yao Chenef99c4f2017-09-22 16:26:54 -070030 * Callback for LogReader
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070031 */
Yao Chenef99c4f2017-09-22 16:26:54 -070032class LogListener : public virtual android::RefBase {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070033public:
34 LogListener();
35 virtual ~LogListener();
36
37 // TODO: Rather than using log_msg, which doesn't have any real internal structure
38 // here, we should pull this out into our own LogEntry class.
39 virtual void OnLogEvent(const log_msg& msg) = 0;
40};
41
42/**
43 * Class to read logs from logd.
44 */
Yao Chenef99c4f2017-09-22 16:26:54 -070045class LogReader : public virtual android::RefBase {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070046public:
47 /**
48 * Construct the LogReader with a pointer back to the StatsService
49 */
50 LogReader();
51
52 /**
53 * Destructor.
54 */
55 virtual ~LogReader();
56
57 /**
58 * Add a LogListener class.
59 */
60 void AddListener(const android::sp<LogListener>& listener);
61
Yao Chenef99c4f2017-09-22 16:26:54 -070062 /**
63 * Run the main LogReader loop
64 */
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070065 void Run();
66
67private:
68 /**
69 * List of listeners to call back on when we do get an event.
70 */
71 std::vector<android::sp<LogListener> > m_listeners;
72
73 /**
74 * Connect to a single instance of logd, and read until there's a read error.
75 * Logd can crash, exit, be killed etc.
76 *
77 * Returns the number of lines that were read.
78 */
79 int connect_and_read();
80};
81
Yao Chenef99c4f2017-09-22 16:26:54 -070082} // namespace statsd
83} // namespace os
84} // namespace android
Bookatz906a35c2017-09-20 15:26:44 -070085
Yao Chenef99c4f2017-09-22 16:26:54 -070086#endif // LOGREADER_H