github.com/blong14/gache@v0.0.0-20240124023949-89416fd8bbfa/internal/logging/debug.go (about) 1 package logging 2 3 import ( 4 "log" 5 "os" 6 "time" 7 8 genv "github.com/blong14/gache/internal/env" 9 ) 10 11 func init() { 12 if err := os.Setenv("DEBUG", "true"); err != nil { 13 log.Fatal(err) 14 } 15 } 16 17 // ShouldLog returns true if in DEBUG mode 18 func ShouldLog() bool { 19 return genv.Debug() 20 } 21 22 // Track logs information IFF the DEBUG env variable is "true" 23 // error handling should use std log package 24 func Track(format string, v ...any) { 25 if ShouldLog() { 26 log.Printf(format, v...) 27 } 28 } 29 30 func Trace(name string, t time.Time) time.Time { 31 if ShouldLog() { 32 if !t.IsZero() { 33 log.Printf("%s total: %s\n", name, time.Since(t)) 34 } else { 35 log.Printf("tracing %s\n", name) 36 } 37 } 38 return time.Now() 39 } 40 41 func TraceStart(name string) func() time.Time { 42 start := Trace(name, time.Time{}) 43 traceEnd := func() time.Time { 44 return Trace(name, start) 45 } 46 return traceEnd 47 }