add method
Adds a new log entry to the logger.
The log will be inserted in the appropriate position based on its InfospectLog.timestamp. If the maximumSize is reached, the oldest logs will be removed.
Parameters:
log
: The log entry to be added.
Implementation
void add(InfospectLog log) {
var logs = callsSubject.value;
int index;
if (logs.isEmpty || !log.timestamp.isBefore(logs.last.timestamp)) {
// Quick path as new logs are usually more recent.
index = logs.length;
} else {
// Binary search to find the insertion index.
var min = 0;
var max = logs.length;
while (min < max) {
final mid = min + ((max - min) >> 1);
final item = logs[mid];
if (log.timestamp.isBefore(item.timestamp)) {
max = mid;
} else {
min = mid + 1;
}
}
assert(min == max);
index = min;
}
var startIndex = 0;
if (maximumSize != null && logs.length >= maximumSize!) {
if (index == 0) return;
startIndex = logs.length - maximumSize! + 1;
}
callsSubject.add([
...logs.sublist(startIndex, index),
log,
...logs.sublist(index, logs.length),
]);
}