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: use os.Stderr directly for writing to stderr #62

Merged
merged 1 commit into from
Mar 22, 2023
Merged
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
11 changes: 6 additions & 5 deletions glog_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,6 @@ var sinks struct {
}

func init() {
sinks.stderr.w = os.Stderr

// 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)
Expand All @@ -167,7 +165,7 @@ func init() {
// if they meet certain conditions.
type stderrSink struct {
mu sync.Mutex
w io.Writer
w io.Writer // if nil Emit uses os.Stderr directly
}

// Enabled implements logsink.Text.Enabled. It returns true if any of the
Expand All @@ -182,8 +180,11 @@ func (s *stderrSink) Enabled(m *logsink.Meta) bool {
func (s *stderrSink) Emit(m *logsink.Meta, data []byte) (n int, err error) {
s.mu.Lock()
defer s.mu.Unlock()

dn, err := s.w.Write(data)
w := s.w
if w == nil {
w = os.Stderr
}
dn, err := w.Write(data)
n += dn
return n, err
}
Expand Down