From 08e005d45f475a8b45d18ca887dbf790e93bc1db Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Fri, 25 May 2018 12:25:55 -0400 Subject: [PATCH] Improve debug logging in golang --- main.go | 8 +------- os.go | 3 +-- util.go | 8 ++++++++ 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index d557ab2045..ab1828d940 100644 --- a/main.go +++ b/main.go @@ -3,8 +3,6 @@ package main import ( "flag" "github.com/ry/v8worker2" - "io/ioutil" - "log" "os" "runtime/pprof" ) @@ -28,10 +26,6 @@ func FlagsParse() []string { } args = v8worker2.SetFlags(args) - // Unless the debug flag is specified, discard logs. - if !*flagDebug { - log.SetOutput(ioutil.Discard) - } return args } @@ -43,7 +37,7 @@ func main() { if *flagGoProf != "" { f, err := os.Create(*flagGoProf) if err != nil { - log.Fatal(err) + panic(err) } pprof.StartCPUProfile(f) defer pprof.StopCPUProfile() diff --git a/os.go b/os.go index 484d07731a..98997cedc1 100644 --- a/os.go +++ b/os.go @@ -3,7 +3,6 @@ package main import ( "github.com/golang/protobuf/proto" "io/ioutil" - "log" "net/url" "os" "path" @@ -37,7 +36,7 @@ func InitOS() { func ResolveModule(moduleSpecifier string, containingFile string) ( moduleName string, filename string, err error) { - log.Printf("ResolveModule %s %s", moduleSpecifier, containingFile) + logDebug("ResolveModule %s %s", moduleSpecifier, containingFile) moduleUrl, err := url.Parse(moduleSpecifier) if err != nil { diff --git a/util.go b/util.go index 9fab27238c..1871156d32 100644 --- a/util.go +++ b/util.go @@ -1,10 +1,18 @@ package main import ( + "fmt" "net/url" "os" ) +func logDebug(format string, v ...interface{}) { + // Unless the debug flag is specified, discard logs. + if *flagDebug { + fmt.Printf(format+"\n", v) + } +} + func assert(cond bool, msg string) { if !cond { panic(msg)