http urls monitor.

log_counter.go 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. "sync/atomic"
  8. )
  9. type logCounter struct {
  10. counter uint64
  11. }
  12. func (c *logCounter) incr() {
  13. atomic.AddUint64(&c.counter, 1)
  14. }
  15. func (c *logCounter) resetCounter() {
  16. atomic.StoreUint64(&c.counter, 0)
  17. }
  18. func (c *logCounter) getCount() uint64 {
  19. return atomic.LoadUint64(&c.counter)
  20. }
  21. func (c *logCounter) Write(p []byte) (n int, err error) {
  22. c.incr()
  23. return len(p), nil
  24. }
  25. // LogCountForLevel returns the number of log invocations for a given threshold.
  26. func (n *Notepad) LogCountForLevel(l Threshold) uint64 {
  27. return n.logCounters[l].getCount()
  28. }
  29. // LogCountForLevelsGreaterThanorEqualTo returns the number of log invocations
  30. // greater than or equal to a given threshold.
  31. func (n *Notepad) LogCountForLevelsGreaterThanorEqualTo(threshold Threshold) uint64 {
  32. var cnt uint64
  33. for i := int(threshold); i < len(n.logCounters); i++ {
  34. cnt += n.LogCountForLevel(Threshold(i))
  35. }
  36. return cnt
  37. }
  38. // ResetLogCounters resets the invocation counters for all levels.
  39. func (n *Notepad) ResetLogCounters() {
  40. for _, np := range n.logCounters {
  41. np.resetCounter()
  42. }
  43. }