Skip to content

Commit

Permalink
add prototype Caller
Browse files Browse the repository at this point in the history
  • Loading branch information
mallvielfrass committed Jul 10, 2021
1 parent 983a6ab commit 8e91c09
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
81 changes: 81 additions & 0 deletions caller.go
Original file line number Diff line number Diff line change
@@ -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()

}
1 change: 1 addition & 0 deletions cmd/ex.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,5 @@ func main() {
panic(err)
}
fmc.PrintStruct(config)

}

0 comments on commit 8e91c09

Please sign in to comment.