From 59f8778c32190bf11a6b8fa8c51953b4aadb1780 Mon Sep 17 00:00:00 2001 From: Egor Taranov Date: Tue, 25 Jun 2024 22:50:04 +0200 Subject: [PATCH] journal: ability to set flags for journal instance --- sdjournal/journal.go | 18 +++++++++++++++++- sdjournal/read.go | 11 +++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/sdjournal/journal.go b/sdjournal/journal.go index 300b4850..65b9a6c0 100644 --- a/sdjournal/journal.go +++ b/sdjournal/journal.go @@ -372,6 +372,16 @@ const ( SD_JOURNAL_FIELD_CURSOR = "__CURSOR" SD_JOURNAL_FIELD_REALTIME_TIMESTAMP = "__REALTIME_TIMESTAMP" SD_JOURNAL_FIELD_MONOTONIC_TIMESTAMP = "__MONOTONIC_TIMESTAMP" + + // Journal Flags + SD_JOURNAL_FLAG_LOCAL_ONLY = int(C.SD_JOURNAL_LOCAL_ONLY) + SD_JOURNAL_FLAG_RUNTIME_ONLY = int(C.SD_JOURNAL_RUNTIME_ONLY) + SD_JOURNAL_FLAG_SYSTEM = int(C.SD_JOURNAL_SYSTEM) + SD_JOURNAL_FLAG_CURRENT_USER = int(C.SD_JOURNAL_CURRENT_USER) + SD_JOURNAL_FLAG_OS_ROOT = int(C.SD_JOURNAL_OS_ROOT) + SD_JOURNAL_FLAG_ALL_NAMESPACES = int(C.SD_JOURNAL_ALL_NAMESPACES) + SD_JOURNAL_FLAG_INCLUDE_DEFAULT_NAMESPACE = int(C.SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE) + SD_JOURNAL_FLAG_TAKE_DIRECTORY_FD = int(C.SD_JOURNAL_TAKE_DIRECTORY_FD) ) // Journal event constants @@ -422,6 +432,12 @@ func (m *Match) String() string { // NewJournal returns a new Journal instance pointing to the local journal func NewJournal() (j *Journal, err error) { + return NewJournalWithFlags(SD_JOURNAL_FLAG_LOCAL_ONLY) +} + +// NewJournalWithFlags return a new Journal instance pointing to the local journal +// with a list of flags indicating the scope and type of entries that will be accessed. +func NewJournalWithFlags(flags int) (j *Journal, err error) { j = &Journal{} sd_journal_open, err := getFunction("sd_journal_open") @@ -429,7 +445,7 @@ func NewJournal() (j *Journal, err error) { return nil, err } - r := C.my_sd_journal_open(sd_journal_open, &j.cjournal, C.SD_JOURNAL_LOCAL_ONLY) + r := C.my_sd_journal_open(sd_journal_open, &j.cjournal, C.int(flags)) if r < 0 { return nil, fmt.Errorf("failed to open journal: %s", syscall.Errno(-r).Error()) diff --git a/sdjournal/read.go b/sdjournal/read.go index 51a060fb..4b7ea732 100644 --- a/sdjournal/read.go +++ b/sdjournal/read.go @@ -15,6 +15,7 @@ package sdjournal +import "C" import ( "errors" "fmt" @@ -48,6 +49,10 @@ type JournalReaderConfig struct { // in this directory. The supplied path may be relative or absolute. Path string + // If not nil, the journal instance will point to a journal with a list + // of flags indicating the scope and type of entries that will be accessed. + Flags []int + // If not nil, Formatter will be used to translate the resulting entries // into strings. If not set, the default format (timestamp and message field) // will be used. If Formatter returns an error, Read will stop and return the error. @@ -78,6 +83,12 @@ func NewJournalReader(config JournalReaderConfig) (*JournalReader, error) { var err error if config.Path != "" { r.journal, err = NewJournalFromDir(config.Path) + } else if len(config.Flags) > 0 { + flags := config.Flags[0] + for i := 1; i < len(config.Flags); i++ { + flags |= config.Flags[i] + } + r.journal, err = NewJournalWithFlags(flags) } else { r.journal, err = NewJournal() }