Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Commit

Permalink
summary/: export new log collector (#602)
Browse files Browse the repository at this point in the history
* export log exporter
* make sure dumpling can redirect log in progress.go
  • Loading branch information
lichunzhu authored Nov 19, 2020
1 parent e3faf51 commit bb4294d
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pkg/gluetikv/glue.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (Glue) OwnsStorage() bool {

// StartProgress implements glue.Glue.
func (Glue) StartProgress(ctx context.Context, cmdName string, total int64, redirectLog bool) glue.Progress {
return utils.StartProgress(ctx, cmdName, total, redirectLog)
return utils.StartProgress(ctx, cmdName, total, redirectLog, nil)
}

// Record implements glue.Glue.
Expand Down
7 changes: 4 additions & 3 deletions pkg/summary/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type LogCollector interface {

type logFunc func(msg string, fields ...zap.Field)

var collector LogCollector = newLogCollector(log.Info)
var collector LogCollector = NewLogCollector(log.Info)

// InitCollector initilize global collector instance.
func InitCollector( // revive:disable-line:flag-parameter
Expand All @@ -62,7 +62,7 @@ func InitCollector( // revive:disable-line:flag-parameter
}
}
}
collector = newLogCollector(logF)
collector = NewLogCollector(logF)
}

type logCollector struct {
Expand All @@ -82,7 +82,8 @@ type logCollector struct {
log logFunc
}

func newLogCollector(log logFunc) LogCollector {
// NewLogCollector returns a new LogCollector.
func NewLogCollector(log logFunc) LogCollector {
return &logCollector{
successUnitCount: 0,
failureUnitCount: 0,
Expand Down
2 changes: 1 addition & 1 deletion pkg/summary/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (suit *testCollectorSuite) TestSumDurationInt(c *C) {
logger := func(msg string, fs ...zap.Field) {
fields = append(fields, fs...)
}
col := newLogCollector(logger)
col := NewLogCollector(logger)
col.CollectDuration("a", time.Second)
col.CollectDuration("b", time.Second)
col.CollectDuration("b", time.Second)
Expand Down
14 changes: 11 additions & 3 deletions pkg/utils/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"go.uber.org/zap"
)

type logFunc func(msg string, fields ...zap.Field)

// ProgressPrinter prints a progress bar.
type ProgressPrinter struct {
name string
Expand Down Expand Up @@ -53,6 +55,7 @@ func (pp *ProgressPrinter) Close() {
// goPrintProgress starts a gorouinte and prints progress.
func (pp *ProgressPrinter) goPrintProgress(
ctx context.Context,
logFuncImpl logFunc,
testWriter io.Writer, // Only for tests
) {
cctx, cancel := context.WithCancel(ctx)
Expand All @@ -67,7 +70,10 @@ func (pp *ProgressPrinter) goPrintProgress(
bar.Set(pb.Terminal, false) // Do not use terminal width
// Hack! set Color to avoid separate progress string
bar.Set(pb.Color, true)
bar.SetWriter(&wrappedWriter{name: pp.name})
if logFuncImpl == nil {
logFuncImpl = log.Info
}
bar.SetWriter(&wrappedWriter{name: pp.name, log: logFuncImpl})
} else {
tmpl := `{{string . "barName" | green}} {{ bar . "<" "-" (cycle . "-" "\\" "|" "/" ) "." ">"}} {{percent .}}`
bar.SetTemplateString(tmpl)
Expand Down Expand Up @@ -110,6 +116,7 @@ func (pp *ProgressPrinter) goPrintProgress(

type wrappedWriter struct {
name string
log logFunc
}

func (ww *wrappedWriter) Write(p []byte) (int, error) {
Expand All @@ -123,7 +130,7 @@ func (ww *wrappedWriter) Write(p []byte) (int, error) {
if err := json.Unmarshal(p, &info); err != nil {
return 0, err
}
log.Info("progress",
ww.log("progress",
zap.String("step", ww.name),
zap.String("progress", info.P),
zap.String("count", info.C),
Expand All @@ -139,8 +146,9 @@ func StartProgress(
name string,
total int64,
redirectLog bool,
log logFunc,
) *ProgressPrinter {
progress := NewProgressPrinter(name, total, redirectLog)
progress.goPrintProgress(ctx, nil)
progress.goPrintProgress(ctx, log, nil)
return progress
}
6 changes: 3 additions & 3 deletions pkg/utils/progress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (r *testProgressSuite) TestProgress(c *C) {
var p string
pCh2 := make(chan string, 2)
progress2 := NewProgressPrinter("test", 2, false)
progress2.goPrintProgress(ctx, &testWriter{
progress2.goPrintProgress(ctx, nil, &testWriter{
fn: func(p string) { pCh2 <- p },
})
progress2.Inc()
Expand All @@ -46,7 +46,7 @@ func (r *testProgressSuite) TestProgress(c *C) {

pCh4 := make(chan string, 4)
progress4 := NewProgressPrinter("test", 4, false)
progress4.goPrintProgress(ctx, &testWriter{
progress4.goPrintProgress(ctx, nil, &testWriter{
fn: func(p string) { pCh4 <- p },
})
progress4.Inc()
Expand All @@ -61,7 +61,7 @@ func (r *testProgressSuite) TestProgress(c *C) {

pCh8 := make(chan string, 8)
progress8 := NewProgressPrinter("test", 8, false)
progress8.goPrintProgress(ctx, &testWriter{
progress8.goPrintProgress(ctx, nil, &testWriter{
fn: func(p string) { pCh8 <- p },
})
progress8.Inc()
Expand Down

0 comments on commit bb4294d

Please sign in to comment.