Skip to content

Commit

Permalink
Merge pull request #44 from ipfs/fix/43
Browse files Browse the repository at this point in the history
protect loggers with rwmutex
  • Loading branch information
Stebalien committed Aug 23, 2018
2 parents c7aaf1b + 0e610d2 commit 3e24213
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions oldlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"os"
"sync"

tracer "github.com/ipfs/go-log/tracer"

Expand Down Expand Up @@ -38,6 +39,7 @@ const (
var ErrNoSuchLogger = errors.New("Error: No such logger")

// loggers is the set of loggers in the system
var loggerMutex sync.RWMutex
var loggers = map[string]*logging.Logger{}

// SetupLogging will initialize the logger backend and set the flags.
Expand Down Expand Up @@ -79,6 +81,10 @@ func SetDebugLogging() {
// SetAllLoggers changes the logging.Level of all loggers to lvl
func SetAllLoggers(lvl logging.Level) {
logging.SetLevel(lvl, "")

loggerMutex.RLock()
defer loggerMutex.RUnlock()

for n := range loggers {
logging.SetLevel(lvl, n)
}
Expand All @@ -98,6 +104,9 @@ func SetLogLevel(name, level string) error {
return nil
}

loggerMutex.RLock()
defer loggerMutex.RUnlock()

// Check if we have a logger by that name
if _, ok := loggers[name]; !ok {
return ErrNoSuchLogger
Expand All @@ -111,6 +120,8 @@ func SetLogLevel(name, level string) error {
// GetSubsystems returns a slice containing the
// names of the current loggers
func GetSubsystems() []string {
loggerMutex.RLock()
defer loggerMutex.RUnlock()
subs := make([]string, 0, len(loggers))

for k := range loggers {
Expand All @@ -120,7 +131,14 @@ func GetSubsystems() []string {
}

func getLogger(name string) *logging.Logger {
log := logging.MustGetLogger(name)
loggers[name] = log
loggerMutex.Lock()
defer loggerMutex.Unlock()

log := loggers[name]
if log == nil {
log = logging.MustGetLogger(name)
loggers[name] = log
}

return log
}

0 comments on commit 3e24213

Please sign in to comment.