You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
starlog/starlog.go

314 lines
7.0 KiB
Go

5 years ago
package starlog
import (
"fmt"
"os"
"path/filepath"
5 years ago
"runtime"
5 years ago
"strconv"
5 years ago
"sync"
5 years ago
"time"
5 years ago
"github.com/fatih/color"
)
5 years ago
5 years ago
const (
BLUE = color.FgBlue
BLACK = color.FgBlack
CYAN = color.FgCyan
GREEN = color.FgGreen
MAGENTA = color.FgMagenta
RED = color.FgRed
WHITE = color.FgWhite
YELLOW = color.FgYellow
GREY = color.FgHiYellow
BOLD = color.Bold
)
5 years ago
5 years ago
const (
LvDebug = iota
LvInfo
LvNotice
LvWarning
LvError
LvCritical
LvPanic
LvFatal
)
5 years ago
5 years ago
var (
levels = map[int]string{
LvDebug: "DEBUG",
LvInfo: "INFO",
LvNotice: "NOTICE",
LvWarning: "WARNING",
LvError: "ERROR",
LvCritical: "CRITICAL",
LvPanic: "PANIC",
LvFatal: "FATAL",
5 years ago
}
5 years ago
Colors = map[int][]color.Attribute{
LvDebug: []color.Attribute{WHITE},
LvInfo: []color.Attribute{GREEN},
LvNotice: []color.Attribute{BLUE},
LvWarning: []color.Attribute{YELLOW},
LvError: []color.Attribute{MAGENTA},
LvCritical: []color.Attribute{RED, BOLD},
LvPanic: []color.Attribute{RED, BOLD},
LvFatal: []color.Attribute{RED},
5 years ago
}
5 years ago
LogLevel int = 0
ShowLine, ShowLevel, DoWrite, DoShow, switching bool = true, true, true, true, false
loghandle *os.File = nil
HandleFunc func([]color.Attribute, string)
5 years ago
lock sync.WaitGroup
5 years ago
)
func write(logs string) {
5 years ago
var i int = 0
defer lock.Done()
5 years ago
for switching {
time.Sleep(time.Millisecond * 100)
5 years ago
i++
if i > 20 {
return
}
5 years ago
}
5 years ago
if loghandle == nil {
return
5 years ago
}
5 years ago
loghandle.WriteString(logs)
5 years ago
}
5 years ago
5 years ago
func output(level int, showline, showlv, dowrite, doshow bool, strlog string) {
5 years ago
var logs string
if level < LogLevel {
5 years ago
return
}
5 years ago
_, fname, line, _ := runtime.Caller(2)
fname = filepath.Base(fname)
date := time.Now().Format("2006-01-02 15:04:05 Mon")
if showline && showlv {
logs = fmt.Sprintf("%s %s %s %s", date, fname+":"+strconv.Itoa(line), `[`+levels[level]+`]`, strlog)
} else if showline && !showlv {
logs = fmt.Sprintf("%s %s %s", date, fname+":"+strconv.Itoa(line), strlog)
} else if !showline && showlv {
logs = fmt.Sprintf("%s %s %s", date, `[`+levels[level]+`]`, strlog)
} else {
logs = fmt.Sprintf("%s %s", date, strlog)
5 years ago
}
5 years ago
if doshow {
for _, v := range Colors[level] {
color.Set(v)
}
fmt.Print(logs)
color.Set(color.Reset)
}
if HandleFunc != nil {
go HandleFunc(Colors[level], logs)
5 years ago
}
5 years ago
if dowrite {
5 years ago
lock.Add(1)
5 years ago
go write(logs)
5 years ago
}
5 years ago
}
func StdPrint(c1, c2 color.Attribute, str ...interface{}) {
color.Set(c1)
color.Set(c2)
fmt.Print(str...)
5 years ago
color.Set(color.Reset)
5 years ago
}
5 years ago
5 years ago
func StdPrintf(c1, c2 color.Attribute, format string, str ...interface{}) {
color.Set(c1)
color.Set(c2)
fmt.Printf(format, str...)
color.Set(color.Reset)
5 years ago
}
5 years ago
func StdPrintln(c1, c2 color.Attribute, str ...interface{}) {
color.Set(c1)
color.Set(c2)
fmt.Println(str...)
5 years ago
color.Set(color.Reset)
}
5 years ago
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)
}
5 years ago
5 years ago
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)
}
func Println(c1, c2 color.Attribute, str ...interface{}) {
color.Set(c1)
color.Set(c2)
strs := fmt.Sprintln(str...)
fmt.Print(strs)
5 years ago
color.Set(color.Reset)
5 years ago
write(strs)
}
func Debug(str ...interface{}) {
strs := fmt.Sprint(str...)
5 years ago
output(LvDebug, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
}
func Debugf(format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
5 years ago
output(LvDebug, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
}
func Debugln(str ...interface{}) {
strs := fmt.Sprintln(str...)
5 years ago
output(LvDebug, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
}
func Info(str ...interface{}) {
strs := fmt.Sprint(str...)
5 years ago
output(LvInfo, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
}
func Infof(format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
5 years ago
output(LvInfo, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
}
func Infoln(str ...interface{}) {
strs := fmt.Sprintln(str...)
5 years ago
output(LvInfo, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
}
func Notice(str ...interface{}) {
strs := fmt.Sprint(str...)
5 years ago
output(LvNotice, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
}
func Noticef(format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
5 years ago
output(LvNotice, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
}
func Noticeln(str ...interface{}) {
strs := fmt.Sprintln(str...)
5 years ago
output(LvNotice, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
}
func Warning(str ...interface{}) {
strs := fmt.Sprint(str...)
5 years ago
output(LvWarning, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
}
func Warningf(format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
5 years ago
output(LvWarning, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
}
func Warningln(str ...interface{}) {
strs := fmt.Sprintln(str...)
5 years ago
output(LvWarning, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
}
func Error(str ...interface{}) {
strs := fmt.Sprint(str...)
5 years ago
output(LvError, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
}
5 years ago
func Errorf(format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
5 years ago
output(LvError, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
}
func Errorln(str ...interface{}) {
strs := fmt.Sprintln(str...)
5 years ago
output(LvError, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
}
func Critical(str ...interface{}) {
strs := fmt.Sprint(str...)
5 years ago
output(LvCritical, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
}
func Criticalf(format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
5 years ago
output(LvCritical, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
}
func Criticalln(str ...interface{}) {
strs := fmt.Sprintln(str...)
5 years ago
output(LvCritical, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
}
func Fatal(str ...interface{}) {
strs := fmt.Sprint(str...)
5 years ago
output(LvFatal, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
CloseLog()
os.Exit(9)
}
func Fatalf(format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
5 years ago
output(LvFatal, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
CloseLog()
os.Exit(9)
}
func Fatalln(str ...interface{}) {
strs := fmt.Sprintln(str...)
5 years ago
output(LvFatal, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
CloseLog()
os.Exit(9)
}
func Panic(str ...interface{}) {
strs := fmt.Sprint(str...)
5 years ago
output(LvPanic, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
panic(str)
}
func Panicf(format string, str ...interface{}) {
strs := fmt.Sprintf(format, str...)
5 years ago
output(LvPanic, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
panic(strs)
}
func Panicln(str ...interface{}) {
strs := fmt.Sprintln(str...)
5 years ago
output(LvPanic, ShowLine, ShowLevel, DoWrite, DoShow, strs)
5 years ago
panic(str)
}
func SetLogFile(path string) error {
var err error
loghandle, err = os.Create(path)
5 years ago
switching = false
5 years ago
return err
}
func SwitchFile(path string) error {
5 years ago
switching = true
5 years ago
if loghandle != nil {
loghandle.Close()
}
return SetLogFile(path)
5 years ago
}
5 years ago
func CloseLog() {
5 years ago
lock.Wait()
5 years ago
if loghandle != nil {
loghandle.Close()
5 years ago
}
}