Add --revert-pattern for logcounter

This commit is contained in:
Eric Lin
2024-01-17 18:21:57 +00:00
parent 18630b6c78
commit 1002df5e13
2 changed files with 16 additions and 8 deletions
+13 -8
View File
@@ -40,10 +40,11 @@ const (
)
type logCounter struct {
logCh <-chan *systemtypes.Log
buffer systemlogmonitor.LogBuffer
pattern string
clock clock.Clock
logCh <-chan *systemtypes.Log
buffer systemlogmonitor.LogBuffer
pattern string
revertPattern string
clock clock.Clock
}
func NewJournaldLogCounter(options *options.LogCounterOptions) (types.LogCounter, error) {
@@ -59,10 +60,11 @@ func NewJournaldLogCounter(options *options.LogCounterOptions) (types.LogCounter
return nil, fmt.Errorf("error watching journald: %v", err)
}
return &logCounter{
logCh: logCh,
buffer: systemlogmonitor.NewLogBuffer(bufferSize),
pattern: options.Pattern,
clock: clock.RealClock{},
logCh: logCh,
buffer: systemlogmonitor.NewLogBuffer(bufferSize),
pattern: options.Pattern,
revertPattern: options.RevertPattern,
clock: clock.RealClock{},
}, nil
}
@@ -84,6 +86,9 @@ func (e *logCounter) Count() (count int, err error) {
if len(e.buffer.Match(e.pattern)) != 0 {
count++
}
if len(e.buffer.Match(e.revertPattern)) != 0 {
count--
}
case <-e.clock.After(timeout):
// Don't block forever if we do not get any new messages
return