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