package tdsync
import (
"context"
"github.com/go-faster/errors"
"go.uber.org/zap"
"github.com/gotd/td/clock"
)
type LogGroup struct {
group CancellableGroup
log *zap .Logger
clock clock .Clock
}
func NewLogGroup (parent context .Context , log *zap .Logger ) *LogGroup {
return &LogGroup {
group : *NewCancellableGroup (parent ),
log : log ,
clock : clock .System ,
}
}
func (g *LogGroup ) SetClock (c clock .Clock ) {
g .clock = c
}
func (g *LogGroup ) Go (taskName string , f func (groupCtx context .Context ) error ) {
g .group .Go (func (ctx context .Context ) error {
start := g .clock .Now ()
l := g .log .With (zap .String ("task" , taskName )).WithOptions (zap .AddCallerSkip (1 ))
l .Debug ("Task started" )
if err := f (ctx ); err != nil {
elapsed := g .clock .Now ().Sub (start )
l .Debug ("Task stopped" , zap .Error (err ), zap .Duration ("elapsed" , elapsed ))
return errors .Wrapf (err , "task %s" , taskName )
}
elapsed := g .clock .Now ().Sub (start )
l .Debug ("Task complete" , zap .Duration ("elapsed" , elapsed ))
return nil
})
}
func (g *LogGroup ) Cancel () {
g .group .Cancel ()
}
func (g *LogGroup ) Wait () error {
return g .group .Wait ()
}
The pages are generated with Golds v0.6.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds .