Nicer debug output of timers

This commit is contained in:
Kovid Goyal
2023-07-24 12:19:27 +05:30
parent ce35532aa5
commit f4b0183341
3 changed files with 24 additions and 6 deletions

View File

@@ -4,11 +4,33 @@ package loop
import (
"fmt"
"reflect"
"runtime"
"time"
"golang.org/x/exp/slices"
"kitty/tools/tty"
)
var debugprintln = tty.DebugPrintln
func (self *timer) update_deadline(now time.Time) {
self.deadline = now.Add(self.interval)
}
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(callback=%s, deadline=%s, repeats=%v)", funcname, self.deadline.Sub(time.Now()), self.repeats)
}
func (self *Loop) add_timer(interval time.Duration, repeats bool, callback TimerCallback) (IdType, error) {
if self.timers == nil {
return 0, fmt.Errorf("Cannot add timers before starting the run loop, add them in OnInitialize instead")