From 48fe794b23215babf05cc4db5a0ad4040ecff675 Mon Sep 17 00:00:00 2001 From: Jonathan Boulle Date: Fri, 20 May 2016 11:43:59 +0200 Subject: [PATCH 1/2] sdjournal: add SeekHead method Simple wrapper around `sd_journal_seek_head` --- sdjournal/journal.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/sdjournal/journal.go b/sdjournal/journal.go index b0651629..87717398 100644 --- a/sdjournal/journal.go +++ b/sdjournal/journal.go @@ -164,6 +164,15 @@ package sdjournal // } // // int +// my_sd_journal_seek_head(void *f, sd_journal *j) +// { +// int (*sd_journal_seek_head)(sd_journal *); +// +// sd_journal_seek_head = f; +// return sd_journal_seek_head(j); +// } +// +// int // my_sd_journal_seek_tail(void *f, sd_journal *j) // { // int (*sd_journal_seek_tail)(sd_journal *); @@ -587,6 +596,25 @@ func (j *Journal) GetRealtimeUsec() (uint64, error) { return uint64(usec), nil } +// SeekHead seeks to the beginning of the journal, i.e. the oldest available +// entry. +func (j *Journal) SeekHead() error { + sd_journal_seek_head, err := j.getFunction("sd_journal_seek_head") + if err != nil { + return err + } + + j.mu.Lock() + r := C.my_sd_journal_seek_head(sd_journal_seek_head, j.cjournal) + j.mu.Unlock() + + if r < 0 { + return fmt.Errorf("failed to seek to head of journal: %d", syscall.Errno(-r)) + } + + return nil +} + // SeekTail may be used to seek to the end of the journal, i.e. the most recent // available entry. func (j *Journal) SeekTail() error { From 0f21494155c1a73b278758a11d14d56393c0d7f2 Mon Sep 17 00:00:00 2001 From: Jonathan Boulle Date: Fri, 20 May 2016 11:52:13 +0200 Subject: [PATCH 2/2] sdjournal: add Rewind method to JournalReader This allows users of the JournalReader to rewind to the beginning of the Journal. Also add a missing docstring to JournalReader.Close --- sdjournal/read.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/sdjournal/read.go b/sdjournal/read.go index 8944448c..5e7cec8d 100644 --- a/sdjournal/read.go +++ b/sdjournal/read.go @@ -125,10 +125,16 @@ func (r *JournalReader) Read(b []byte) (int, error) { return len(msg), nil } +// Close closes the JournalReader's handle to the journal. func (r *JournalReader) Close() error { return r.journal.Close() } +// Rewind attempts to rewind the JournalReader to the first entry. +func (r *JournalReader) Rewind() error { + return r.journal.SeekHead() +} + // Follow synchronously follows the JournalReader, writing each new journal entry to writer. The // follow will continue until a single time.Time is received on the until channel. func (r *JournalReader) Follow(until <-chan time.Time, writer io.Writer) (err error) {