github.com/hedzr/evendeep@v0.4.8/dbglog/log_delve.go (about) 1 //go:build delve || verbose 2 // +build delve verbose 3 4 package dbglog 5 6 import ( 7 "github.com/hedzr/log" 8 "github.com/hedzr/log/color" 9 "gopkg.in/hedzr/errors.v3" 10 ) 11 12 // LogValid shows dbglog.Log is enabled or abandoned 13 const LogValid bool = true 14 15 func SetLogEnabled() { logValid = true } // enables dbglog.Log at runtime 16 func SetLogDisabled() { logValid = false } // disables dbglog.Log at runtime 17 18 // DeferVisit moves errors in container ec, and log its via dbglog.Log 19 func DeferVisit(ec errors.Error, err *error) { 20 ec.Defer(err) 21 if *err != nil { 22 log.Skip(0).Errorf("%v", color.ToColor(color.Red, "%+v", *err)) 23 } 24 } 25 26 // Log will print formatted message while build-tags `delve` or `verbose` present. 27 // 28 // The flag dbglog.LogValid identify that state. 29 func Log(format string, args ...interface{}) { //nolint:goprintffuncname //no 30 if logValid { 31 log.Skip(0).Infof("%v", color.ToDim(format, args...)) // is there a `log` bug? so Skip(0) is a must-have rather than Skip(1), because stdLogger will detect how many frames should be skipped 32 // color.Dim(format, args...) 33 } 34 } 35 36 func Err(format string, args ...interface{}) { //nolint:goprintffuncname //so what 37 log.Skip(0).Infof("[ERROR] %v", color.ToColor(color.Red, format, args...)) 38 } 39 40 func Wrn(format string, args ...interface{}) { //nolint:goprintffuncname //so what 41 log.Skip(0).Infof("[WARN] %v", color.ToColor(color.Yellow, format, args...)) 42 } 43 44 func Colored(clr color.Color, format string, args ...interface{}) { //nolint:goprintffuncname //so what 45 log.Skip(0).Infof("%v", color.ToColor(clr, format, args...)) 46 }