Created
August 8, 2014 09:33
-
-
Save zhangskills/11acb5420ea917174b54 to your computer and use it in GitHub Desktop.
获取调用log.Debug的函数名和代码所在的行数
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
package main | |
import "runtime" | |
import "log" | |
func main() { | |
test() | |
} | |
func test() { | |
test2() | |
} | |
func test2(){ | |
pc,file,line,ok := runtime.Caller(2) | |
log.Println(pc) | |
log.Println(file) | |
log.Println(line) | |
log.Println(ok) | |
f := runtime.FuncForPC(pc) | |
log.Println(f.Name()) | |
pc,file,line,ok = runtime.Caller(0) | |
log.Println(pc) | |
log.Println(file) | |
log.Println(line) | |
log.Println(ok) | |
f = runtime.FuncForPC(pc) | |
log.Println(f.Name()) | |
pc,file,line,ok = runtime.Caller(1) | |
log.Println(pc) | |
log.Println(file) | |
log.Println(line) | |
log.Println(ok) | |
f = runtime.FuncForPC(pc) | |
log.Println(f.Name()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment