pull/1/head
兔子 4 years ago
parent a5e148d6b8
commit ecba5e76ff

@ -13,6 +13,8 @@ import (
"sync" "sync"
"time" "time"
"b612.me/starmap"
"github.com/mattn/go-colorable" "github.com/mattn/go-colorable"
) )
@ -48,6 +50,8 @@ var (
LvPanic: []Attr{FgRed, Bold}, LvPanic: []Attr{FgRed, Bold},
LvFatal: []Attr{FgRed}, LvFatal: []Attr{FgRed},
} }
stdLock sync.Mutex
logmaps sync.Map
) )
type StarLogger struct { type StarLogger struct {
@ -63,14 +67,21 @@ type StarLogger struct {
DoShow bool DoShow bool
switching bool switching bool
isStd bool isStd bool
//HandleFunc //handleFunc
waiting chan int stopChan chan int
setChan chan int
outshow io.Writer outshow io.Writer
LogLevel int LogLevel int
HandleFunc func([]Attr, string) handleFunc func([]Attr, string)
StdFuncColor bool StdFuncColor bool
logbuf string logbuf string
BufSize int BufSize int
stacks starmap.StarStack
}
type logTran struct {
color []Attr
log string
} }
var Std = New(nil) var Std = New(nil)
@ -80,7 +91,9 @@ func init() {
Std.DoShow = true Std.DoShow = true
Std.Flag = "MAN" Std.Flag = "MAN"
Std.isStd = true Std.isStd = true
logmaps.Store("std", Std)
} }
func Close() error { func Close() error {
if StdFile == nil { if StdFile == nil {
return errors.New("File not Open") return errors.New("File not Open")
@ -107,9 +120,23 @@ func New(out io.Writer) *StarLogger {
logger.StdFuncColor = true logger.StdFuncColor = true
logger.BufSize = 0 logger.BufSize = 0
logger.Flag = "MAN" logger.Flag = "MAN"
logger.stopChan, logger.setChan = make(chan int), make(chan int)
go logger.runTransfer()
logmaps.Store(fmt.Sprint(time.Now().UnixNano()), logger)
return logger return logger
} }
func (logger *StarLogger) SetHandleFunc(funcs func([]Attr, string)) {
if logger.handleFunc == nil {
logger.setChan <- 1
}
logger.handleFunc = funcs
}
func (logger *StarLogger) GetHandleFunc() func([]Attr, string) {
return logger.handleFunc
}
func (logger *StarLogger) Flush() error { func (logger *StarLogger) Flush() error {
logger.mu.Lock() logger.mu.Lock()
defer logger.mu.Unlock() defer logger.mu.Unlock()
@ -118,6 +145,46 @@ func (logger *StarLogger) Flush() error {
return nil return nil
} }
func (logger *StarLogger) Close() error {
logger.stopChan <- 1
logmaps.Range(func(k, v interface{}) bool {
if v.(*StarLogger) == logger {
logmaps.Delete(k)
return false
}
return true
})
logger = nil
return nil
}
func (logger *StarLogger) runTransfer() {
if logger.handleFunc == nil {
select {
case <-logger.stopChan:
return
case <-logger.setChan:
}
}
for {
select {
case <-logger.stopChan:
return
default:
}
if logger.handleFunc == nil {
time.Sleep(time.Millisecond * 100)
continue
}
poped := logger.stacks.MustPop()
if poped == nil {
time.Sleep(time.Millisecond * 20)
continue
}
val := poped.(logTran)
logger.handleFunc(val.color, val.log)
}
}
func NewFlag() *StarLogger { func NewFlag() *StarLogger {
return Std.NewFlag() return Std.NewFlag()
} }
@ -135,9 +202,13 @@ func (logger *StarLogger) NewFlag() *StarLogger {
logger2.mu = logger.mu logger2.mu = logger.mu
logger2.lock = logger.lock logger2.lock = logger.lock
logger2.Flag = string([]byte{uint8(rand.Intn(26) + 65), uint8(rand.Intn(26) + 65), uint8(rand.Intn(26) + 65)}) logger2.Flag = string([]byte{uint8(rand.Intn(26) + 65), uint8(rand.Intn(26) + 65), uint8(rand.Intn(26) + 65)})
logger2.stopChan, logger2.setChan = make(chan int), make(chan int)
logger2.handleFunc = logger.handleFunc
go logger2.runTransfer()
if logger2.Flag == "MAN" { if logger2.Flag == "MAN" {
logger2.Flag = "RYZ" logger2.Flag = "RYZ"
} }
logmaps.Store(fmt.Sprint(time.Now().UnixNano()), logger2)
return logger2 return logger2
} }
@ -202,8 +273,9 @@ func (logger *StarLogger) output(level int, logstr string) {
logcolor := NewColor(Colors[level]...) logcolor := NewColor(Colors[level]...)
logcolor.Fprint(logger.outshow, logs) logcolor.Fprint(logger.outshow, logs)
} }
if logger.HandleFunc != nil { if logger.handleFunc != nil {
go logger.HandleFunc(Colors[level], logs) //logger.handleFunc(Colors[level], logs)
logger.stacks.Push(logTran{Colors[level], logs})
} }
if logger.DoWrite { if logger.DoWrite {
if logger.BufSize == 0 { if logger.BufSize == 0 {
@ -223,6 +295,8 @@ func (logger *StarLogger) Debug(str ...interface{}) {
} }
func Debug(str ...interface{}) { func Debug(str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true Std.isStd = true
Std.Debug(str...) Std.Debug(str...)
} }
@ -233,6 +307,8 @@ func (logger *StarLogger) Debugf(format string, str ...interface{}) {
} }
func Debugf(format string, str ...interface{}) { func Debugf(format string, str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true Std.isStd = true
Std.Debugf(format, str...) Std.Debugf(format, str...)
} }
@ -243,6 +319,8 @@ func (logger *StarLogger) Debugln(str ...interface{}) {
} }
func Debugln(str ...interface{}) { func Debugln(str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true Std.isStd = true
Std.Debugln(str...) Std.Debugln(str...)
} }
@ -253,6 +331,8 @@ func (logger *StarLogger) Info(str ...interface{}) {
} }
func Info(str ...interface{}) { func Info(str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true Std.isStd = true
Std.Info(str...) Std.Info(str...)
} }
@ -263,6 +343,8 @@ func (logger *StarLogger) Infof(format string, str ...interface{}) {
} }
func Infof(format string, str ...interface{}) { func Infof(format string, str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true Std.isStd = true
Std.Infof(format, str...) Std.Infof(format, str...)
} }
@ -273,6 +355,8 @@ func (logger *StarLogger) Infoln(str ...interface{}) {
} }
func Infoln(str ...interface{}) { func Infoln(str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true Std.isStd = true
Std.Infoln(str...) Std.Infoln(str...)
} }
@ -283,6 +367,8 @@ func (logger *StarLogger) Notice(str ...interface{}) {
} }
func Notice(str ...interface{}) { func Notice(str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true Std.isStd = true
Std.Notice(str...) Std.Notice(str...)
} }
@ -293,6 +379,8 @@ func (logger *StarLogger) Noticef(format string, str ...interface{}) {
} }
func Noticef(format string, str ...interface{}) { func Noticef(format string, str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true Std.isStd = true
Std.Noticef(format, str...) Std.Noticef(format, str...)
} }
@ -303,6 +391,8 @@ func (logger *StarLogger) Noticeln(str ...interface{}) {
} }
func Noticeln(str ...interface{}) { func Noticeln(str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true Std.isStd = true
Std.Noticeln(str...) Std.Noticeln(str...)
} }
@ -313,6 +403,8 @@ func (logger *StarLogger) Warning(str ...interface{}) {
} }
func Warning(str ...interface{}) { func Warning(str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true Std.isStd = true
Std.Warning(str...) Std.Warning(str...)
} }
@ -323,6 +415,8 @@ func (logger *StarLogger) Warningf(format string, str ...interface{}) {
} }
func Warningf(format string, str ...interface{}) { func Warningf(format string, str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true Std.isStd = true
Std.Warningf(format, str...) Std.Warningf(format, str...)
} }
@ -333,6 +427,8 @@ func (logger *StarLogger) Warningln(str ...interface{}) {
} }
func Warningln(str ...interface{}) { func Warningln(str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true Std.isStd = true
Std.Warningln(str...) Std.Warningln(str...)
} }
@ -343,6 +439,8 @@ func (logger *StarLogger) Error(str ...interface{}) {
} }
func Error(str ...interface{}) { func Error(str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true Std.isStd = true
Std.Error(str...) Std.Error(str...)
} }
@ -353,6 +451,8 @@ func (logger *StarLogger) Errorf(format string, str ...interface{}) {
} }
func Errorf(format string, str ...interface{}) { func Errorf(format string, str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true Std.isStd = true
Std.Errorf(format, str...) Std.Errorf(format, str...)
} }
@ -363,6 +463,8 @@ func (logger *StarLogger) Errorln(str ...interface{}) {
} }
func Errorln(str ...interface{}) { func Errorln(str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true Std.isStd = true
Std.Errorln(str...) Std.Errorln(str...)
} }
@ -373,6 +475,8 @@ func (logger *StarLogger) Critical(str ...interface{}) {
} }
func Critical(str ...interface{}) { func Critical(str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true Std.isStd = true
Std.Critical(str...) Std.Critical(str...)
} }
@ -383,6 +487,8 @@ func (logger *StarLogger) Criticalf(format string, str ...interface{}) {
} }
func Criticalf(format string, str ...interface{}) { func Criticalf(format string, str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true Std.isStd = true
Std.Criticalf(format, str...) Std.Criticalf(format, str...)
} }
@ -393,6 +499,8 @@ func (logger *StarLogger) Criticalln(str ...interface{}) {
} }
func Criticalln(str ...interface{}) { func Criticalln(str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true Std.isStd = true
Std.Criticalln(str...) Std.Criticalln(str...)
} }
@ -404,6 +512,8 @@ func (logger *StarLogger) Fatal(str ...interface{}) {
} }
func Fatal(str ...interface{}) { func Fatal(str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true Std.isStd = true
Std.Fatal(str...) Std.Fatal(str...)
} }
@ -415,6 +525,8 @@ func (logger *StarLogger) Fatalf(format string, str ...interface{}) {
} }
func Fatalf(format string, str ...interface{}) { func Fatalf(format string, str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true Std.isStd = true
Std.Fatalf(format, str...) Std.Fatalf(format, str...)
} }
@ -426,6 +538,8 @@ func (logger *StarLogger) Fatalln(str ...interface{}) {
} }
func Fatalln(str ...interface{}) { func Fatalln(str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true Std.isStd = true
Std.Fatalln(str...) Std.Fatalln(str...)
} }
@ -437,6 +551,8 @@ func (logger *StarLogger) Panic(str ...interface{}) {
} }
func Panic(str ...interface{}) { func Panic(str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true Std.isStd = true
Std.Panic(str...) Std.Panic(str...)
} }
@ -448,6 +564,8 @@ func (logger *StarLogger) Panicf(format string, str ...interface{}) {
} }
func Panicf(format string, str ...interface{}) { func Panicf(format string, str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true Std.isStd = true
Std.Panicf(format, str...) Std.Panicf(format, str...)
} }
@ -459,6 +577,8 @@ func (logger *StarLogger) Panicln(str ...interface{}) {
} }
func Panicln(str ...interface{}) { func Panicln(str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true Std.isStd = true
Std.Panicln(str...) Std.Panicln(str...)
} }
@ -471,6 +591,8 @@ func (logger *StarLogger) Print(str ...interface{}) {
} }
func Print(str ...interface{}) { func Print(str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true Std.isStd = true
Std.Print(str...) Std.Print(str...)
} }
@ -483,6 +605,8 @@ func (logger *StarLogger) Printf(format string, str ...interface{}) {
} }
func Printf(format string, str ...interface{}) { func Printf(format string, str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true Std.isStd = true
Std.Printf(format, str...) Std.Printf(format, str...)
} }
@ -495,6 +619,8 @@ func (logger *StarLogger) Println(str ...interface{}) {
} }
func Println(str ...interface{}) { func Println(str ...interface{}) {
stdLock.Lock()
defer stdLock.Unlock()
Std.isStd = true Std.isStd = true
Std.Println(str...) Std.Println(str...)
} }
@ -504,8 +630,8 @@ func StdPrint(c []Attr, str ...interface{}) {
defer Std.lock.Unlock() defer Std.lock.Unlock()
colorstr := NewColor(c...) colorstr := NewColor(c...)
colorstr.Print(str...) colorstr.Print(str...)
if Std.StdFuncColor && Std.HandleFunc != nil { if Std.StdFuncColor && Std.handleFunc != nil {
go Std.HandleFunc(c, fmt.Sprint(str...)) go Std.handleFunc(c, fmt.Sprint(str...))
} }
} }
@ -514,8 +640,8 @@ func StdPrintf(c []Attr, format string, str ...interface{}) {
defer Std.lock.Unlock() defer Std.lock.Unlock()
colorstr := NewColor(c...) colorstr := NewColor(c...)
colorstr.Printf(format, str...) colorstr.Printf(format, str...)
if Std.StdFuncColor && Std.HandleFunc != nil { if Std.StdFuncColor && Std.handleFunc != nil {
go Std.HandleFunc(c, fmt.Sprintf(format, str...)) go Std.handleFunc(c, fmt.Sprintf(format, str...))
} }
} }
@ -524,8 +650,8 @@ func StdPrintln(c []Attr, str ...interface{}) {
defer Std.lock.Unlock() defer Std.lock.Unlock()
colorstr := NewColor(c...) colorstr := NewColor(c...)
colorstr.Println(str...) colorstr.Println(str...)
if Std.StdFuncColor && Std.HandleFunc != nil { if Std.StdFuncColor && Std.handleFunc != nil {
go Std.HandleFunc(c, fmt.Sprintln(str...)) go Std.handleFunc(c, fmt.Sprintln(str...))
} }
} }
@ -537,6 +663,33 @@ func (logger *StarLogger) SwitchOut(out io.Writer) {
logger.switching = false logger.switching = false
} }
func (logger *StarLogger) SetSwitching(out bool) {
logger.lock.Lock()
defer logger.lock.Unlock()
logger.switching = out
}
func SetSwitchingAll(out bool) {
logmaps.Range(func(k, v interface{}) bool {
v.(*StarLogger).SetSwitching(out)
return true
})
}
func SwitchOutAll(out io.Writer) {
logmaps.Range(func(k, v interface{}) bool {
v.(*StarLogger).SwitchOut(out)
return true
})
}
func SethandleFuncAll(fn func([]Attr, string)) {
logmaps.Range(func(k, v interface{}) bool {
v.(*StarLogger).handleFunc = fn
return true
})
}
func SetLogFile(path string) error { func SetLogFile(path string) error {
var err error var err error
StdFile, err = os.Create(path) StdFile, err = os.Create(path)

@ -5,5 +5,4 @@ import (
) )
func Test_LOG(t *testing.T) { func Test_LOG(t *testing.T) {
} }

Loading…
Cancel
Save