blob: d8ffb02aa52b1712480311a09f08666aea0ae0ff [file] [log] [blame]
Mark Salyzyn0175b072014-02-26 09:50:16 -08001/*
2 * Copyright (C) 2012-2014 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
Mark Salyzyn671e3432014-05-06 07:34:59 -070017#include <ctype.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080018#include <stdio.h>
19#include <string.h>
Mark Salyzyn57a0af92014-05-09 17:44:18 -070020#include <sys/user.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080021#include <time.h>
22#include <unistd.h>
23
Mark Salyzyn671e3432014-05-06 07:34:59 -070024#include <cutils/properties.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080025#include <log/logger.h>
26
27#include "LogBuffer.h"
Mark Salyzyn671e3432014-05-06 07:34:59 -070028#include "LogReader.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080029
Mark Salyzyndfa7a072014-02-11 12:29:31 -080030// Default
Mark Salyzyn0175b072014-02-26 09:50:16 -080031#define LOG_BUFFER_SIZE (256 * 1024) // Tuned on a per-platform basis here?
Mark Salyzyndfa7a072014-02-11 12:29:31 -080032#define log_buffer_size(id) mMaxSize[id]
Mark Salyzyn57a0af92014-05-09 17:44:18 -070033#define LOG_BUFFER_MIN_SIZE (64 * 1024UL)
34#define LOG_BUFFER_MAX_SIZE (256 * 1024 * 1024UL)
35
36static bool valid_size(unsigned long value) {
37 if ((value < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < value)) {
38 return false;
39 }
40
41 long pages = sysconf(_SC_PHYS_PAGES);
42 if (pages < 1) {
43 return true;
44 }
45
46 long pagesize = sysconf(_SC_PAGESIZE);
47 if (pagesize <= 1) {
48 pagesize = PAGE_SIZE;
49 }
50
51 // maximum memory impact a somewhat arbitrary ~3%
52 pages = (pages + 31) / 32;
53 unsigned long maximum = pages * pagesize;
54
55 if ((maximum < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < maximum)) {
56 return true;
57 }
58
59 return value <= maximum;
60}
Mark Salyzyn0175b072014-02-26 09:50:16 -080061
Mark Salyzyn671e3432014-05-06 07:34:59 -070062static unsigned long property_get_size(const char *key) {
63 char property[PROPERTY_VALUE_MAX];
64 property_get(key, property, "");
65
66 char *cp;
67 unsigned long value = strtoul(property, &cp, 10);
68
69 switch(*cp) {
70 case 'm':
71 case 'M':
72 value *= 1024;
73 /* FALLTHRU */
74 case 'k':
75 case 'K':
76 value *= 1024;
77 /* FALLTHRU */
78 case '\0':
79 break;
80
81 default:
82 value = 0;
83 }
84
Mark Salyzyn57a0af92014-05-09 17:44:18 -070085 if (!valid_size(value)) {
86 value = 0;
87 }
88
Mark Salyzyn671e3432014-05-06 07:34:59 -070089 return value;
90}
91
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070092void LogBuffer::init() {
Mark Salyzyn57a0af92014-05-09 17:44:18 -070093 static const char global_tuneable[] = "persist.logd.size"; // Settings App
94 static const char global_default[] = "ro.logd.size"; // BoardConfig.mk
95
96 unsigned long default_size = property_get_size(global_tuneable);
97 if (!default_size) {
98 default_size = property_get_size(global_default);
99 }
Mark Salyzyn671e3432014-05-06 07:34:59 -0700100
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800101 log_id_for_each(i) {
Mark Salyzyn671e3432014-05-06 07:34:59 -0700102 char key[PROP_NAME_MAX];
Mark Salyzyn671e3432014-05-06 07:34:59 -0700103
Mark Salyzyn57a0af92014-05-09 17:44:18 -0700104 snprintf(key, sizeof(key), "%s.%s",
105 global_tuneable, android_log_id_to_name(i));
106 unsigned long property_size = property_get_size(key);
107
108 if (!property_size) {
109 snprintf(key, sizeof(key), "%s.%s",
110 global_default, android_log_id_to_name(i));
111 property_size = property_get_size(key);
112 }
113
114 if (!property_size) {
115 property_size = default_size;
116 }
117
118 if (!property_size) {
119 property_size = LOG_BUFFER_SIZE;
120 }
121
122 if (setSize(i, property_size)) {
123 setSize(i, LOG_BUFFER_MIN_SIZE);
124 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800125 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800126}
127
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700128LogBuffer::LogBuffer(LastLogTimes *times)
129 : mTimes(*times) {
130 pthread_mutex_init(&mLogElementsLock, NULL);
131
132 init();
133}
134
Mark Salyzyn7e2f83c2014-03-05 07:41:49 -0800135void LogBuffer::log(log_id_t log_id, log_time realtime,
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700136 uid_t uid, pid_t pid, pid_t tid,
137 const char *msg, unsigned short len) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800138 if ((log_id >= LOG_ID_MAX) || (log_id < 0)) {
139 return;
140 }
141 LogBufferElement *elem = new LogBufferElement(log_id, realtime,
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700142 uid, pid, tid, msg, len);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800143
144 pthread_mutex_lock(&mLogElementsLock);
145
146 // Insert elements in time sorted order if possible
147 // NB: if end is region locked, place element at end of list
148 LogBufferElementCollection::iterator it = mLogElements.end();
149 LogBufferElementCollection::iterator last = it;
Mark Salyzyneae155e2014-10-13 16:49:47 -0700150 while (last != mLogElements.begin()) {
151 --it;
Mark Salyzync03e72c2014-02-18 11:23:53 -0800152 if ((*it)->getRealTime() <= realtime) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800153 break;
154 }
155 last = it;
156 }
Mark Salyzync03e72c2014-02-18 11:23:53 -0800157
Mark Salyzyn0175b072014-02-26 09:50:16 -0800158 if (last == mLogElements.end()) {
159 mLogElements.push_back(elem);
160 } else {
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800161 uint64_t end = 1;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800162 bool end_set = false;
163 bool end_always = false;
164
165 LogTimeEntry::lock();
166
167 LastLogTimes::iterator t = mTimes.begin();
168 while(t != mTimes.end()) {
169 LogTimeEntry *entry = (*t);
170 if (entry->owned_Locked()) {
171 if (!entry->mNonBlock) {
172 end_always = true;
173 break;
174 }
175 if (!end_set || (end <= entry->mEnd)) {
176 end = entry->mEnd;
177 end_set = true;
178 }
179 }
180 t++;
181 }
182
183 if (end_always
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800184 || (end_set && (end >= (*last)->getSequence()))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800185 mLogElements.push_back(elem);
186 } else {
187 mLogElements.insert(last,elem);
188 }
189
190 LogTimeEntry::unlock();
191 }
192
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700193 stats.add(elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800194 maybePrune(log_id);
195 pthread_mutex_unlock(&mLogElementsLock);
196}
197
198// If we're using more than 256K of memory for log entries, prune
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800199// at least 10% of the log entries.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800200//
201// mLogElementsLock must be held when this function is called.
202void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800203 size_t sizes = stats.sizes(id);
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800204 if (sizes > log_buffer_size(id)) {
205 size_t sizeOver90Percent = sizes - ((log_buffer_size(id) * 9) / 10);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800206 size_t elements = stats.elements(id);
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800207 unsigned long pruneRows = elements * sizeOver90Percent / sizes;
208 elements /= 10;
209 if (pruneRows <= elements) {
210 pruneRows = elements;
211 }
212 prune(id, pruneRows);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800213 }
214}
215
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700216LogBufferElementCollection::iterator LogBuffer::erase(LogBufferElementCollection::iterator it) {
217 LogBufferElement *e = *it;
218
219 it = mLogElements.erase(it);
220 stats.subtract(e);
221 delete e;
222
223 return it;
224}
225
Mark Salyzyn0175b072014-02-26 09:50:16 -0800226// prune "pruneRows" of type "id" from the buffer.
227//
228// mLogElementsLock must be held when this function is called.
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700229void LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800230 LogTimeEntry *oldest = NULL;
231
232 LogTimeEntry::lock();
233
234 // Region locked?
235 LastLogTimes::iterator t = mTimes.begin();
236 while(t != mTimes.end()) {
237 LogTimeEntry *entry = (*t);
TraianX Schiauda6495d2014-12-17 10:53:41 +0200238 if (entry->owned_Locked() && entry->isWatching(id)
Mark Salyzyn0175b072014-02-26 09:50:16 -0800239 && (!oldest || (oldest->mStart > entry->mStart))) {
240 oldest = entry;
241 }
242 t++;
243 }
244
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800245 LogBufferElementCollection::iterator it;
246
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700247 if (caller_uid != AID_ROOT) {
248 for(it = mLogElements.begin(); it != mLogElements.end();) {
249 LogBufferElement *e = *it;
250
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800251 if (oldest && (oldest->mStart <= e->getSequence())) {
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700252 break;
253 }
254
255 if (e->getLogId() != id) {
256 ++it;
257 continue;
258 }
259
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700260 if (e->getUid() == caller_uid) {
261 it = erase(it);
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700262 pruneRows--;
263 if (pruneRows == 0) {
264 break;
265 }
266 } else {
267 ++it;
268 }
269 }
270 LogTimeEntry::unlock();
271 return;
272 }
273
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800274 // prune by worst offender by uid
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700275 bool hasBlacklist = mPrune.naughty();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800276 while (pruneRows > 0) {
277 // recalculate the worst offender on every batched pass
278 uid_t worst = (uid_t) -1;
279 size_t worst_sizes = 0;
280 size_t second_worst_sizes = 0;
281
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700282 if ((id != LOG_ID_CRASH) && mPrune.worstUidEnabled()) {
Mark Salyzyn720f6d12015-03-16 08:26:05 -0700283 std::unique_ptr<const UidEntry *[]> sorted = stats.sort(2, id);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700284
Mark Salyzyn720f6d12015-03-16 08:26:05 -0700285 if (sorted.get()) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700286 if (sorted[0] && sorted[1]) {
287 worst = sorted[0]->getKey();
288 worst_sizes = sorted[0]->getSizes();
289 second_worst_sizes = sorted[1]->getSizes();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800290 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800291 }
292 }
293
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700294 // skip if we have neither worst nor naughty filters
295 if ((worst == (uid_t) -1) && !hasBlacklist) {
296 break;
297 }
298
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800299 bool kick = false;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700300 bool leading = true;
301 LogBufferElement *last = NULL;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800302 for(it = mLogElements.begin(); it != mLogElements.end();) {
303 LogBufferElement *e = *it;
304
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800305 if (oldest && (oldest->mStart <= e->getSequence())) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800306 break;
307 }
308
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800309 if (e->getLogId() != id) {
310 ++it;
311 continue;
312 }
313
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700314 unsigned short dropped = e->getDropped();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800315
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700316 // remove any leading drops
317 if (leading && dropped) {
318 it = erase(it);
319 continue;
320 }
321
322 pid_t pid = e->getPid();
323
324 // merge any drops
325 if (last && dropped
326 && ((dropped + last->getDropped()) < USHRT_MAX)
327 && (last->getPid() == pid)
328 && (last->getTid() == e->getTid())) {
329 it = mLogElements.erase(it);
330 stats.erase(e);
331 delete e;
332 last->setDropped(dropped + last->getDropped());
333 continue;
334 }
335
336 leading = false;
337
338 if (hasBlacklist && mPrune.naughty(e)) {
339 last = NULL;
340 it = erase(it);
341 if (dropped) {
342 continue;
343 }
344
345 pruneRows--;
346 if (pruneRows == 0) {
347 break;
348 }
349
350 if (e->getUid() == worst) {
351 kick = true;
352 if (worst_sizes < second_worst_sizes) {
353 break;
354 }
355 worst_sizes -= e->getMsgLen();
356 }
357 continue;
358 }
359
360 if (dropped) {
361 last = e;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800362 ++it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700363 continue;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800364 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700365
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700366 if (e->getUid() != worst) {
367 last = NULL;
368 ++it;
369 continue;
370 }
371
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700372 pruneRows--;
373 if (pruneRows == 0) {
374 break;
375 }
376
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700377 kick = true;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700378
379 unsigned short len = e->getMsgLen();
380 stats.drop(e);
381 e->setDropped(1);
382 // merge any drops
383 if (last
384 && (last->getDropped() < (USHRT_MAX - 1))
385 && (last->getPid() == pid)
386 && (last->getTid() == e->getTid())) {
387 it = mLogElements.erase(it);
388 stats.erase(e);
389 delete e;
390 last->setDropped(last->getDropped() + 1);
391 } else {
392 last = e;
393 ++it;
394 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700395 if (worst_sizes < second_worst_sizes) {
396 break;
397 }
398 worst_sizes -= len;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800399 }
400
Mark Salyzyn1c950472014-04-01 17:19:47 -0700401 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800402 break; // the following loop will ask bad clients to skip/drop
403 }
404 }
405
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800406 bool whitelist = false;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700407 bool hasWhitelist = mPrune.nice();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800408 it = mLogElements.begin();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800409 while((pruneRows > 0) && (it != mLogElements.end())) {
410 LogBufferElement *e = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700411
412 if (e->getLogId() != id) {
413 it++;
414 continue;
415 }
416
417 if (oldest && (oldest->mStart <= e->getSequence())) {
418 if (whitelist) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800419 break;
420 }
Mark Salyzyn1c950472014-04-01 17:19:47 -0700421
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700422 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
423 // kick a misbehaving log reader client off the island
424 oldest->release_Locked();
425 } else {
426 oldest->triggerSkip_Locked(id, pruneRows);
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800427 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700428 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800429 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700430
431 if (hasWhitelist && mPrune.nice(e)) { // WhiteListed
432 whitelist = true;
433 it++;
434 continue;
435 }
436
437 it = erase(it);
438 pruneRows--;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800439 }
440
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700441 // Do not save the whitelist if we are reader range limited
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800442 if (whitelist && (pruneRows > 0)) {
443 it = mLogElements.begin();
444 while((it != mLogElements.end()) && (pruneRows > 0)) {
445 LogBufferElement *e = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700446
447 if (e->getLogId() != id) {
448 ++it;
449 continue;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800450 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700451
452 if (oldest && (oldest->mStart <= e->getSequence())) {
453 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
454 // kick a misbehaving log reader client off the island
455 oldest->release_Locked();
456 } else {
457 oldest->triggerSkip_Locked(id, pruneRows);
458 }
459 break;
460 }
461
462 it = erase(it);
463 pruneRows--;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800464 }
465 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800466
Mark Salyzyn0175b072014-02-26 09:50:16 -0800467 LogTimeEntry::unlock();
468}
469
470// clear all rows of type "id" from the buffer.
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700471void LogBuffer::clear(log_id_t id, uid_t uid) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800472 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700473 prune(id, ULONG_MAX, uid);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800474 pthread_mutex_unlock(&mLogElementsLock);
475}
476
477// get the used space associated with "id".
478unsigned long LogBuffer::getSizeUsed(log_id_t id) {
479 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800480 size_t retval = stats.sizes(id);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800481 pthread_mutex_unlock(&mLogElementsLock);
482 return retval;
483}
484
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800485// set the total space allocated to "id"
486int LogBuffer::setSize(log_id_t id, unsigned long size) {
487 // Reasonable limits ...
Mark Salyzyn57a0af92014-05-09 17:44:18 -0700488 if (!valid_size(size)) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800489 return -1;
490 }
491 pthread_mutex_lock(&mLogElementsLock);
492 log_buffer_size(id) = size;
493 pthread_mutex_unlock(&mLogElementsLock);
494 return 0;
495}
496
497// get the total space allocated to "id"
498unsigned long LogBuffer::getSize(log_id_t id) {
499 pthread_mutex_lock(&mLogElementsLock);
500 size_t retval = log_buffer_size(id);
501 pthread_mutex_unlock(&mLogElementsLock);
502 return retval;
503}
504
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800505uint64_t LogBuffer::flushTo(
506 SocketClient *reader, const uint64_t start, bool privileged,
507 int (*filter)(const LogBufferElement *element, void *arg), void *arg) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800508 LogBufferElementCollection::iterator it;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800509 uint64_t max = start;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800510 uid_t uid = reader->getUid();
511
512 pthread_mutex_lock(&mLogElementsLock);
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600513
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800514 if (start <= 1) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600515 // client wants to start from the beginning
516 it = mLogElements.begin();
517 } else {
518 // Client wants to start from some specified time. Chances are
519 // we are better off starting from the end of the time sorted list.
520 for (it = mLogElements.end(); it != mLogElements.begin(); /* do nothing */) {
521 --it;
522 LogBufferElement *element = *it;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800523 if (element->getSequence() <= start) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600524 it++;
525 break;
526 }
527 }
528 }
529
530 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800531 LogBufferElement *element = *it;
532
533 if (!privileged && (element->getUid() != uid)) {
534 continue;
535 }
536
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800537 if (element->getSequence() <= start) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800538 continue;
539 }
540
541 // NB: calling out to another object with mLogElementsLock held (safe)
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800542 if (filter) {
543 int ret = (*filter)(element, arg);
544 if (ret == false) {
545 continue;
546 }
547 if (ret != true) {
548 break;
549 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800550 }
551
552 pthread_mutex_unlock(&mLogElementsLock);
553
554 // range locking in LastLogTimes looks after us
555 max = element->flushTo(reader);
556
557 if (max == element->FLUSH_ERROR) {
558 return max;
559 }
560
561 pthread_mutex_lock(&mLogElementsLock);
562 }
563 pthread_mutex_unlock(&mLogElementsLock);
564
565 return max;
566}
Mark Salyzyn34facab2014-02-06 14:48:50 -0800567
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800568void LogBuffer::formatStatistics(char **strp, uid_t uid, unsigned int logMask) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800569 pthread_mutex_lock(&mLogElementsLock);
570
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700571 stats.format(strp, uid, logMask);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800572
573 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800574}