github.com/halybang/go-ethereum@v1.0.5-0.20180325041310-3b262bc1367c/functrace/functrace.go (about) 1 // based on http://sabhiram.com/go/2015/01/21/golang_trace_fns_part_1.html 2 package functrace 3 4 import ( 5 "fmt" 6 "regexp" 7 "runtime" 8 9 "github.com/wanchain/go-wanchain/log" 10 ) 11 12 // Trace Functions 13 func Enter(str ...string) { 14 // Skip this function, and fetch the PC and file for its parent 15 pc, _, _, _ := runtime.Caller(1) 16 // Retrieve a Function object this functions parent 17 functionObject := runtime.FuncForPC(pc) 18 // Regex to extract just the function name (and not the module path) 19 extractFnName := regexp.MustCompile(`^.*\.(.*)$`) 20 fnName := extractFnName.ReplaceAllString(functionObject.Name(), "$1") 21 22 var smsg string 23 if len(str) > 0 { 24 smsg = str[0] 25 } 26 log.Debug(fmt.Sprintf(">> Entering %s(%s)", fnName, smsg)) 27 } 28 29 func Exit() { 30 // Skip this function, and fetch the PC and file for its parent 31 pc, _, _, _ := runtime.Caller(1) 32 // Retrieve a Function object this functions parent 33 functionObject := runtime.FuncForPC(pc) 34 // Regex to extract just the function name (and not the module path) 35 extractFnName := regexp.MustCompile(`^.*\.(.*)$`) 36 fnName := extractFnName.ReplaceAllString(functionObject.Name(), "$1") 37 log.Debug(fmt.Sprintf("<< Exiting %s()", fnName)) 38 }