Skip to content

Instantly share code, notes, and snippets.

@ruandao
Created July 5, 2018 11:38
Show Gist options
  • Save ruandao/90d63e9c3906336d01d0a23ccfe7324e to your computer and use it in GitHub Desktop.
Save ruandao/90d63e9c3906336d01d0a23ccfe7324e to your computer and use it in GitHub Desktop.
用于统计某个函数执行了多久 defer Trace(xxx...)()
// Trace trace
func Trace(name string, param ...interface{}) func() {
if !TraceSwitch {
return func() {}
}
start := time.Now()
log.Println("####Enter", name, param)
return func() {
_, file, line, ok := runtime.Caller(1)
if !ok {
file = "???"
line = 0
}
for i := len(file) - 1; i > 0; i-- {
if file[i] == '/' {
file = file[i+1:]
break
}
}
log.Println("#####Exit", name, fmt.Sprintf("%s:%d", file, line), time.Since(start))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment