log update
This commit is contained in:
parent
9fa54452ec
commit
22162048be
81
starlog.go
81
starlog.go
@ -10,8 +10,10 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/fatih/color"
|
||||
"github.com/mattn/go-colorable"
|
||||
)
|
||||
|
||||
// Base attributes
|
||||
const (
|
||||
BLUE = color.FgBlue
|
||||
BLACK = color.FgBlack
|
||||
@ -21,10 +23,11 @@ const (
|
||||
RED = color.FgRed
|
||||
WHITE = color.FgWhite
|
||||
YELLOW = color.FgYellow
|
||||
GREY = color.FgHiYellow
|
||||
GREY = color.FgHiRed
|
||||
BOLD = color.Bold
|
||||
)
|
||||
|
||||
// Log Level
|
||||
const (
|
||||
LvDebug = iota
|
||||
LvInfo
|
||||
@ -36,6 +39,7 @@ const (
|
||||
LvFatal
|
||||
)
|
||||
|
||||
// Log Init Data
|
||||
var (
|
||||
levels = map[int]string{
|
||||
LvDebug: "DEBUG",
|
||||
@ -62,10 +66,15 @@ var (
|
||||
loghandle *os.File = nil
|
||||
HandleFunc func([]color.Attribute, string)
|
||||
lock sync.WaitGroup
|
||||
waiting = make(chan int, 1)
|
||||
)
|
||||
|
||||
// Output 使用此进行windows支持
|
||||
var Output = colorable.NewColorableStdout()
|
||||
|
||||
func write(logs string) {
|
||||
var i int = 0
|
||||
var i int
|
||||
lock.Add(1)
|
||||
defer lock.Done()
|
||||
for switching {
|
||||
time.Sleep(time.Millisecond * 100)
|
||||
@ -82,6 +91,10 @@ func write(logs string) {
|
||||
|
||||
func output(level int, showline, showlv, dowrite, doshow bool, strlog string) {
|
||||
var logs string
|
||||
waiting <- 1
|
||||
defer func() {
|
||||
<-waiting
|
||||
}()
|
||||
if level < LogLevel {
|
||||
return
|
||||
}
|
||||
@ -98,67 +111,59 @@ func output(level int, showline, showlv, dowrite, doshow bool, strlog string) {
|
||||
logs = fmt.Sprintf("%s %s", date, strlog)
|
||||
}
|
||||
if doshow {
|
||||
for _, v := range Colors[level] {
|
||||
color.Set(v)
|
||||
}
|
||||
fmt.Print(logs)
|
||||
color.Set(color.Reset)
|
||||
logcolor := color.New(Colors[level]...)
|
||||
logstr := logcolor.Sprint(logs)
|
||||
fmt.Fprint(Output, logstr)
|
||||
}
|
||||
if HandleFunc != nil {
|
||||
go HandleFunc(Colors[level], logs)
|
||||
}
|
||||
if dowrite {
|
||||
lock.Add(1)
|
||||
write(logs)
|
||||
}
|
||||
}
|
||||
|
||||
// StdPrint 进行stdout标准输出
|
||||
func StdPrint(c1, c2 color.Attribute, str ...interface{}) {
|
||||
color.Set(c1)
|
||||
color.Set(c2)
|
||||
fmt.Print(str...)
|
||||
color.Set(color.Reset)
|
||||
waiting <- 1
|
||||
defer func() {
|
||||
<-waiting
|
||||
}()
|
||||
colorstr := color.New(c1, c2)
|
||||
colorstr.Fprint(Output, str...)
|
||||
}
|
||||
|
||||
func StdPrintf(c1, c2 color.Attribute, format string, str ...interface{}) {
|
||||
color.Set(c1)
|
||||
color.Set(c2)
|
||||
fmt.Printf(format, str...)
|
||||
color.Set(color.Reset)
|
||||
waiting <- 1
|
||||
defer func() {
|
||||
<-waiting
|
||||
}()
|
||||
colorstr := color.New(c1, c2)
|
||||
colorstr.Fprintf(Output, format, str...)
|
||||
}
|
||||
|
||||
func StdPrintln(c1, c2 color.Attribute, str ...interface{}) {
|
||||
color.Set(c1)
|
||||
color.Set(c2)
|
||||
fmt.Println(str...)
|
||||
color.Set(color.Reset)
|
||||
waiting <- 1
|
||||
defer func() {
|
||||
<-waiting
|
||||
}()
|
||||
colorstr := color.New(c1, c2)
|
||||
colorstr.Fprintln(Output, str...)
|
||||
}
|
||||
|
||||
func Print(c1, c2 color.Attribute, str ...interface{}) {
|
||||
color.Set(c1)
|
||||
color.Set(c2)
|
||||
strs := fmt.Sprint(str...)
|
||||
fmt.Print(strs)
|
||||
color.Set(color.Reset)
|
||||
write(strs)
|
||||
StdPrint(c1, c2, str...)
|
||||
write(fmt.Sprint(str...))
|
||||
}
|
||||
|
||||
func Printf(c1, c2 color.Attribute, format string, str ...interface{}) {
|
||||
color.Set(c1)
|
||||
color.Set(c2)
|
||||
strs := fmt.Sprintf(format, str...)
|
||||
fmt.Print(strs)
|
||||
color.Set(color.Reset)
|
||||
write(strs)
|
||||
StdPrintf(c1, c2, format, str...)
|
||||
write(fmt.Sprintf(format, str...))
|
||||
}
|
||||
|
||||
func Println(c1, c2 color.Attribute, str ...interface{}) {
|
||||
color.Set(c1)
|
||||
color.Set(c2)
|
||||
strs := fmt.Sprintln(str...)
|
||||
fmt.Print(strs)
|
||||
color.Set(color.Reset)
|
||||
write(strs)
|
||||
StdPrintln(c1, c2, str...)
|
||||
write(fmt.Sprintln(str...))
|
||||
}
|
||||
|
||||
func Debug(str ...interface{}) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user