mirror of
https://github.com/coredns/coredns.git
synced 2025-10-27 00:04:15 -04:00
pkg/log: fix data race on d (#2698)
* pkg/log: fix data race on d Wrap d in a mutex to prevent data race. This makes is slower, but this is a debugging aid anyway. It's not used normally. Signed-off-by: Miek Gieben <miek@miek.nl> * Fix tests compilation Signed-off-by: Miek Gieben <miek@miek.nl> * Fix test compile Signed-off-by: Miek Gieben <miek@miek.nl>
This commit is contained in:
@@ -13,11 +13,33 @@ import (
|
||||
"io/ioutil"
|
||||
golog "log"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// D controls whether we should output debug logs. If true, we do.
|
||||
var D bool
|
||||
// D controls whether we should output debug logs. If true, we do, once set
|
||||
// it can not be unset.
|
||||
var D = &d{}
|
||||
|
||||
type d struct {
|
||||
on bool
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
// Set sets d to true.
|
||||
func (d *d) Set() {
|
||||
d.Lock()
|
||||
d.on = true
|
||||
d.Unlock()
|
||||
}
|
||||
|
||||
// Value return the boolean value of d.
|
||||
func (d *d) Value() bool {
|
||||
d.RLock()
|
||||
b := d.on
|
||||
d.RUnlock()
|
||||
return b
|
||||
}
|
||||
|
||||
// RFC3339Milli doesn't exist, invent it here.
|
||||
func clock() string { return time.Now().Format("2006-01-02T15:04:05.000Z07:00") }
|
||||
@@ -35,7 +57,7 @@ func log(level string, v ...interface{}) {
|
||||
// Debug is equivalent to log.Print(), but prefixed with "[DEBUG] ". It only outputs something
|
||||
// if D is true.
|
||||
func Debug(v ...interface{}) {
|
||||
if !D {
|
||||
if !D.Value() {
|
||||
return
|
||||
}
|
||||
log(debug, v...)
|
||||
@@ -44,7 +66,7 @@ func Debug(v ...interface{}) {
|
||||
// Debugf is equivalent to log.Printf(), but prefixed with "[DEBUG] ". It only outputs something
|
||||
// if D is true.
|
||||
func Debugf(format string, v ...interface{}) {
|
||||
if !D {
|
||||
if !D.Value() {
|
||||
return
|
||||
}
|
||||
logf(debug, format, v...)
|
||||
|
||||
Reference in New Issue
Block a user