Created
July 5, 2018 11:38
-
-
Save ruandao/90d63e9c3906336d01d0a23ccfe7324e to your computer and use it in GitHub Desktop.
用于统计某个函数执行了多久
defer Trace(xxx...)()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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