docstore/pkg/log/scanlog.go
2019-08-26 20:51:04 +02:00

47 lines
725 B
Go

package log
import (
"fmt"
"git.giftfish.de/ston1th/docstore/pkg/core"
"sync"
)
const defaultSize = 500
type ScanLog struct {
// protects logs
sync.RWMutex
size int
logs []string
}
func NewScanLog(size int) *ScanLog {
if size <= 0 {
size = defaultSize
}
return &ScanLog{size: size}
}
func (l *ScanLog) Printf(f string, v ...interface{}) {
s := fmt.Sprintf(f, v...)
Println(s)
s = core.LogTime() + s
l.Lock()
llen := len(l.logs)
if llen < l.size {
l.logs = append(l.logs, s)
} else if llen == l.size {
l.logs = append(l.logs[1:], s)
}
l.Unlock()
}
func (l *ScanLog) Logs() (logs string) {
l.RLock()
for i := len(l.logs) - 1; i >= 0; i-- {
logs += "\n" + l.logs[i]
}
l.RUnlock()
return
}