Skip to content

Commit fa6cc46

Browse files
author
Yusuke Kato
authored
fix (#11)
1 parent 549312c commit fa6cc46

File tree

1 file changed

+39
-48
lines changed

1 file changed

+39
-48
lines changed

glg.go

Lines changed: 39 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"path/filepath"
1111
"sync"
1212
"sync/atomic"
13-
"time"
1413
"unsafe"
1514

1615
"github.com/kpango/fastime"
@@ -20,7 +19,6 @@ import (
2019
type Glg struct {
2120
logger sync.Map // map[uint8]*logger
2221
timer *atomic.Value // []byte
23-
mu sync.Mutex
2422
levelCounter *uint32
2523
levelMap sync.Map
2624
buffer sync.Pool
@@ -84,6 +82,8 @@ const (
8482

8583
// Default Format
8684
df = "%v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v "
85+
86+
timeFormat = "2006-01-02 15:04:05"
8787
)
8888

8989
var (
@@ -224,7 +224,8 @@ func New() *Glg {
224224
g.logger.Store(k, v)
225225
}
226226

227-
return g.startTimerD()
227+
// return g.startTimerD()
228+
return g
228229
}
229230

230231
// Get returns singleton glg instance
@@ -235,36 +236,36 @@ func Get() *Glg {
235236
return glg
236237
}
237238

238-
func (g *Glg) startTimerD() *Glg {
239-
g.timer = new(atomic.Value)
240-
timeFormat := "2006-01-02 15:04:05"
241-
242-
g.storeTime(timeFormat)
243-
244-
var ctx context.Context
245-
ctx, g.cancel = context.WithCancel(context.Background())
246-
247-
go func() {
248-
ticker := time.NewTicker(time.Millisecond * 500)
249-
for {
250-
select {
251-
case <-ctx.Done():
252-
ticker.Stop()
253-
return
254-
case <-ticker.C:
255-
g.storeTime(timeFormat)
256-
}
257-
}
258-
}()
259-
260-
return g
261-
}
262-
263-
func (g *Glg) storeTime(format string) {
264-
buf := g.buffer.Get().([]byte)
265-
g.timer.Store(fastime.Now().AppendFormat(buf[:0], format))
266-
g.buffer.Put(buf[:0])
267-
}
239+
// func (g *Glg) startTimerD() *Glg {
240+
// g.timer = new(atomic.Value)
241+
// timeFormat := "2006-01-02 15:04:05"
242+
//
243+
// g.storeTime(timeFormat)
244+
//
245+
// var ctx context.Context
246+
// ctx, g.cancel = context.WithCancel(context.Background())
247+
//
248+
// go func() {
249+
// ticker := time.NewTicker(time.Millisecond * 500)
250+
// for {
251+
// select {
252+
// case <-ctx.Done():
253+
// ticker.Stop()
254+
// return
255+
// case <-ticker.C:
256+
// g.storeTime(timeFormat)
257+
// }
258+
// }
259+
// }()
260+
//
261+
// return g
262+
// }
263+
//
264+
// func (g *Glg) storeTime(format string) {
265+
// buf := g.buffer.Get().([]byte)
266+
// g.timer.Store(fastime.Now().AppendFormat(buf[:0], format))
267+
// g.buffer.Put(buf[:0])
268+
// }
268269

269270
// Stop stops glg timer daemon
270271
func (g *Glg) Stop() *Glg {
@@ -688,31 +689,21 @@ func (g *Glg) out(level LEVEL, format string, val ...interface{}) error {
688689

689690
switch log.writeMode {
690691
case writeColorStd:
691-
g.mu.Lock()
692-
buf = append(append(append(append(append(buf[:0], g.timer.Load().([]byte)...), "\t["...), log.tag...), "]:\t"...), format...)
693-
g.mu.Unlock()
692+
buf = append(append(append(append(fastime.Now().AppendFormat(buf[:0], timeFormat), "\t["...), log.tag...), "]:\t"...), format...)
694693
_, err = fmt.Fprintf(log.std, log.color(*(*string)(unsafe.Pointer(&buf)))+"\n", val...)
695694
case writeStd:
696-
g.mu.Lock()
697-
buf = append(append(append(append(append(append(buf[:0], g.timer.Load().([]byte)...), "\t["...), log.tag...), "]:\t"...), format...), "\n"...)
698-
g.mu.Unlock()
695+
buf = append(append(append(append(append(fastime.Now().AppendFormat(buf[:0], timeFormat), "\t["...), log.tag...), "]:\t"...), format...), "\n"...)
699696
_, err = fmt.Fprintf(log.std, *(*string)(unsafe.Pointer(&buf)), val...)
700697
case writeWriter:
701-
g.mu.Lock()
702-
buf = append(append(append(append(append(append(buf[:0], g.timer.Load().([]byte)...), "\t["...), log.tag...), "]:\t"...), format...), "\n"...)
703-
g.mu.Unlock()
698+
buf = append(append(append(append(append(fastime.Now().AppendFormat(buf[:0], timeFormat), "\t["...), log.tag...), "]:\t"...), format...), "\n"...)
704699
_, err = fmt.Fprintf(log.writer, *(*string)(unsafe.Pointer(&buf)), val...)
705700
case writeColorBoth:
706-
g.mu.Lock()
707-
buf = append(append(append(append(append(buf[:0], g.timer.Load().([]byte)...), "\t["...), log.tag...), "]:\t"...), format...)
708-
g.mu.Unlock()
701+
buf = append(append(append(append(fastime.Now().AppendFormat(buf[:0], timeFormat), "\t["...), log.tag...), "]:\t"...), format...)
709702
var str = *(*string)(unsafe.Pointer(&buf))
710703
_, err = fmt.Fprintf(log.std, log.color(str)+"\n", val...)
711704
_, err = fmt.Fprintf(log.writer, str+"\n", val...)
712705
case writeBoth:
713-
g.mu.Lock()
714-
buf = append(append(append(append(append(append(buf[:0], g.timer.Load().([]byte)...), "\t["...), log.tag...), "]:\t"...), format...), "\n"...)
715-
g.mu.Unlock()
706+
buf = append(append(append(append(append(fastime.Now().AppendFormat(buf[:0], timeFormat), "\t["...), log.tag...), "]:\t"...), format...), "\n"...)
716707
_, err = fmt.Fprintf(io.MultiWriter(log.std, log.writer), *(*string)(unsafe.Pointer(&buf)), val...)
717708
}
718709
g.buffer.Put(buf[:0])

0 commit comments

Comments
 (0)