Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 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 | */ |
| 16 | |
| 17 | #ifndef LOGREADER_H |
| 18 | #define LOGREADER_H |
| 19 | |
| 20 | #include <log/log_read.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 21 | #include <utils/RefBase.h> |
| 22 | |
| 23 | #include <vector> |
| 24 | |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 25 | namespace android { |
| 26 | namespace os { |
| 27 | namespace statsd { |
| 28 | |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 29 | /** |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 30 | * Callback for LogReader |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 31 | */ |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 32 | class LogListener : public virtual android::RefBase { |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 33 | public: |
| 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 Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 45 | class LogReader : public virtual android::RefBase { |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 46 | public: |
| 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 Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 62 | /** |
| 63 | * Run the main LogReader loop |
| 64 | */ |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 65 | void Run(); |
| 66 | |
| 67 | private: |
| 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 Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 82 | } // namespace statsd |
| 83 | } // namespace os |
| 84 | } // namespace android |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 85 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 86 | #endif // LOGREADER_H |