journald/read: simplify walking backwards
In case Tail=N parameter is requested, we need to show N lines. It does not make sense to walk backwards one by one if we can do it at once. Now, if Since=T is also provided, make sure we haven't jumped too far (before T), and if we did, move forward. The primary motivation for this was to make the code simpler. This also fixes a tiny bug in the "since" implementation. Before this commit: > $ docker logs -t --tail=6000 --since="2019-03-10T03:54:25.00" $ID | head > 2019-03-10T03:54:24.999821000Z 95981 After: > $ docker logs -t --tail=6000 --since="2019-03-10T03:54:25.00" $ID | head > 2019-03-10T03:54:25.000013000Z 95982 Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
parent
e8f6166791
commit
ff3cd167ea
1 changed files with 7 additions and 24 deletions
|
@ -361,7 +361,6 @@ func (s *journald) readLogs(logWatcher *logger.LogWatcher, config logger.ReadCon
|
|||
untilUnixMicro = uint64(nano / 1000)
|
||||
}
|
||||
if config.Tail > 0 {
|
||||
lines := config.Tail
|
||||
// If until time provided, start from there.
|
||||
// Otherwise start at the end of the journal.
|
||||
if untilUnixMicro != 0 && C.sd_journal_seek_realtime_usec(j, C.uint64_t(untilUnixMicro)) < 0 {
|
||||
|
@ -371,29 +370,13 @@ func (s *journald) readLogs(logWatcher *logger.LogWatcher, config logger.ReadCon
|
|||
logWatcher.Err <- fmt.Errorf("error seeking to end of journal")
|
||||
return
|
||||
}
|
||||
if C.sd_journal_previous(j) < 0 {
|
||||
logWatcher.Err <- fmt.Errorf("error backtracking to previous journal entry")
|
||||
return
|
||||
}
|
||||
// Walk backward.
|
||||
for lines > 0 {
|
||||
// Stop if the entry time is before our cutoff.
|
||||
// We'll need the entry time if it isn't, so go
|
||||
// ahead and parse it now.
|
||||
if C.sd_journal_get_realtime_usec(j, &stamp) != 0 {
|
||||
break
|
||||
} else {
|
||||
// Compare the timestamp on the entry to our threshold value.
|
||||
if sinceUnixMicro != 0 && sinceUnixMicro > uint64(stamp) {
|
||||
break
|
||||
}
|
||||
}
|
||||
lines--
|
||||
// If we're at the start of the journal, or
|
||||
// don't need to back up past any more entries,
|
||||
// stop.
|
||||
if lines == 0 || C.sd_journal_previous(j) <= 0 {
|
||||
break
|
||||
// (Try to) skip backwards by the requested number of lines...
|
||||
if C.sd_journal_previous_skip(j, C.uint64_t(config.Tail)) > 0 {
|
||||
// ...but not before "since"
|
||||
if sinceUnixMicro != 0 &&
|
||||
C.sd_journal_get_realtime_usec(j, &stamp) == 0 &&
|
||||
uint64(stamp) < sinceUnixMicro {
|
||||
C.sd_journal_seek_realtime_usec(j, C.uint64_t(sinceUnixMicro))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
|
Loading…
Reference in a new issue