// 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
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
}
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)
// 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...))
// 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...))
// 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...))
// 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...))
// 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...))
// 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...))