Skip to content

Commit

Permalink
Prevent data race in SetOutput* methods
Browse files Browse the repository at this point in the history
  • Loading branch information
dims committed Aug 5, 2019
1 parent ab4d90c commit 959d342
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions klog.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,8 @@ func (rb *redirectBuffer) Write(bytes []byte) (n int, err error) {

// SetOutput sets the output destination for all severities
func SetOutput(w io.Writer) {
logging.mu.Lock()
defer logging.mu.Unlock()
for s := fatalLog; s >= infoLog; s-- {
rb := &redirectBuffer{
w: w,
Expand All @@ -756,6 +758,8 @@ func SetOutput(w io.Writer) {

// SetOutputBySeverity sets the output destination for specific severity
func SetOutputBySeverity(name string, w io.Writer) {
logging.mu.Lock()
defer logging.mu.Unlock()
sev, ok := severityByName(name)
if !ok {
panic(fmt.Sprintf("SetOutputBySeverity(%q): unrecognized severity name", name))
Expand Down

0 comments on commit 959d342

Please sign in to comment.