diff --git a/gossip/comm/comm_impl.go b/gossip/comm/comm_impl.go index 65454a23310..cdc4c842c14 100644 --- a/gossip/comm/comm_impl.go +++ b/gossip/comm/comm_impl.go @@ -87,7 +87,7 @@ func NewCommInstanceWithServer(port int, idMapper identity.Mapper, peerIdentity selfCertHash: certHash, PKIID: idMapper.GetPKIidOfCert(peerIdentity), idMapper: idMapper, - logger: util.GetLogger(util.LOGGING_COMM_MODULE, fmt.Sprintf("%d", port)), + logger: util.GetLogger(util.LoggingCommModule, fmt.Sprintf("%d", port)), peerIdentity: peerIdentity, opts: dialOpts, port: port, diff --git a/gossip/discovery/discovery_impl.go b/gossip/discovery/discovery_impl.go index d70384aa654..108f5815830 100644 --- a/gossip/discovery/discovery_impl.go +++ b/gossip/discovery/discovery_impl.go @@ -115,7 +115,7 @@ func NewDiscoveryService(bootstrapPeers []string, self NetworkMember, comm CommS lock: &sync.RWMutex{}, toDieChan: make(chan struct{}, 1), toDieFlag: int32(0), - logger: util.GetLogger(util.LOGGING_DISCOVERY_MODULE, self.Endpoint), + logger: util.GetLogger(util.LoggingDiscoveryModule, self.Endpoint), } d.logger.SetLevel(logging.WARNING) diff --git a/gossip/gossip/gossip_impl.go b/gossip/gossip/gossip_impl.go index 625710a79f5..7ba10336a42 100644 --- a/gossip/gossip/gossip_impl.go +++ b/gossip/gossip/gossip_impl.go @@ -77,7 +77,7 @@ func NewGossipService(conf *Config, s *grpc.Server, secAdvisor api.SecurityAdvis var c comm.Comm var err error idMapper := identity.NewIdentityMapper(mcs) - lgr := util.GetLogger(util.LOGGING_GOSSIP_MODULE, conf.ID) + lgr := util.GetLogger(util.LoggingGossipModule, conf.ID) if s == nil { c, err = createCommWithServer(conf.BindPort, idMapper, selfIdentity) } else { diff --git a/gossip/util/logging.go b/gossip/util/logging.go index 17556abd4ee..34acd8395e5 100644 --- a/gossip/util/logging.go +++ b/gossip/util/logging.go @@ -28,11 +28,16 @@ import ( ) const ( - LOGGING_MESSAGE_BUFF_MODULE = "mbuff" - LOGGING_EMITTER_MODULE = "emitter" - LOGGING_GOSSIP_MODULE = "gossip" - LOGGING_DISCOVERY_MODULE = "discovery" - LOGGING_COMM_MODULE = "comm" + // LoggingMessageBuffModule defines the module for logging message buffer + LoggingMessageBuffModule = "mbuff" + // LoggingEmitterModule defines the module for logging emitter + LoggingEmitterModule = "emitter" + // LoggingGossipModule defines the module for logging gossip + LoggingGossipModule = "gossip" + // LoggingDiscoveryModule defines the module for logging discovery + LoggingDiscoveryModule = "discovery" + // LoggingCommModule defines the module for logging communication + LoggingCommModule = "comm" ) var loggersByModules = make(map[string]*Logger) @@ -48,25 +53,30 @@ func init() { grpclog.SetLogger(log.New(ioutil.Discard, "", 0)) } +// Logger defines a logger for gossip type Logger struct { logging.Logger module string } +// SetDefaultFormat sets the formatter for the gossip logger func SetDefaultFormat(formatStr string) { format = logging.MustStringFormatter(formatStr) } +// SetDefaultLoggingLevel sets the default logging level for the gossip logger func SetDefaultLoggingLevel(level logging.Level) { defaultLevel = level } +// SetLevel sets the level for the logger func (l *Logger) SetLevel(lvl logging.Level) { logging.SetLevel(lvl, l.module) } -func GetLogger(module string, peerId string) *Logger { - module = module + "-" + peerId +// GetLogger returns a logger for given gossip module and peerID +func GetLogger(module string, peerID string) *Logger { + module = module + "-" + peerID lock.Lock() defer lock.Unlock() diff --git a/gossip/util/misc.go b/gossip/util/misc.go index 3b5e0413f3c..e3cb6e69708 100644 --- a/gossip/util/misc.go +++ b/gossip/util/misc.go @@ -24,12 +24,14 @@ import ( "sync" ) +// Equals returns whether a and b are the same type Equals func(a interface{}, b interface{}) bool func init() { rand.Seed(42) } +// IndexInSlice returns the index of given object o in array func IndexInSlice(array interface{}, o interface{}, equals Equals) int { arr := reflect.ValueOf(array) for i := 0; i < arr.Len(); i++ { @@ -44,6 +46,8 @@ func numbericEqual(a interface{}, b interface{}) bool { return a.(int) == b.(int) } +// GetRandomIndices returns a slice of random indices +// from 0 to given highestIndex func GetRandomIndices(indiceCount, highestIndex int) []int { if highestIndex+1 < indiceCount { return nil @@ -67,29 +71,34 @@ func GetRandomIndices(indiceCount, highestIndex int) []int { return indices } +// Abs returns abs(a-b) func Abs(a, b uint64) uint64 { if a > b { return a - b - } else { - return b - a } + return b - a } +// Set is a generic and thread-safe +// set container type Set struct { items map[interface{}]struct{} lock *sync.RWMutex } +// NewSet returns a new set func NewSet() *Set { return &Set{lock: &sync.RWMutex{}, items: make(map[interface{}]struct{})} } +// Add adds given item to the set func (s *Set) Add(item interface{}) { s.lock.Lock() defer s.lock.Unlock() s.items[item] = struct{}{} } +// Exists returns true whether given item is in the set func (s *Set) Exists(item interface{}) bool { s.lock.RLock() defer s.lock.RUnlock() @@ -97,6 +106,8 @@ func (s *Set) Exists(item interface{}) bool { return exists } +// ToArray returns a slice with items +// at the point in time the method was invoked func (s *Set) ToArray() []interface{} { s.lock.RLock() defer s.lock.RUnlock() @@ -109,12 +120,14 @@ func (s *Set) ToArray() []interface{} { return a } +// Clear removes all elements from set func (s *Set) Clear() { s.lock.Lock() defer s.lock.Unlock() s.items = make(map[interface{}]struct{}) } +// Remove removes a given item from the set func (s *Set) Remove(item interface{}) { s.lock.Lock() defer s.lock.Unlock() @@ -126,6 +139,8 @@ type goroutine struct { Stack []string } +// PrintStackTrace prints to stdout +// all goroutines func PrintStackTrace() { buf := make([]byte, 1<<16) runtime.Stack(buf, true)