Skip to content

Commit

Permalink
libpod/Container.readFromJournal(): don't skip the first entry
Browse files Browse the repository at this point in the history
When reading log entries from the journal, don't skip past the first
matching entry after we've positioned the cursor at it.

Make the first blank-line entry that we logged so that the container
would always have at least one log entry for us to find (until it gets
vacuumed out, at least) a fake history entry, so that `logs` doesn't
pass it on for display.

CI already has tests that exercise journal-based logging, so
[NO TESTS NEEDED]

Signed-off-by: Nalin Dahyabhai <[email protected]>
  • Loading branch information
nalind committed Aug 23, 2021
1 parent 6b06e9b commit 1411fa5
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions libpod/container_log_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ func (c *Container) initializeJournal(ctx context.Context) error {
m["SYSLOG_IDENTIFIER"] = "podman"
m["PODMAN_ID"] = c.ID()
m["CONTAINER_ID_FULL"] = c.ID()
history := events.History
m["PODMAN_EVENT"] = history.String()
return journal.Send("", journal.PriInfo, m)
}

Expand Down Expand Up @@ -89,10 +91,10 @@ func (c *Container) readFromJournal(ctx context.Context, options *logs.LogOption
var cursorError error
for i := 1; i <= 3; i++ {
cursor, cursorError = journal.GetCursor()
if err != nil {
if cursorError != nil {
time.Sleep(time.Duration(i*100) * time.Millisecond)
continue
}
time.Sleep(time.Duration(i*100) * time.Millisecond)
break
}
if cursorError != nil {
Expand All @@ -116,6 +118,7 @@ func (c *Container) readFromJournal(ctx context.Context, options *logs.LogOption

tailQueue := []*logs.LogLine{} // needed for options.Tail
doTail := options.Tail > 0
lastReadCursor := ""
for {
select {
case <-ctx.Done():
Expand All @@ -125,18 +128,25 @@ func (c *Container) readFromJournal(ctx context.Context, options *logs.LogOption
// Fallthrough
}

if _, err := journal.Next(); err != nil {
logrus.Errorf("Failed to move journal cursor to next entry: %v", err)
return
if lastReadCursor != "" {
// Advance to next entry if we read this one.
if _, err := journal.Next(); err != nil {
logrus.Errorf("Failed to move journal cursor to next entry: %v", err)
return
}
}
latestCursor, err := journal.GetCursor()

// Fetch the location of this entry, presumably either
// the one that follows the last one we read, or that
// same last one, if there is no next entry (yet).
cursor, err = journal.GetCursor()
if err != nil {
logrus.Errorf("Failed to get journal cursor: %v", err)
return
}

// Hit the end of the journal.
if cursor == latestCursor {
// Hit the end of the journal (so far?).
if cursor == lastReadCursor {
if doTail {
// Flush *once* we hit the end of the journal.
startIndex := int64(len(tailQueue)-1) - options.Tail
Expand All @@ -157,8 +167,9 @@ func (c *Container) readFromJournal(ctx context.Context, options *logs.LogOption
journal.Wait(sdjournal.IndefiniteWait)
continue
}
cursor = latestCursor
lastReadCursor = cursor

// Read the journal entry.
entry, err := journal.GetEntry()
if err != nil {
logrus.Errorf("Failed to get journal entry: %v", err)
Expand Down

0 comments on commit 1411fa5

Please sign in to comment.