Skip to content

Commit

Permalink
Fix data races
Browse files Browse the repository at this point in the history
  • Loading branch information
typeless committed Mar 30, 2017
1 parent 912b340 commit bce07fd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ install: $(wildcard *.go)
build: $(EXECUTABLE)

$(EXECUTABLE): $(SOURCES)
go build -i -v -tags '$(TAGS)' -ldflags '-s -w $(LDFLAGS)' -o $@
go build -race -i -v -tags '$(TAGS)' -ldflags '-s -w $(LDFLAGS)' -o $@

.PHONY: docker
docker:
Expand Down
8 changes: 7 additions & 1 deletion modules/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,13 @@ func (l *Logger) SetLogger(adapter string, config string) error {
// DelLogger removes a logger adapter instance.
func (l *Logger) DelLogger(adapter string) error {
l.lock.Lock()
defer l.lock.Unlock()
if lg, ok := l.outputs[adapter]; ok {
lg.Destroy()
delete(l.outputs, adapter)
} else {
panic("log: unknown adapter \"" + adapter + "\" (forgotten register?)")
}
l.lock.Unlock()
return nil
}

Expand Down Expand Up @@ -264,11 +264,13 @@ func (l *Logger) StartLogger() {
for {
select {
case bm := <-l.msg:
l.lock.Lock()
for _, l := range l.outputs {
if err := l.WriteMsg(bm.msg, bm.skip, bm.level); err != nil {
fmt.Println("ERROR, unable to WriteMsg:", err)
}
}
l.lock.Unlock()
case <-l.quit:
return
}
Expand All @@ -277,13 +279,16 @@ func (l *Logger) StartLogger() {

// Flush flushes all chan data.
func (l *Logger) Flush() {
l.lock.Lock()
for _, l := range l.outputs {
l.Flush()
}
l.lock.Unlock()
}

// Close closes logger, flush all chan data and destroy all adapter instances.
func (l *Logger) Close() {
l.lock.Lock()
l.quit <- true
for {
if len(l.msg) > 0 {
Expand All @@ -297,6 +302,7 @@ func (l *Logger) Close() {
break
}
}
l.lock.Unlock()
for _, l := range l.outputs {
l.Flush()
l.Destroy()
Expand Down

0 comments on commit bce07fd

Please sign in to comment.