matterbridge/vendor/github.com/spf13/jwalterweatherman/log_counter.go

56 lines
1.2 KiB
Go
Raw Normal View History

2018-03-04 14:46:13 -08:00
// Copyright © 2016 Steve Francia <spf@spf13.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package jwalterweatherman
import (
"sync/atomic"
)
2021-03-20 14:40:23 -07:00
type logCounter struct {
counter uint64
2018-03-04 14:46:13 -08:00
}
2021-03-20 14:40:23 -07:00
func (c *logCounter) incr() {
atomic.AddUint64(&c.counter, 1)
2018-03-04 14:46:13 -08:00
}
2021-03-20 14:40:23 -07:00
func (c *logCounter) resetCounter() {
atomic.StoreUint64(&c.counter, 0)
2018-03-04 14:46:13 -08:00
}
2021-03-20 14:40:23 -07:00
func (c *logCounter) getCount() uint64 {
return atomic.LoadUint64(&c.counter)
2018-03-04 14:46:13 -08:00
}
2021-03-20 14:40:23 -07:00
func (c *logCounter) Write(p []byte) (n int, err error) {
2018-03-04 14:46:13 -08:00
c.incr()
return len(p), nil
}
2021-03-20 14:40:23 -07:00
// LogCountForLevel returns the number of log invocations for a given threshold.
func (n *Notepad) LogCountForLevel(l Threshold) uint64 {
return n.logCounters[l].getCount()
}
// LogCountForLevelsGreaterThanorEqualTo returns the number of log invocations
// greater than or equal to a given threshold.
func (n *Notepad) LogCountForLevelsGreaterThanorEqualTo(threshold Threshold) uint64 {
var cnt uint64
for i := int(threshold); i < len(n.logCounters); i++ {
cnt += n.LogCountForLevel(Threshold(i))
}
return cnt
}
// ResetLogCounters resets the invocation counters for all levels.
func (n *Notepad) ResetLogCounters() {
for _, np := range n.logCounters {
np.resetCounter()
2018-03-04 14:46:13 -08:00
}
}