Skip to content

Commit 09b205d

Browse files
committed
journal: add StdoutIsJournalStream function
See discussion in #410
1 parent 04e77f7 commit 09b205d

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

journal/journal_unix.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,24 @@ func Enabled() bool {
8282
//
8383
// [Journal Native Protocol]: https://systemd.io/JOURNAL_NATIVE_PROTOCOL/#automatic-protocol-upgrading
8484
func StderrIsJournalStream() (bool, error) {
85+
return fdIsJournalStream(syscall.Stderr)
86+
}
87+
88+
// StdoutIsJournalStream returns whether the process stdout is connected
89+
// to the Journal's stream transport.
90+
//
91+
// Returns true if JOURNAL_STREAM environment variable is present,
92+
// and stdout's device and inode numbers match it.
93+
//
94+
// Error is returned if unexpected error occurs: e.g. if JOURNAL_STREAM environment variable
95+
// is present, but malformed, fstat syscall fails, etc.
96+
//
97+
// Most users should probably use [StderrIsJournalStream].
98+
func StdoutIsJournalStream() (bool, error) {
99+
return fdIsJournalStream(syscall.Stdout)
100+
}
101+
102+
func fdIsJournalStream(fd int) (bool, error) {
85103
journalStream := os.Getenv("JOURNAL_STREAM")
86104
if journalStream == "" {
87105
return false, nil
@@ -94,7 +112,7 @@ func StderrIsJournalStream() (bool, error) {
94112
}
95113

96114
var stat syscall.Stat_t
97-
err = syscall.Fstat(syscall.Stderr, &stat)
115+
err = syscall.Fstat(fd, &stat)
98116
if err != nil {
99117
return false, err
100118
}

journal/journal_windows.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@ func Send(message string, priority Priority, vars map[string]string) error {
3737
func StderrIsJournalStream() (bool, error) {
3838
return false, nil
3939
}
40+
41+
func StdoutIsJournalStream() (bool, error) {
42+
return false, nil
43+
}

0 commit comments

Comments
 (0)