blob: c4ac33724bc729626a7806e5514fd4d06676bab6 [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#include "LogReader.h"
18
19#include <log/log_read.h>
20
21#include <utils/Errors.h>
22
23#include <time.h>
24#include <unistd.h>
25
26using namespace android;
27using namespace std;
28
Bookatz906a35c2017-09-20 15:26:44 -070029namespace android {
30namespace os {
31namespace statsd {
32
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070033#define SNOOZE_INITIAL_MS 100
Yao Chenef99c4f2017-09-22 16:26:54 -070034#define SNOOZE_MAX_MS (10 * 60 * 1000) // Ten minutes
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070035
36// ================================================================================
Yao Chenef99c4f2017-09-22 16:26:54 -070037LogListener::LogListener() {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070038}
39
Yao Chenef99c4f2017-09-22 16:26:54 -070040LogListener::~LogListener() {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070041}
42
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070043// ================================================================================
Yao Chenef99c4f2017-09-22 16:26:54 -070044LogReader::LogReader() {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070045}
46
Yao Chenef99c4f2017-09-22 16:26:54 -070047LogReader::~LogReader() {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070048}
49
Yao Chenef99c4f2017-09-22 16:26:54 -070050void LogReader::AddListener(const sp<LogListener>& listener) {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070051 m_listeners.push_back(listener);
52}
53
Yao Chenef99c4f2017-09-22 16:26:54 -070054void LogReader::Run() {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070055 int nextSnoozeMs = SNOOZE_INITIAL_MS;
56
57 // In an ideal world, this outer loop will only ever run one iteration, but it
58 // exists to handle crashes in logd. The inner loop inside connect_and_read()
59 // reads from logd forever, but if that read fails, we fall out to the outer
60 // loop, do the backoff (resetting the backoff timeout if we successfully read
61 // something), and then try again.
62 while (true) {
63 // Connect and read
64 int lineCount = connect_and_read();
65
66 // Figure out how long to sleep.
67 if (lineCount > 0) {
68 // If we managed to read at least one line, reset the backoff
69 nextSnoozeMs = SNOOZE_INITIAL_MS;
70 } else {
71 // Otherwise, expontial backoff
72 nextSnoozeMs *= 1.5f;
73 if (nextSnoozeMs > 10 * 60 * 1000) {
74 // Don't wait for toooo long.
75 nextSnoozeMs = SNOOZE_MAX_MS;
76 }
77 }
78
79 // Sleep
80 timespec ts;
81 timespec rem;
82 ts.tv_sec = nextSnoozeMs / 1000;
83 ts.tv_nsec = (nextSnoozeMs % 1000) * 1000000L;
84 while (nanosleep(&ts, &rem) == -1) {
85 if (errno == EINTR) {
86 ts = rem;
87 }
88 // other errors are basically impossible
89 }
90 }
91}
92
Yao Chenef99c4f2017-09-22 16:26:54 -070093int LogReader::connect_and_read() {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070094 int lineCount = 0;
95 status_t err;
96 logger_list* loggers;
97 logger* eventLogger;
98
99 // Prepare the logging context
100 loggers = android_logger_list_alloc(ANDROID_LOG_RDONLY,
Yao Chenef99c4f2017-09-22 16:26:54 -0700101 /* don't stop after N lines */ 0,
102 /* no pid restriction */ 0);
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700103
104 // Open the buffer(s)
Chenjie Yu7ca2e822017-09-07 13:02:47 -0700105 eventLogger = android_logger_open(loggers, LOG_ID_STATS);
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700106
107 // Read forever
108 if (eventLogger) {
109 while (true) {
110 log_msg msg;
111
112 // Read a message
113 err = android_logger_list_read(loggers, &msg);
114 if (err < 0) {
115 fprintf(stderr, "logcat read failure: %s\n", strerror(err));
116 break;
117 }
118
119 // Record that we read one (used above to know how to snooze).
120 lineCount++;
121
122 // Call the listeners
123 for (vector<sp<LogListener> >::iterator it = m_listeners.begin();
Yao Chenef99c4f2017-09-22 16:26:54 -0700124 it != m_listeners.end(); it++) {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700125 (*it)->OnLogEvent(msg);
126 }
127 }
128 }
129
130 // Free the logger list and close the individual loggers
131 android_logger_list_free(loggers);
132
133 return lineCount;
134}
135
Yao Chenef99c4f2017-09-22 16:26:54 -0700136} // namespace statsd
137} // namespace os
138} // namespace android