From: Ludi Rehak Date: Mon, 8 Aug 2022 21:47:45 +0000 (-0700) Subject: log: change isDiscard type to atomic.Bool X-Git-Tag: go1.20rc1~1738 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=c7942f87a2587ee989f6e282d887b4652119133a;p=gostls13.git log: change isDiscard type to atomic.Bool Change-Id: Iff881cc6cc2ec34c7cf8bbd5dd1b0a05a19e1c23 Reviewed-on: https://go-review.googlesource.com/c/go/+/422175 Run-TryBot: Rob Pike Auto-Submit: Ian Lance Taylor TryBot-Result: Gopher Robot Reviewed-by: Rob Pike Run-TryBot: Ian Lance Taylor Run-TryBot: Ian Lance Taylor Reviewed-by: Dmitri Shuralyov Reviewed-by: Ian Lance Taylor --- diff --git a/src/log/log.go b/src/log/log.go index f7e48d5599..566535f25c 100644 --- a/src/log/log.go +++ b/src/log/log.go @@ -54,12 +54,12 @@ const ( // the Writer's Write method. A Logger can be used simultaneously from // multiple goroutines; it guarantees to serialize access to the Writer. type Logger struct { - mu sync.Mutex // ensures atomic writes; protects the following fields - prefix string // prefix on each line to identify the logger (but see Lmsgprefix) - flag int // properties - out io.Writer // destination for output - buf []byte // for accumulating text to write - isDiscard int32 // atomic boolean: whether out == io.Discard + mu sync.Mutex // ensures atomic writes; protects the following fields + prefix string // prefix on each line to identify the logger (but see Lmsgprefix) + flag int // properties + out io.Writer // destination for output + buf []byte // for accumulating text to write + isDiscard atomic.Bool // whether out == io.Discard } // New creates a new Logger. The out variable sets the @@ -70,7 +70,7 @@ type Logger struct { func New(out io.Writer, prefix string, flag int) *Logger { l := &Logger{out: out, prefix: prefix, flag: flag} if out == io.Discard { - l.isDiscard = 1 + l.isDiscard.Store(true) } return l } @@ -80,11 +80,7 @@ func (l *Logger) SetOutput(w io.Writer) { l.mu.Lock() defer l.mu.Unlock() l.out = w - isDiscard := int32(0) - if w == io.Discard { - isDiscard = 1 - } - atomic.StoreInt32(&l.isDiscard, isDiscard) + l.isDiscard.Store(w == io.Discard) } var std = New(os.Stderr, "", LstdFlags) @@ -202,7 +198,7 @@ func (l *Logger) Output(calldepth int, s string) error { // Printf calls l.Output to print to the logger. // Arguments are handled in the manner of fmt.Printf. func (l *Logger) Printf(format string, v ...any) { - if atomic.LoadInt32(&l.isDiscard) != 0 { + if l.isDiscard.Load() { return } l.Output(2, fmt.Sprintf(format, v...)) @@ -211,7 +207,7 @@ func (l *Logger) Printf(format string, v ...any) { // Print calls l.Output to print to the logger. // Arguments are handled in the manner of fmt.Print. func (l *Logger) Print(v ...any) { - if atomic.LoadInt32(&l.isDiscard) != 0 { + if l.isDiscard.Load() { return } l.Output(2, fmt.Sprint(v...)) @@ -220,7 +216,7 @@ func (l *Logger) Print(v ...any) { // Println calls l.Output to print to the logger. // Arguments are handled in the manner of fmt.Println. func (l *Logger) Println(v ...any) { - if atomic.LoadInt32(&l.isDiscard) != 0 { + if l.isDiscard.Load() { return } l.Output(2, fmt.Sprintln(v...)) @@ -339,7 +335,7 @@ func Writer() io.Writer { // Print calls Output to print to the standard logger. // Arguments are handled in the manner of fmt.Print. func Print(v ...any) { - if atomic.LoadInt32(&std.isDiscard) != 0 { + if std.isDiscard.Load() { return } std.Output(2, fmt.Sprint(v...)) @@ -348,7 +344,7 @@ func Print(v ...any) { // Printf calls Output to print to the standard logger. // Arguments are handled in the manner of fmt.Printf. func Printf(format string, v ...any) { - if atomic.LoadInt32(&std.isDiscard) != 0 { + if std.isDiscard.Load() { return } std.Output(2, fmt.Sprintf(format, v...)) @@ -357,7 +353,7 @@ func Printf(format string, v ...any) { // Println calls Output to print to the standard logger. // Arguments are handled in the manner of fmt.Println. func Println(v ...any) { - if atomic.LoadInt32(&std.isDiscard) != 0 { + if std.isDiscard.Load() { return } std.Output(2, fmt.Sprintln(v...))