Skip to content

Commit c7942f8

Browse files
ludi317gopherbot
authored andcommitted
log: change isDiscard type to atomic.Bool
Change-Id: Iff881cc6cc2ec34c7cf8bbd5dd1b0a05a19e1c23 Reviewed-on: https://go-review.googlesource.com/c/go/+/422175 Run-TryBot: Rob Pike <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Rob Pike <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 0981d9f commit c7942f8

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

src/log/log.go

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ const (
5454
// the Writer's Write method. A Logger can be used simultaneously from
5555
// multiple goroutines; it guarantees to serialize access to the Writer.
5656
type Logger struct {
57-
mu sync.Mutex // ensures atomic writes; protects the following fields
58-
prefix string // prefix on each line to identify the logger (but see Lmsgprefix)
59-
flag int // properties
60-
out io.Writer // destination for output
61-
buf []byte // for accumulating text to write
62-
isDiscard int32 // atomic boolean: whether out == io.Discard
57+
mu sync.Mutex // ensures atomic writes; protects the following fields
58+
prefix string // prefix on each line to identify the logger (but see Lmsgprefix)
59+
flag int // properties
60+
out io.Writer // destination for output
61+
buf []byte // for accumulating text to write
62+
isDiscard atomic.Bool // whether out == io.Discard
6363
}
6464

6565
// New creates a new Logger. The out variable sets the
@@ -70,7 +70,7 @@ type Logger struct {
7070
func New(out io.Writer, prefix string, flag int) *Logger {
7171
l := &Logger{out: out, prefix: prefix, flag: flag}
7272
if out == io.Discard {
73-
l.isDiscard = 1
73+
l.isDiscard.Store(true)
7474
}
7575
return l
7676
}
@@ -80,11 +80,7 @@ func (l *Logger) SetOutput(w io.Writer) {
8080
l.mu.Lock()
8181
defer l.mu.Unlock()
8282
l.out = w
83-
isDiscard := int32(0)
84-
if w == io.Discard {
85-
isDiscard = 1
86-
}
87-
atomic.StoreInt32(&l.isDiscard, isDiscard)
83+
l.isDiscard.Store(w == io.Discard)
8884
}
8985

9086
var std = New(os.Stderr, "", LstdFlags)
@@ -202,7 +198,7 @@ func (l *Logger) Output(calldepth int, s string) error {
202198
// Printf calls l.Output to print to the logger.
203199
// Arguments are handled in the manner of fmt.Printf.
204200
func (l *Logger) Printf(format string, v ...any) {
205-
if atomic.LoadInt32(&l.isDiscard) != 0 {
201+
if l.isDiscard.Load() {
206202
return
207203
}
208204
l.Output(2, fmt.Sprintf(format, v...))
@@ -211,7 +207,7 @@ func (l *Logger) Printf(format string, v ...any) {
211207
// Print calls l.Output to print to the logger.
212208
// Arguments are handled in the manner of fmt.Print.
213209
func (l *Logger) Print(v ...any) {
214-
if atomic.LoadInt32(&l.isDiscard) != 0 {
210+
if l.isDiscard.Load() {
215211
return
216212
}
217213
l.Output(2, fmt.Sprint(v...))
@@ -220,7 +216,7 @@ func (l *Logger) Print(v ...any) {
220216
// Println calls l.Output to print to the logger.
221217
// Arguments are handled in the manner of fmt.Println.
222218
func (l *Logger) Println(v ...any) {
223-
if atomic.LoadInt32(&l.isDiscard) != 0 {
219+
if l.isDiscard.Load() {
224220
return
225221
}
226222
l.Output(2, fmt.Sprintln(v...))
@@ -339,7 +335,7 @@ func Writer() io.Writer {
339335
// Print calls Output to print to the standard logger.
340336
// Arguments are handled in the manner of fmt.Print.
341337
func Print(v ...any) {
342-
if atomic.LoadInt32(&std.isDiscard) != 0 {
338+
if std.isDiscard.Load() {
343339
return
344340
}
345341
std.Output(2, fmt.Sprint(v...))
@@ -348,7 +344,7 @@ func Print(v ...any) {
348344
// Printf calls Output to print to the standard logger.
349345
// Arguments are handled in the manner of fmt.Printf.
350346
func Printf(format string, v ...any) {
351-
if atomic.LoadInt32(&std.isDiscard) != 0 {
347+
if std.isDiscard.Load() {
352348
return
353349
}
354350
std.Output(2, fmt.Sprintf(format, v...))
@@ -357,7 +353,7 @@ func Printf(format string, v ...any) {
357353
// Println calls Output to print to the standard logger.
358354
// Arguments are handled in the manner of fmt.Println.
359355
func Println(v ...any) {
360-
if atomic.LoadInt32(&std.isDiscard) != 0 {
356+
if std.isDiscard.Load() {
361357
return
362358
}
363359
std.Output(2, fmt.Sprintln(v...))

0 commit comments

Comments
 (0)