Skip to content

Commit 59f8778

Browse files
committed
journal: ability to set flags for journal instance
1 parent 7d375ec commit 59f8778

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

sdjournal/journal.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,16 @@ const (
372372
SD_JOURNAL_FIELD_CURSOR = "__CURSOR"
373373
SD_JOURNAL_FIELD_REALTIME_TIMESTAMP = "__REALTIME_TIMESTAMP"
374374
SD_JOURNAL_FIELD_MONOTONIC_TIMESTAMP = "__MONOTONIC_TIMESTAMP"
375+
376+
// Journal Flags
377+
SD_JOURNAL_FLAG_LOCAL_ONLY = int(C.SD_JOURNAL_LOCAL_ONLY)
378+
SD_JOURNAL_FLAG_RUNTIME_ONLY = int(C.SD_JOURNAL_RUNTIME_ONLY)
379+
SD_JOURNAL_FLAG_SYSTEM = int(C.SD_JOURNAL_SYSTEM)
380+
SD_JOURNAL_FLAG_CURRENT_USER = int(C.SD_JOURNAL_CURRENT_USER)
381+
SD_JOURNAL_FLAG_OS_ROOT = int(C.SD_JOURNAL_OS_ROOT)
382+
SD_JOURNAL_FLAG_ALL_NAMESPACES = int(C.SD_JOURNAL_ALL_NAMESPACES)
383+
SD_JOURNAL_FLAG_INCLUDE_DEFAULT_NAMESPACE = int(C.SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE)
384+
SD_JOURNAL_FLAG_TAKE_DIRECTORY_FD = int(C.SD_JOURNAL_TAKE_DIRECTORY_FD)
375385
)
376386

377387
// Journal event constants
@@ -422,14 +432,20 @@ func (m *Match) String() string {
422432

423433
// NewJournal returns a new Journal instance pointing to the local journal
424434
func NewJournal() (j *Journal, err error) {
435+
return NewJournalWithFlags(SD_JOURNAL_FLAG_LOCAL_ONLY)
436+
}
437+
438+
// NewJournalWithFlags return a new Journal instance pointing to the local journal
439+
// with a list of flags indicating the scope and type of entries that will be accessed.
440+
func NewJournalWithFlags(flags int) (j *Journal, err error) {
425441
j = &Journal{}
426442

427443
sd_journal_open, err := getFunction("sd_journal_open")
428444
if err != nil {
429445
return nil, err
430446
}
431447

432-
r := C.my_sd_journal_open(sd_journal_open, &j.cjournal, C.SD_JOURNAL_LOCAL_ONLY)
448+
r := C.my_sd_journal_open(sd_journal_open, &j.cjournal, C.int(flags))
433449

434450
if r < 0 {
435451
return nil, fmt.Errorf("failed to open journal: %s", syscall.Errno(-r).Error())

sdjournal/read.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
package sdjournal
1717

18+
import "C"
1819
import (
1920
"errors"
2021
"fmt"
@@ -48,6 +49,10 @@ type JournalReaderConfig struct {
4849
// in this directory. The supplied path may be relative or absolute.
4950
Path string
5051

52+
// If not nil, the journal instance will point to a journal with a list
53+
// of flags indicating the scope and type of entries that will be accessed.
54+
Flags []int
55+
5156
// If not nil, Formatter will be used to translate the resulting entries
5257
// into strings. If not set, the default format (timestamp and message field)
5358
// 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) {
7883
var err error
7984
if config.Path != "" {
8085
r.journal, err = NewJournalFromDir(config.Path)
86+
} else if len(config.Flags) > 0 {
87+
flags := config.Flags[0]
88+
for i := 1; i < len(config.Flags); i++ {
89+
flags |= config.Flags[i]
90+
}
91+
r.journal, err = NewJournalWithFlags(flags)
8192
} else {
8293
r.journal, err = NewJournal()
8394
}

0 commit comments

Comments
 (0)