2015-10-23 23:41:41 +00:00
|
|
|
package stats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Logger struct {
|
2015-10-26 23:11:09 +00:00
|
|
|
base
|
|
|
|
|
2015-10-23 23:41:41 +00:00
|
|
|
out io.Writer
|
|
|
|
interval time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewLogger(out io.Writer, interval time.Duration) *Logger {
|
|
|
|
l := &Logger{
|
|
|
|
out: out,
|
|
|
|
interval: interval,
|
|
|
|
}
|
2015-10-26 23:11:09 +00:00
|
|
|
l.init()
|
2015-10-23 23:41:41 +00:00
|
|
|
go l.printWithInterval()
|
|
|
|
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewStdoutLogger(interval time.Duration) *Logger {
|
2015-10-25 11:35:39 +00:00
|
|
|
return NewLogger(os.Stdout, interval)
|
2015-10-23 23:41:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) Print() {
|
2015-10-26 23:54:00 +00:00
|
|
|
for _, s := range l.stats {
|
|
|
|
l.out.Write([]byte(s.String()))
|
|
|
|
l.out.Write([]byte{'\n'})
|
2015-10-23 23:41:41 +00:00
|
|
|
s.time.Clear()
|
|
|
|
s.errors.Clear()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) printWithInterval() {
|
|
|
|
if l.interval == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for range time.NewTicker(l.interval).C {
|
|
|
|
l.Print()
|
|
|
|
}
|
|
|
|
}
|