1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-25 15:29:32 -05:00

Improve debug logging in golang

This commit is contained in:
Ryan Dahl 2018-05-25 12:25:55 -04:00
parent 5353e9e90f
commit 08e005d45f
3 changed files with 10 additions and 9 deletions

View file

@ -3,8 +3,6 @@ package main
import ( import (
"flag" "flag"
"github.com/ry/v8worker2" "github.com/ry/v8worker2"
"io/ioutil"
"log"
"os" "os"
"runtime/pprof" "runtime/pprof"
) )
@ -28,10 +26,6 @@ func FlagsParse() []string {
} }
args = v8worker2.SetFlags(args) args = v8worker2.SetFlags(args)
// Unless the debug flag is specified, discard logs.
if !*flagDebug {
log.SetOutput(ioutil.Discard)
}
return args return args
} }
@ -43,7 +37,7 @@ func main() {
if *flagGoProf != "" { if *flagGoProf != "" {
f, err := os.Create(*flagGoProf) f, err := os.Create(*flagGoProf)
if err != nil { if err != nil {
log.Fatal(err) panic(err)
} }
pprof.StartCPUProfile(f) pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile() defer pprof.StopCPUProfile()

3
os.go
View file

@ -3,7 +3,6 @@ package main
import ( import (
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"io/ioutil" "io/ioutil"
"log"
"net/url" "net/url"
"os" "os"
"path" "path"
@ -37,7 +36,7 @@ func InitOS() {
func ResolveModule(moduleSpecifier string, containingFile string) ( func ResolveModule(moduleSpecifier string, containingFile string) (
moduleName string, filename string, err error) { moduleName string, filename string, err error) {
log.Printf("ResolveModule %s %s", moduleSpecifier, containingFile) logDebug("ResolveModule %s %s", moduleSpecifier, containingFile)
moduleUrl, err := url.Parse(moduleSpecifier) moduleUrl, err := url.Parse(moduleSpecifier)
if err != nil { if err != nil {

View file

@ -1,10 +1,18 @@
package main package main
import ( import (
"fmt"
"net/url" "net/url"
"os" "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) { func assert(cond bool, msg string) {
if !cond { if !cond {
panic(msg) panic(msg)