Skip to content

Commit 2666824

Browse files
Marcel Hermnexoscp
authored andcommitted
Add new follow method to follow entries from next to tail
1 parent 458b399 commit 2666824

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

sdjournal/read.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,39 @@ process:
256256
}
257257
}
258258

259+
// FollowTail synchronously follows the JournalReader, writing each new journal entry to writer. The
260+
// follow will continue until a single time.Time is received on the until channel.
261+
func (r *JournalReader) FollowTail(entries chan *JournalEntry) error {
262+
defer close(entries)
263+
264+
// skip first entry which has already been read
265+
if _, err := r.journal.Next(); err != nil {
266+
return err
267+
}
268+
269+
for {
270+
status := r.journal.Wait(200 * time.Millisecond)
271+
if status != SD_JOURNAL_APPEND && status != SD_JOURNAL_INVALIDATE {
272+
continue
273+
}
274+
275+
for {
276+
if c, err := r.journal.Next(); err != nil {
277+
return err
278+
} else if c == 0 {
279+
// EOF, should mean we're at the tail
280+
break
281+
}
282+
283+
if entry, err := r.journal.GetEntry(); err != nil {
284+
return err
285+
} else {
286+
entries <- entry
287+
}
288+
}
289+
}
290+
}
291+
259292
// simpleMessageFormatter is the default formatter.
260293
// It returns a string representing the current journal entry in a simple format which
261294
// includes the entry timestamp and MESSAGE field.

0 commit comments

Comments
 (0)