Skip to content

Commit

Permalink
add a close to log, wait for log's goroutinue end.
Browse files Browse the repository at this point in the history
  • Loading branch information
sydnash committed Nov 24, 2017
1 parent 517563f commit 45de57e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
10 changes: 10 additions & 0 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Msg struct {
type Logger interface {
DoPrintf(level int, levelDesc, format string, a ...interface{})
SetColored(colored bool)
Close()
}

var glogger Logger
Expand Down Expand Up @@ -101,6 +102,15 @@ func Fatal(format string, param ...interface{}) {
do(FATAL_LEVEL, FATAL_LEVEL_DESC, format, param...)
}

func Close() {
if glogger == nil {
return
}
gloggerMut.Lock()
glogger.Close()
gloggerMut.Unlock()
}

//init log with SimpleLogger
func Init(path string, fileLevel, shellLevel, maxLine, bufSize int) Logger {
logger := CreateLogger(path, fileLevel, shellLevel, maxLine, bufSize)
Expand Down
15 changes: 15 additions & 0 deletions log/log_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"log"
"os"
"path"
"runtime"
"sync"
"time"
)

Expand All @@ -20,6 +22,7 @@ type SimpleLogger struct {
shellLevel int
isColored bool
buffer chan *Msg
wg sync.WaitGroup
}

const (
Expand Down Expand Up @@ -116,14 +119,26 @@ func (self *SimpleLogger) createLogFile(dir string) (*os.File, error) {
return file, nil
}

func (self *SimpleLogger) Close() {
for len(self.buffer) > 0 {
runtime.Gosched()
}
close(self.buffer)
self.wg.Wait()
}

func (self *SimpleLogger) run() {
go func() {
self.wg.Add(1)
for {
m, ok := <-self.buffer
if ok {
self.doPrintf(m.level, m.levelDesc, m.fmt, m.param...)
} else {
break
}
}
self.wg.Done()
}()
}

Expand Down
2 changes: 2 additions & 0 deletions lotou.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ func StartWithName(nodeName string, f CloseFunc, customLogger log.Logger, data .
}()

core.Wait()

log.Close()
}

//RawStart start lotou, with no wait
Expand Down

0 comments on commit 45de57e

Please sign in to comment.