Debian Codegraph @

log_counter.go

raw symbols SCIP
1 // Copyright © 2016 Steve Francia <[email protected]>.
2 //
3 // Use of this source code is governed by an MIT-style
4 // license that can be found in the LICENSE file.
5
6 package jwalterweatherman
7
8 import (
9 "io"
10 "sync/atomic"
11 )
12
13 // Counter is an io.Writer that increments a counter on Write.
14 type Counter struct {
15 count uint64
16 }
17
18 func (c *Counter) incr() {
19 atomic.AddUint64(&c.count, 1)
20 }
21
22 // Reset resets the counter.
23 func (c *Counter) Reset() {
24 atomic.StoreUint64(&c.count, 0)
25 }
26
27 // Count returns the current count.
28 func (c *Counter) Count() uint64 {
29 return atomic.LoadUint64(&c.count)
30 }
31
32 func (c *Counter) Write(p []byte) (n int, err error) {
33 c.incr()
34 return len(p), nil
35 }
36
37 // LogCounter creates a LogListener that counts log statements >= the given threshold.
38 func LogCounter(counter *Counter, t1 Threshold) LogListener {
39 return func(t2 Threshold) io.Writer {
40 if t2 < t1 {
41 // Not interested in this threshold.
42 return nil
43 }
44 return counter
45 }
46 }