Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

glog: check that stderr is valid before using it by default #72

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion glog_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,10 @@ var sinks struct {
func init() {
// Register stderr first: that way if we crash during file-writing at least
// the log will have gone somewhere.
logsink.TextSinks = append(logsink.TextSinks, &sinks.stderr, &sinks.file)
if shouldRegisterStderrSink() {
logsink.TextSinks = append(logsink.TextSinks, &sinks.stderr)
}
logsink.TextSinks = append(logsink.TextSinks, &sinks.file)

sinks.file.flushChan = make(chan logsink.Severity, 1)
go sinks.file.flushDaemon()
Expand Down
7 changes: 7 additions & 0 deletions glog_file_nonwindows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ package glog

import "os/user"

// shouldRegisterStderrSink determines whether we should register a log sink that writes to stderr.
// Today, this always returns true on non-Windows platforms, as it specifically checks for a
// condition that is only present on Windows.
func shouldRegisterStderrSink() bool {
return true
}

func lookupUser() string {
if current, err := user.Current(); err == nil {
return current.Username
Expand Down
13 changes: 13 additions & 0 deletions glog_file_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,22 @@
package glog

import (
"os"
"syscall"
)

// shouldRegisterStderrSink determines whether we should register a log sink that writes to stderr.
// Today, this checks if stderr is "valid", in that it maps to a non-NULL Handle.
// Windows Services are spawned without Stdout and Stderr, so any attempt to use them equates to
// referencing an invalid file Handle.
// os.Stderr's FD is derived from a call to `syscall.GetStdHandle(syscall.STD_ERROR_HANDLE)`.
// Documentation[1] for the GetStdHandle function indicates the return value may be NULL if the
// application lacks the standard handle, so consider Stderr valid if its FD is non-NULL.
// [1]: https://learn.microsoft.com/en-us/windows/console/getstdhandle
func shouldRegisterStderrSink() bool {
return os.Stderr.Fd() != 0
}

// This follows the logic in the standard library's user.Current() function, except
// that it leaves out the potentially expensive calls required to look up the user's
// display name in Active Directory.
Expand Down