本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
958B

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