pull/1/head
兔子 5 years ago
parent 3f68382587
commit 66d9e0c2e9

@ -6,6 +6,7 @@ import (
"path/filepath"
"runtime"
"strconv"
"sync"
"time"
"github.com/fatih/color"
@ -60,11 +61,18 @@ var (
ShowLine, ShowLevel, DoWrite, DoShow, switching bool = true, true, true, true, false
loghandle *os.File = nil
HandleFunc func([]color.Attribute, string)
lock sync.WaitGroup
)
func write(logs string) {
var i int = 0
defer lock.Done()
for switching {
time.Sleep(time.Millisecond * 100)
i++
if i > 20 {
return
}
}
if loghandle == nil {
return
@ -100,6 +108,7 @@ func output(level int, showline, showlv, dowrite, doshow bool, strlog string) {
go HandleFunc(Colors[level], logs)
}
if dowrite {
lock.Add(1)
go write(logs)
}
}
@ -284,10 +293,12 @@ func Panicln(str ...interface{}) {
func SetLogFile(path string) error {
var err error
loghandle, err = os.Create(path)
switching = false
return err
}
func SwitchFile(path string) error {
switching = true
if loghandle != nil {
loghandle.Close()
}
@ -295,6 +306,7 @@ func SwitchFile(path string) error {
}
func CloseLog() {
lock.Wait()
if loghandle != nil {
loghandle.Close()
}

Loading…
Cancel
Save