logger: fix concurrency issue
Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
parent
b4c530653c
commit
10e676923c
@ -7,6 +7,7 @@ import (
|
|||||||
"hash/adler32"
|
"hash/adler32"
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/mitchellh/colorstring"
|
"github.com/mitchellh/colorstring"
|
||||||
@ -21,6 +22,7 @@ var colorize = colorstring.Colorize{
|
|||||||
type Console struct {
|
type Console struct {
|
||||||
Out io.Writer
|
Out io.Writer
|
||||||
maxLength int
|
maxLength int
|
||||||
|
l sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Console) Write(p []byte) (n int, err error) {
|
func (c *Console) Write(p []byte) (n int, err error) {
|
||||||
@ -31,9 +33,12 @@ func (c *Console) Write(p []byte) (n int, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
source := c.parseSource(event)
|
source := c.parseSource(event)
|
||||||
|
|
||||||
|
c.l.Lock()
|
||||||
if len(source) > c.maxLength {
|
if len(source) > c.maxLength {
|
||||||
c.maxLength = len(source)
|
c.maxLength = len(source)
|
||||||
}
|
}
|
||||||
|
c.l.Unlock()
|
||||||
|
|
||||||
return fmt.Fprintln(c.Out,
|
return fmt.Fprintln(c.Out,
|
||||||
colorize.Color(fmt.Sprintf("%s %s %s%s%s",
|
colorize.Color(fmt.Sprintf("%s %s %s%s%s",
|
||||||
|
Reference in New Issue
Block a user