Skip to content

Commit 549312c

Browse files
author
Yusuke Kato
authored
fix (#10)
1 parent e73e5d2 commit 549312c

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

glg.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
type Glg struct {
2121
logger sync.Map // map[uint8]*logger
2222
timer *atomic.Value // []byte
23+
mu sync.Mutex
2324
levelCounter *uint32
2425
levelMap sync.Map
2526
buffer sync.Pool
@@ -687,21 +688,31 @@ func (g *Glg) out(level LEVEL, format string, val ...interface{}) error {
687688

688689
switch log.writeMode {
689690
case writeColorStd:
691+
g.mu.Lock()
690692
buf = append(append(append(append(append(buf[:0], g.timer.Load().([]byte)...), "\t["...), log.tag...), "]:\t"...), format...)
693+
g.mu.Unlock()
691694
_, err = fmt.Fprintf(log.std, log.color(*(*string)(unsafe.Pointer(&buf)))+"\n", val...)
692695
case writeStd:
696+
g.mu.Lock()
693697
buf = append(append(append(append(append(append(buf[:0], g.timer.Load().([]byte)...), "\t["...), log.tag...), "]:\t"...), format...), "\n"...)
698+
g.mu.Unlock()
694699
_, err = fmt.Fprintf(log.std, *(*string)(unsafe.Pointer(&buf)), val...)
695700
case writeWriter:
701+
g.mu.Lock()
696702
buf = append(append(append(append(append(append(buf[:0], g.timer.Load().([]byte)...), "\t["...), log.tag...), "]:\t"...), format...), "\n"...)
703+
g.mu.Unlock()
697704
_, err = fmt.Fprintf(log.writer, *(*string)(unsafe.Pointer(&buf)), val...)
698705
case writeColorBoth:
706+
g.mu.Lock()
699707
buf = append(append(append(append(append(buf[:0], g.timer.Load().([]byte)...), "\t["...), log.tag...), "]:\t"...), format...)
708+
g.mu.Unlock()
700709
var str = *(*string)(unsafe.Pointer(&buf))
701710
_, err = fmt.Fprintf(log.std, log.color(str)+"\n", val...)
702711
_, err = fmt.Fprintf(log.writer, str+"\n", val...)
703712
case writeBoth:
713+
g.mu.Lock()
704714
buf = append(append(append(append(append(append(buf[:0], g.timer.Load().([]byte)...), "\t["...), log.tag...), "]:\t"...), format...), "\n"...)
715+
g.mu.Unlock()
705716
_, err = fmt.Fprintf(io.MultiWriter(log.std, log.writer), *(*string)(unsafe.Pointer(&buf)), val...)
706717
}
707718
g.buffer.Put(buf[:0])

sample/sample.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import (
4+
"sync"
5+
6+
"github.com/kpango/glg"
7+
)
8+
9+
func main() {
10+
var wg sync.WaitGroup
11+
12+
wg.Add(1)
13+
go func() {
14+
defer wg.Done()
15+
glg.Info("test1")
16+
}()
17+
wg.Add(1)
18+
go func() {
19+
defer wg.Done()
20+
glg.Info("test2")
21+
}()
22+
23+
wg.Wait()
24+
}

0 commit comments

Comments
 (0)