You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
810 B
Go

package traces
import (
"context"
"joybuywork/internal/app"
"fmt"
"time"
)
type FnTimer struct {
*FnTimers
name string
start time.Time
}
func (ft FnTimer) Stop() {
if ft.FnTimers == nil {
return
}
elapsed := time.Since(ft.start)
ft.content = append(ft.content, fmt.Sprintf("[函数耗时] %s: %s\n", ft.name, elapsed))
}
type FnTimers struct {
ctx context.Context
content []string
}
func NewFnTimers(ctx context.Context) *FnTimers {
return &FnTimers{
ctx: ctx,
}
}
func (s *FnTimers) Trace(name string) (ft FnTimer) {
if s == nil {
return
}
return FnTimer{
FnTimers: s,
name: name,
start: time.Now(),
}
}
func (s *FnTimers) Push() {
if s == nil {
return
}
var str string
for _, t := range s.content {
str += t + "\n"
}
app.FeishuNormalSend(str)
}