mirror of
https://github.com/bluenviron/mediamtx
synced 2025-01-07 07:20:01 +00:00
5fb7f4e846
needed by #2255
35 lines
646 B
Go
35 lines
646 B
Go
package logger
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
minIntervalBetweenWarnings = 1 * time.Second
|
|
)
|
|
|
|
type limitedLogger struct {
|
|
w Writer
|
|
mutex sync.Mutex
|
|
lastPrinted time.Time
|
|
}
|
|
|
|
// NewLimitedLogger is a wrapper around a Writer that limits printed messages.
|
|
func NewLimitedLogger(w Writer) Writer {
|
|
return &limitedLogger{
|
|
w: w,
|
|
}
|
|
}
|
|
|
|
// Log is the main logging function.
|
|
func (l *limitedLogger) Log(level Level, format string, args ...interface{}) {
|
|
now := time.Now()
|
|
l.mutex.Lock()
|
|
if now.Sub(l.lastPrinted) >= minIntervalBetweenWarnings {
|
|
l.lastPrinted = now
|
|
l.w.Log(level, format, args...)
|
|
}
|
|
l.mutex.Unlock()
|
|
}
|