blob: c9164f914cd2d3ea4de1b5090022a737e11525fb [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
34#define SNOOZE_MAX_MS (10 * 60 * 1000) // Ten minutes
35
36
37// ================================================================================
38LogListener::LogListener()
39{
40}
41
42LogListener::~LogListener()
43{
44}
45
46
47// ================================================================================
48LogReader::LogReader()
49{
50}
51
52LogReader::~LogReader()
53{
54}
55
56void
57LogReader::AddListener(const sp<LogListener>& listener)
58{
59 m_listeners.push_back(listener);
60}
61
62void
63LogReader::Run()
64{
65 int nextSnoozeMs = SNOOZE_INITIAL_MS;
66
67 // In an ideal world, this outer loop will only ever run one iteration, but it
68 // exists to handle crashes in logd. The inner loop inside connect_and_read()
69 // reads from logd forever, but if that read fails, we fall out to the outer
70 // loop, do the backoff (resetting the backoff timeout if we successfully read
71 // something), and then try again.
72 while (true) {
73 // Connect and read
74 int lineCount = connect_and_read();
75
76 // Figure out how long to sleep.
77 if (lineCount > 0) {
78 // If we managed to read at least one line, reset the backoff
79 nextSnoozeMs = SNOOZE_INITIAL_MS;
80 } else {
81 // Otherwise, expontial backoff
82 nextSnoozeMs *= 1.5f;
83 if (nextSnoozeMs > 10 * 60 * 1000) {
84 // Don't wait for toooo long.
85 nextSnoozeMs = SNOOZE_MAX_MS;
86 }
87 }
88
89 // Sleep
90 timespec ts;
91 timespec rem;
92 ts.tv_sec = nextSnoozeMs / 1000;
93 ts.tv_nsec = (nextSnoozeMs % 1000) * 1000000L;
94 while (nanosleep(&ts, &rem) == -1) {
95 if (errno == EINTR) {
96 ts = rem;
97 }
98 // other errors are basically impossible
99 }
100 }
101}
102
103int
104LogReader::connect_and_read()
105{
106 int lineCount = 0;
107 status_t err;
108 logger_list* loggers;
109 logger* eventLogger;
110
111 // Prepare the logging context
112 loggers = android_logger_list_alloc(ANDROID_LOG_RDONLY,
113 /* don't stop after N lines */ 0,
114 /* no pid restriction */ 0);
115
116 // Open the buffer(s)
Chenjie Yu7ca2e822017-09-07 13:02:47 -0700117 eventLogger = android_logger_open(loggers, LOG_ID_STATS);
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700118
119 // Read forever
120 if (eventLogger) {
121 while (true) {
122 log_msg msg;
123
124 // Read a message
125 err = android_logger_list_read(loggers, &msg);
126 if (err < 0) {
127 fprintf(stderr, "logcat read failure: %s\n", strerror(err));
128 break;
129 }
130
131 // Record that we read one (used above to know how to snooze).
132 lineCount++;
133
134 // Call the listeners
135 for (vector<sp<LogListener> >::iterator it = m_listeners.begin();
136 it != m_listeners.end(); it++) {
137 (*it)->OnLogEvent(msg);
138 }
139 }
140 }
141
142 // Free the logger list and close the individual loggers
143 android_logger_list_free(loggers);
144
145 return lineCount;
146}
147
Bookatz906a35c2017-09-20 15:26:44 -0700148} // namespace statsd
149} // namespace os
150} // namespace android