Skip to content

Commit

Permalink
Merge pull request #54 from hashicorp/f-mutex-copy
Browse files Browse the repository at this point in the history
Fix sharing the mutex between created loggers properly
  • Loading branch information
evanphx authored Dec 18, 2019
2 parents a2d2089 + 72426e5 commit bb2ebcc
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions interceptlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ var _ Logger = &interceptLogger{}
type interceptLogger struct {
Logger

sync.Mutex
mu *sync.Mutex
sinkCount *int32
Sinks map[SinkAdapter]struct{}
}

func NewInterceptLogger(opts *LoggerOptions) InterceptLogger {
intercept := &interceptLogger{
Logger: New(opts),
mu: new(sync.Mutex),
sinkCount: new(int32),
Sinks: make(map[SinkAdapter]struct{}),
}
Expand All @@ -36,8 +37,8 @@ func (i *interceptLogger) Trace(msg string, args ...interface{}) {
return
}

i.Lock()
defer i.Unlock()
i.mu.Lock()
defer i.mu.Unlock()
for s := range i.Sinks {
s.Accept(i.Name(), Trace, msg, i.retrieveImplied(args...)...)
}
Expand All @@ -50,8 +51,8 @@ func (i *interceptLogger) Debug(msg string, args ...interface{}) {
return
}

i.Lock()
defer i.Unlock()
i.mu.Lock()
defer i.mu.Unlock()
for s := range i.Sinks {
s.Accept(i.Name(), Debug, msg, i.retrieveImplied(args...)...)
}
Expand All @@ -64,8 +65,8 @@ func (i *interceptLogger) Info(msg string, args ...interface{}) {
return
}

i.Lock()
defer i.Unlock()
i.mu.Lock()
defer i.mu.Unlock()
for s := range i.Sinks {
s.Accept(i.Name(), Info, msg, i.retrieveImplied(args...)...)
}
Expand All @@ -78,8 +79,8 @@ func (i *interceptLogger) Warn(msg string, args ...interface{}) {
return
}

i.Lock()
defer i.Unlock()
i.mu.Lock()
defer i.mu.Unlock()
for s := range i.Sinks {
s.Accept(i.Name(), Warn, msg, i.retrieveImplied(args...)...)
}
Expand All @@ -92,8 +93,8 @@ func (i *interceptLogger) Error(msg string, args ...interface{}) {
return
}

i.Lock()
defer i.Unlock()
i.mu.Lock()
defer i.mu.Unlock()
for s := range i.Sinks {
s.Accept(i.Name(), Error, msg, i.retrieveImplied(args...)...)
}
Expand Down Expand Up @@ -178,8 +179,8 @@ func (i *interceptLogger) With(args ...interface{}) Logger {

// RegisterSink attaches a SinkAdapter to interceptLoggers sinks.
func (i *interceptLogger) RegisterSink(sink SinkAdapter) {
i.Lock()
defer i.Unlock()
i.mu.Lock()
defer i.mu.Unlock()

i.Sinks[sink] = struct{}{}

Expand All @@ -188,8 +189,8 @@ func (i *interceptLogger) RegisterSink(sink SinkAdapter) {

// DeregisterSink removes a SinkAdapter from interceptLoggers sinks.
func (i *interceptLogger) DeregisterSink(sink SinkAdapter) {
i.Lock()
defer i.Unlock()
i.mu.Lock()
defer i.mu.Unlock()

delete(i.Sinks, sink)

Expand Down

0 comments on commit bb2ebcc

Please sign in to comment.