logd: use std::function and lambdas where appropriate

Test: logging unit tests
Change-Id: I7cfc63937b5dadb5547c4661ca2f5204d7b4a174
This commit is contained in:
Tom Cherry 2020-05-04 17:25:34 -07:00
parent 6ec71e9253
commit 320f5968da
4 changed files with 36 additions and 53 deletions

View file

@ -1033,7 +1033,7 @@ unsigned long LogBuffer::getSize(log_id_t id) {
uint64_t LogBuffer::flushTo(SocketClient* reader, uint64_t start, pid_t* lastTid, bool privileged,
bool security,
int (*filter)(const LogBufferElement* element, void* arg), void* arg) {
const std::function<int(const LogBufferElement* element)>& filter) {
LogBufferElementCollection::iterator it;
uid_t uid = reader->getUid();
@ -1071,7 +1071,7 @@ uint64_t LogBuffer::flushTo(SocketClient* reader, uint64_t start, pid_t* lastTid
// NB: calling out to another object with wrlock() held (safe)
if (filter) {
int ret = (*filter)(element, arg);
int ret = filter(element);
if (ret == false) {
continue;
}

View file

@ -115,8 +115,7 @@ class LogBuffer {
uint64_t flushTo(SocketClient* writer, uint64_t start,
pid_t* lastTid, // &lastTid[LOG_ID_MAX] or nullptr
bool privileged, bool security,
int (*filter)(const LogBufferElement* element, void* arg) = nullptr,
void* arg = nullptr);
const std::function<int(const LogBufferElement* element)>& filter);
bool clear(log_id_t id, uid_t uid = AID_ROOT);
unsigned long getSize(log_id_t id);

View file

@ -149,56 +149,37 @@ bool LogReader::onDataAvailable(SocketClient* cli) {
uint64_t sequence = 1;
// Convert realtime to sequence number
if (start != log_time::EPOCH) {
class LogFindStart {
const pid_t mPid;
const unsigned mLogMask;
bool startTimeSet;
const log_time start;
uint64_t& sequence;
uint64_t last;
bool isMonotonic;
public:
LogFindStart(unsigned logMask, pid_t pid, log_time start, uint64_t& sequence,
bool isMonotonic)
: mPid(pid),
mLogMask(logMask),
startTimeSet(false),
start(start),
sequence(sequence),
last(sequence),
isMonotonic(isMonotonic) {}
static int callback(const LogBufferElement* element, void* obj) {
LogFindStart* me = reinterpret_cast<LogFindStart*>(obj);
if ((!me->mPid || (me->mPid == element->getPid())) &&
(me->mLogMask & (1 << element->getLogId()))) {
if (me->start == element->getRealTime()) {
me->sequence = element->getSequence();
me->startTimeSet = true;
return -1;
} else if (!me->isMonotonic || android::isMonotonic(element->getRealTime())) {
if (me->start < element->getRealTime()) {
me->sequence = me->last;
me->startTimeSet = true;
return -1;
}
me->last = element->getSequence();
} else {
me->last = element->getSequence();
}
}
return false;
bool start_time_set = false;
bool is_monotonic = logbuf().isMonotonic() && android::isMonotonic(start);
uint64_t last = sequence;
auto log_find_start = [pid, logMask, start, is_monotonic, &sequence, &start_time_set,
&last](const LogBufferElement* element) -> int {
if (pid && pid != element->getPid()) {
return 0;
}
if ((logMask & (1 << element->getLogId())) == 0) {
return 0;
}
if (start == element->getRealTime()) {
sequence = element->getSequence();
start_time_set = true;
return -1;
} else if (!is_monotonic || android::isMonotonic(element->getRealTime())) {
if (start < element->getRealTime()) {
sequence = last;
start_time_set = true;
return -1;
}
last = element->getSequence();
} else {
last = element->getSequence();
}
return 0;
};
bool found() { return startTimeSet; }
} logFindStart(logMask, pid, start, sequence,
logbuf().isMonotonic() && android::isMonotonic(start));
logbuf().flushTo(cli, sequence, nullptr, privileged, can_read_security, log_find_start);
logbuf().flushTo(cli, sequence, nullptr, privileged, can_read_security,
logFindStart.callback, &logFindStart);
if (!logFindStart.found()) {
if (!start_time_set) {
if (nonBlock) {
doSocketDelete(cli);
return false;

View file

@ -23,6 +23,8 @@
#include "LogBuffer.h"
#include "LogReader.h"
using namespace std::placeholders;
pthread_mutex_t LogReaderThread::timesLock = PTHREAD_MUTEX_INITIALIZER;
LogReaderThread::LogReaderThread(LogReader& reader, SocketClient* client, bool non_block,
@ -96,11 +98,12 @@ void* LogReaderThread::threadStart(void* obj) {
if (me->mTail) {
logbuf.flushTo(client, start, nullptr, me->privileged_, me->can_read_security_logs_,
FilterFirstPass, me);
std::bind(&LogReaderThread::FilterFirstPass, _1, me));
me->leadingDropped = true;
}
start = logbuf.flushTo(client, start, me->mLastTid, me->privileged_,
me->can_read_security_logs_, FilterSecondPass, me);
me->can_read_security_logs_,
std::bind(&LogReaderThread::FilterSecondPass, _1, me));
// We only ignore entries before the original start time for the first flushTo(), if we
// get entries after this first flush before the original start time, then the client