fix: data race const readings

This commit is contained in:
marcello 2020-01-05 15:32:01 -06:00
parent 64ff1d551c
commit e2ed6ffb4c
2 changed files with 17 additions and 14 deletions

View File

@ -31,12 +31,12 @@ var (
defaultLogger = Logger{log.New(os.Stdout, "\r\n", 0)} defaultLogger = Logger{log.New(os.Stdout, "\r\n", 0)}
sqlRegexp = regexp.MustCompile(`\?`) sqlRegexp = regexp.MustCompile(`\?`)
numericPlaceHolderRegexp = regexp.MustCompile(`\$\d+`) numericPlaceHolderRegexp = regexp.MustCompile(`\$\d+`)
reset = "" reset = RESET
cyan = "" cyan = CYAN
magenta = "" magenta = MAGENTA
red = "" red = RED
redBold = "" redBold = REDBOLD
yellow = "" yellow = YELLOW
) )
func isPrintable(s string) bool { func isPrintable(s string) bool {
@ -54,6 +54,16 @@ func NoColor() bool {
return os.Getenv("NO_COLOR") != "" return os.Getenv("NO_COLOR") != ""
} }
// ResetColors reads consts colors for test cases
func ResetColors() {
red = RED
cyan = CYAN
reset = RESET
yellow = YELLOW
redBold = REDBOLD
magenta = MAGENTA
}
// LogFormatter is a default logger with timestamps, sql error handling, loglevel and NO_COLOR support // LogFormatter is a default logger with timestamps, sql error handling, loglevel and NO_COLOR support
var LogFormatter = func(values ...interface{}) (messages []interface{}) { var LogFormatter = func(values ...interface{}) (messages []interface{}) {
suppressColor := NoColor() suppressColor := NoColor()
@ -66,13 +76,6 @@ var LogFormatter = func(values ...interface{}) (messages []interface{}) {
yellow = "" yellow = ""
redBold = "" redBold = ""
magenta = "" magenta = ""
} else {
red = RED
cyan = CYAN
reset = RESET
yellow = YELLOW
redBold = REDBOLD
magenta = MAGENTA
} }
if len(values) > 1 { if len(values) > 1 {
var ( var (

View File

@ -15,7 +15,7 @@ func TestNoColor(t *testing.T) {
l := gorm.Logger{log.New(os.Stdout, "\r\n", 0)} l := gorm.Logger{log.New(os.Stdout, "\r\n", 0)}
l.Print("info", "[info] NO_COLOR log test") l.Print("info", "[info] NO_COLOR log test")
os.Setenv("NO_COLOR", "") os.Setenv("NO_COLOR", "")
gorm.NoColor() gorm.ResetColors()
} }
func TestSQLLog(t *testing.T) { func TestSQLLog(t *testing.T) {