Skip to content

Commit

Permalink
Improve debug logging in golang
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed May 25, 2018
1 parent 5353e9e commit 08e005d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
8 changes: 1 addition & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package main
import (
"flag"
"github.com/ry/v8worker2"
"io/ioutil"
"log"
"os"
"runtime/pprof"
)
Expand All @@ -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
}

Expand All @@ -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()
Expand Down
3 changes: 1 addition & 2 deletions os.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"github.com/golang/protobuf/proto"
"io/ioutil"
"log"
"net/url"
"os"
"path"
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
@@ -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)
Expand Down

0 comments on commit 08e005d

Please sign in to comment.