Make getting function name a utility function

This commit is contained in:
Kovid Goyal
2023-07-25 11:39:52 +05:30
parent bb897fa299
commit ca485cc0f7
2 changed files with 14 additions and 11 deletions

View File

@@ -4,13 +4,12 @@ package loop
import (
"fmt"
"reflect"
"runtime"
"time"
"golang.org/x/exp/slices"
"kitty/tools/tty"
"kitty/tools/utils"
)
var debugprintln = tty.DebugPrintln
@@ -28,15 +27,7 @@ func (self *timer) update_deadline(now time.Time) {
}
func (self timer) String() string {
funcname := "<nil>"
if self.callback != nil {
p := reflect.ValueOf(self.callback).Pointer()
f := runtime.FuncForPC(p)
if f != nil {
funcname = f.Name()
}
}
return fmt.Sprintf("Timer(id=%d, callback=%s, deadline=%s, repeats=%v)", self.id, funcname, self.deadline.Sub(time.Now()), self.repeats)
return fmt.Sprintf("Timer(id=%d, callback=%s, deadline=%s, repeats=%v)", self.id, utils.FunctionName(self.callback), self.deadline.Sub(time.Now()), self.repeats)
}
func (self *Loop) add_timer(interval time.Duration, repeats bool, callback TimerCallback) (IdType, error) {

View File

@@ -294,3 +294,15 @@ func SourceLoc(skip_frames ...int) string {
}
return "unknown"
}
func FunctionName(a any) string {
if a == nil {
return "<nil>"
}
p := reflect.ValueOf(a).Pointer()
f := runtime.FuncForPC(p)
if f != nil {
return f.Name()
}
return ""
}