diff --git a/starlog.go b/starlog.go index b438183..7e5e2ae 100644 --- a/starlog.go +++ b/starlog.go @@ -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() }