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