From 8e91c09327a4feaf2eab2ae3e6d3efccbf6f785b Mon Sep 17 00:00:00 2001 From: mallvielfrass Date: Sat, 10 Jul 2021 09:21:12 +0300 Subject: [PATCH] add prototype Caller --- caller.go | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ cmd/ex.go | 1 + 2 files changed, 82 insertions(+) create mode 100644 caller.go diff --git a/caller.go b/caller.go new file mode 100644 index 0000000..cd81c01 --- /dev/null +++ b/caller.go @@ -0,0 +1,81 @@ +package fmc + +import ( + "runtime" + "strings" +) + +//Caller log run function +func Caller() { + pc := make([]uintptr, 40) + n := runtime.Callers(0, pc) + + // Printfln("#rbtn= #gbt%d", n) + pc = pc[0:n] // pass only valid pcs to runtime.CallersFrames + frames := runtime.CallersFrames(pc) + i := 0 + for { + + frame, more := frames.Next() + + name, line, file := frame.Func, frame.Line, frame.File + // fmt.Println(name) + nm := "" + if name == nil { + // fmt.Println("name==nil") + f := runtime.FuncForPC(frame.PC) + //file, line := f.FileLine(frame.PC) + //fmt.Printf("%s:%d %s\n", file, line, f.Name()) + nm = f.Name() + //Printfln("#gbt%s#wbt:#gbt%d #ybt%s", file, line, f.Name()) + } else { + nm = name.Name() + + } + + if strings.Contains(nm, "fmc.Caller") || strings.Contains(nm, "runtime.Callers") || strings.Contains(nm, "runtime.goexit") || strings.Contains(nm, "runtime.main") { + + } else { + if i == 0 { + Printf("#wbtRun> ") + i++ + } else { + Printf("\t#rbtFrom> ") + i++ + } + Printfln("#gbt%s#wbt:#gbt%d #ybt%s", file, line, nm) + } + + // } + + if !more { + break + } + } +} + +//WhoCallerIs calling func name +func WhoCallerIs() string { + pc := make([]uintptr, 40) + n := runtime.Callers(0, pc) + + // Printfln("#rbtn= #gbt%d", n) + pc = pc[2 : n-2] // pass only valid pcs to runtime.CallersFrames + frames := runtime.CallersFrames(pc) + + frame, _ := frames.Next() + + //name, line, file := frame.Func, frame.Line, frame.File + // fmt.Println(name) + if frame.Func == nil { + // fmt.Println("name==nil") + f := runtime.FuncForPC(frame.PC) + //file, line := f.FileLine(frame.PC) + //fmt.Printf("%s:%d %s\n", file, line, f.Name()) + // Printfln("#gbt%s#wbt:#gbt%d #ybt%s", file, line, f.Name()) + return f.Name() + } + // Printfln("#gbt%s#wbt:#gbt%d #ybt%s", file, line, name.Name()) + return frame.Func.Name() + +} diff --git a/cmd/ex.go b/cmd/ex.go index dfbd198..eb836a1 100644 --- a/cmd/ex.go +++ b/cmd/ex.go @@ -69,4 +69,5 @@ func main() { panic(err) } fmc.PrintStruct(config) + }