Skip to content

Commit

Permalink
[quickfix] Small race condition in logger code. Quickly fixed by addi…
Browse files Browse the repository at this point in the history
…ng mutex.
  • Loading branch information
gdey committed Jul 29, 2016
1 parent 1f57f6a commit ebeb2b9
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions server/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"bytes"
"log"
"os"
"sync"
"text/template"
"time"
)

type Logger struct {
File *os.File
Format string
File *os.File
Format string
sync.Mutex
template *template.Template
skip bool
}
Expand All @@ -34,13 +36,15 @@ func (l *Logger) initTemplate() {
if l.Format == "" {
l.Format = DefaultLogFormat
}
l.Lock()
// setup our server log template
l.template = template.New("logfile")

if _, err := l.template.Parse(l.Format); err != nil {
log.Printf("Could not parse log template(%v) disabling logging. Error: %v", l.Format, err)
l.skip = true
}
l.Unlock()
}

func (l *Logger) Log(item logItem) {
Expand All @@ -59,7 +63,9 @@ func (l *Logger) Log(item logItem) {
if err := l.template.Execute(lbuf, item); err != nil {
// Don't care about the error.
log.Println("Error writing to log file; disabling logging.", err)
l.Lock()
l.skip = true
l.Unlock()
return
}
b := lbuf.Bytes()
Expand All @@ -70,5 +76,7 @@ func (l *Logger) Log(item logItem) {
}

// Don't care about the error.
l.Lock()
l.File.Write(b)
l.Unlock()
}

0 comments on commit ebeb2b9

Please sign in to comment.