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