]> Cypherpunks repositories - gostls13.git/commitdiff
log: change isDiscard type to atomic.Bool
authorLudi Rehak <ludi317@gmail.com>
Mon, 8 Aug 2022 21:47:45 +0000 (14:47 -0700)
committerGopher Robot <gobot@golang.org>
Tue, 9 Aug 2022 14:40:33 +0000 (14:40 +0000)
Change-Id: Iff881cc6cc2ec34c7cf8bbd5dd1b0a05a19e1c23
Reviewed-on: https://go-review.googlesource.com/c/go/+/422175
Run-TryBot: Rob Pike <r@golang.org>
Auto-Submit: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
src/log/log.go

index f7e48d5599b3ed27f05faf4b5cededb60faeae96..566535f25c1852bfaf2ac25f29931a0bed74797d 100644 (file)
@@ -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...))