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

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

Loading…
Cancel
Save