@@ -54,12 +54,12 @@ const (
54
54
// the Writer's Write method. A Logger can be used simultaneously from
55
55
// multiple goroutines; it guarantees to serialize access to the Writer.
56
56
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
63
63
}
64
64
65
65
// New creates a new Logger. The out variable sets the
@@ -70,7 +70,7 @@ type Logger struct {
70
70
func New (out io.Writer , prefix string , flag int ) * Logger {
71
71
l := & Logger {out : out , prefix : prefix , flag : flag }
72
72
if out == io .Discard {
73
- l .isDiscard = 1
73
+ l .isDiscard . Store ( true )
74
74
}
75
75
return l
76
76
}
@@ -80,11 +80,7 @@ func (l *Logger) SetOutput(w io.Writer) {
80
80
l .mu .Lock ()
81
81
defer l .mu .Unlock ()
82
82
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 )
88
84
}
89
85
90
86
var std = New (os .Stderr , "" , LstdFlags )
@@ -202,7 +198,7 @@ func (l *Logger) Output(calldepth int, s string) error {
202
198
// Printf calls l.Output to print to the logger.
203
199
// Arguments are handled in the manner of fmt.Printf.
204
200
func (l * Logger ) Printf (format string , v ... any ) {
205
- if atomic . LoadInt32 ( & l .isDiscard ) != 0 {
201
+ if l .isDiscard . Load () {
206
202
return
207
203
}
208
204
l .Output (2 , fmt .Sprintf (format , v ... ))
@@ -211,7 +207,7 @@ func (l *Logger) Printf(format string, v ...any) {
211
207
// Print calls l.Output to print to the logger.
212
208
// Arguments are handled in the manner of fmt.Print.
213
209
func (l * Logger ) Print (v ... any ) {
214
- if atomic . LoadInt32 ( & l .isDiscard ) != 0 {
210
+ if l .isDiscard . Load () {
215
211
return
216
212
}
217
213
l .Output (2 , fmt .Sprint (v ... ))
@@ -220,7 +216,7 @@ func (l *Logger) Print(v ...any) {
220
216
// Println calls l.Output to print to the logger.
221
217
// Arguments are handled in the manner of fmt.Println.
222
218
func (l * Logger ) Println (v ... any ) {
223
- if atomic . LoadInt32 ( & l .isDiscard ) != 0 {
219
+ if l .isDiscard . Load () {
224
220
return
225
221
}
226
222
l .Output (2 , fmt .Sprintln (v ... ))
@@ -339,7 +335,7 @@ func Writer() io.Writer {
339
335
// Print calls Output to print to the standard logger.
340
336
// Arguments are handled in the manner of fmt.Print.
341
337
func Print (v ... any ) {
342
- if atomic . LoadInt32 ( & std .isDiscard ) != 0 {
338
+ if std .isDiscard . Load () {
343
339
return
344
340
}
345
341
std .Output (2 , fmt .Sprint (v ... ))
@@ -348,7 +344,7 @@ func Print(v ...any) {
348
344
// Printf calls Output to print to the standard logger.
349
345
// Arguments are handled in the manner of fmt.Printf.
350
346
func Printf (format string , v ... any ) {
351
- if atomic . LoadInt32 ( & std .isDiscard ) != 0 {
347
+ if std .isDiscard . Load () {
352
348
return
353
349
}
354
350
std .Output (2 , fmt .Sprintf (format , v ... ))
@@ -357,7 +353,7 @@ func Printf(format string, v ...any) {
357
353
// Println calls Output to print to the standard logger.
358
354
// Arguments are handled in the manner of fmt.Println.
359
355
func Println (v ... any ) {
360
- if atomic . LoadInt32 ( & std .isDiscard ) != 0 {
356
+ if std .isDiscard . Load () {
361
357
return
362
358
}
363
359
std .Output (2 , fmt .Sprintln (v ... ))
0 commit comments