feat: add logger

This commit is contained in:
崔竞宁
2022-09-14 14:55:25 +08:00
parent 5e4e39988a
commit b76cde8fa4
2 changed files with 17 additions and 3 deletions

9
acp.go
View File

@@ -64,9 +64,6 @@ func New(ctx context.Context, opts ...Option) (*Copyer, error) {
updateProgressBar: func(f func(bar *progressbar.ProgressBar)) {}, updateProgressBar: func(f func(bar *progressbar.ProgressBar)) {},
updateCopying: func(f func(set map[int64]struct{})) {}, updateCopying: func(f func(set map[int64]struct{})) {},
logf: func(l logrus.Level, format string, args ...any) {
logrus.StandardLogger().Logf(l, format, args...)
},
badDsts: make(map[string]error), badDsts: make(map[string]error),
writePipe: make(chan *writeJob, 32), writePipe: make(chan *writeJob, 32),
@@ -84,6 +81,12 @@ func New(ctx context.Context, opts ...Option) (*Copyer, error) {
c.readingFiles = make(chan struct{}, 1) c.readingFiles = make(chan struct{}, 1)
} }
if opt.logger != nil {
c.logf = func(l logrus.Level, format string, args ...any) { opt.logger.Logf(l, format, args...) }
} else {
c.logf = func(l logrus.Level, format string, args ...any) { logrus.StandardLogger().Logf(l, format, args...) }
}
c.running.Add(1) c.running.Add(1)
go wrap(ctx, func() { c.run(ctx) }) go wrap(ctx, func() { c.run(ctx) })
return c, nil return c, nil

11
opt.go
View File

@@ -5,6 +5,8 @@ import (
"os" "os"
"path" "path"
"strings" "strings"
"github.com/sirupsen/logrus"
) )
type source struct { type source struct {
@@ -34,6 +36,8 @@ type option struct {
autoFill bool autoFill bool
autoFillSplitDepth int autoFillSplitDepth int
logger *logrus.Logger
} }
func newOption() *option { func newOption() *option {
@@ -183,3 +187,10 @@ func WithAutoFill(on bool, depth int) Option {
return o return o
} }
} }
func WithLogger(logger *logrus.Logger) Option {
return func(o *option) *option {
o.logger = logger
return o
}
}