github.com/attic-labs/noms@v0.0.0-20210827224422-e5fa29d95e8b/samples/go/decent/dbg/debug.go (about) 1 // Copyright 2017 Attic Labs, Inc. All rights reserved. 2 // Licensed under the Apache License, version 2.0: 3 // http://www.apache.org/licenses/LICENSE-2.0 4 5 package dbg 6 7 import ( 8 "fmt" 9 "github.com/attic-labs/noms/go/d" 10 "log" 11 "os" 12 "strconv" 13 ) 14 15 var ( 16 Filepath = "/tmp/noms-dbg.log" 17 lg = NewLogger(Filepath) 18 ) 19 20 func NewLogger(fp string) *log.Logger { 21 f, err := os.OpenFile(fp, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644) 22 d.PanicIfError(err) 23 pid := strconv.FormatInt(int64(os.Getpid()), 10) 24 return log.New(f, pid+": ", 0644) 25 } 26 27 func GetLogger() *log.Logger { 28 return lg 29 } 30 31 func SetLogger(newLg *log.Logger) { 32 lg = newLg 33 } 34 35 func Debug(s string, args ...interface{}) { 36 s1 := fmt.Sprintf(s, args...) 37 lg.Println(s1) 38 } 39 40 func BoxF(s string, args ...interface{}) func() { 41 s1 := fmt.Sprintf(s, args...) 42 Debug("starting %s", s1) 43 f := func() { 44 Debug("finished %s", s1) 45 } 46 return f 47 }