Skip to content

Commit

Permalink
fix (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yusuke Kato authored Oct 30, 2018
1 parent e73e5d2 commit 549312c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
11 changes: 11 additions & 0 deletions glg.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
type Glg struct {
logger sync.Map // map[uint8]*logger
timer *atomic.Value // []byte
mu sync.Mutex
levelCounter *uint32
levelMap sync.Map
buffer sync.Pool
Expand Down Expand Up @@ -687,21 +688,31 @@ func (g *Glg) out(level LEVEL, format string, val ...interface{}) error {

switch log.writeMode {
case writeColorStd:
g.mu.Lock()
buf = append(append(append(append(append(buf[:0], g.timer.Load().([]byte)...), "\t["...), log.tag...), "]:\t"...), format...)
g.mu.Unlock()
_, err = fmt.Fprintf(log.std, log.color(*(*string)(unsafe.Pointer(&buf)))+"\n", val...)
case writeStd:
g.mu.Lock()
buf = append(append(append(append(append(append(buf[:0], g.timer.Load().([]byte)...), "\t["...), log.tag...), "]:\t"...), format...), "\n"...)
g.mu.Unlock()
_, err = fmt.Fprintf(log.std, *(*string)(unsafe.Pointer(&buf)), val...)
case writeWriter:
g.mu.Lock()
buf = append(append(append(append(append(append(buf[:0], g.timer.Load().([]byte)...), "\t["...), log.tag...), "]:\t"...), format...), "\n"...)
g.mu.Unlock()
_, err = fmt.Fprintf(log.writer, *(*string)(unsafe.Pointer(&buf)), val...)
case writeColorBoth:
g.mu.Lock()
buf = append(append(append(append(append(buf[:0], g.timer.Load().([]byte)...), "\t["...), log.tag...), "]:\t"...), format...)
g.mu.Unlock()
var str = *(*string)(unsafe.Pointer(&buf))
_, err = fmt.Fprintf(log.std, log.color(str)+"\n", val...)
_, err = fmt.Fprintf(log.writer, str+"\n", val...)
case writeBoth:
g.mu.Lock()
buf = append(append(append(append(append(append(buf[:0], g.timer.Load().([]byte)...), "\t["...), log.tag...), "]:\t"...), format...), "\n"...)
g.mu.Unlock()
_, err = fmt.Fprintf(io.MultiWriter(log.std, log.writer), *(*string)(unsafe.Pointer(&buf)), val...)
}
g.buffer.Put(buf[:0])
Expand Down
24 changes: 24 additions & 0 deletions sample/sample.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"sync"

"github.com/kpango/glg"
)

func main() {
var wg sync.WaitGroup

wg.Add(1)
go func() {
defer wg.Done()
glg.Info("test1")
}()
wg.Add(1)
go func() {
defer wg.Done()
glg.Info("test2")
}()

wg.Wait()
}

0 comments on commit 549312c

Please sign in to comment.