blob: 4c2afe8ba43ff05c48b19ef4637f2dc27f93dac0 [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/**
30 * Callback for LogReader
31 */
32class LogListener : public virtual android::RefBase
33{
34public:
35 LogListener();
36 virtual ~LogListener();
37
38 // TODO: Rather than using log_msg, which doesn't have any real internal structure
39 // here, we should pull this out into our own LogEntry class.
40 virtual void OnLogEvent(const log_msg& msg) = 0;
41};
42
43/**
44 * Class to read logs from logd.
45 */
46class LogReader : public virtual android::RefBase
47{
48public:
49 /**
50 * Construct the LogReader with a pointer back to the StatsService
51 */
52 LogReader();
53
54 /**
55 * Destructor.
56 */
57 virtual ~LogReader();
58
59 /**
60 * Add a LogListener class.
61 */
62 void AddListener(const android::sp<LogListener>& listener);
63
64 /**
65 * Run the main LogReader loop
66 */
67 void Run();
68
69private:
70 /**
71 * List of listeners to call back on when we do get an event.
72 */
73 std::vector<android::sp<LogListener> > m_listeners;
74
75 /**
76 * Connect to a single instance of logd, and read until there's a read error.
77 * Logd can crash, exit, be killed etc.
78 *
79 * Returns the number of lines that were read.
80 */
81 int connect_and_read();
82};
83
Bookatz906a35c2017-09-20 15:26:44 -070084} // namespace statsd
85} // namespace os
86} // namespace android
87
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070088#endif // LOGREADER_H