2021-02-19 16:12:14 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"log"
|
2021-03-23 21:57:53 +00:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
2021-02-19 16:12:14 +00:00
|
|
|
|
|
|
|
c "github.com/fatih/color"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Logger interface {
|
|
|
|
Printf(format string, v ...interface{})
|
|
|
|
Print(v ...interface{})
|
|
|
|
Println(v ...interface{})
|
|
|
|
Fatal(v ...interface{})
|
|
|
|
Fatalf(format string, v ...interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type logger struct {
|
2021-03-23 21:57:53 +00:00
|
|
|
logger *log.Logger
|
|
|
|
shortfile bool
|
|
|
|
printer *c.Color
|
|
|
|
}
|
|
|
|
|
|
|
|
func Lshortfile() string {
|
|
|
|
_, file, line, ok := runtime.Caller(2)
|
|
|
|
lineString := strconv.Itoa(line)
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if file == "" {
|
|
|
|
return lineString
|
|
|
|
}
|
|
|
|
for i := len(file) - 1; i > 0; i-- {
|
|
|
|
if file[i] == '/' || file[i] == '\\' {
|
|
|
|
file = file[i+1:]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return file + ":" + lineString + ":"
|
2021-02-19 16:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewLogger(out io.Writer, prefix string, flag int, color c.Attribute) (l logger) {
|
2021-03-23 21:57:53 +00:00
|
|
|
// Use reimplemented Lshortfile since wrapping the log functions messes them up
|
|
|
|
if flag&log.Lshortfile != 0 {
|
|
|
|
flag -= log.Lshortfile
|
|
|
|
l.shortfile = true
|
|
|
|
}
|
|
|
|
|
2021-02-19 16:12:14 +00:00
|
|
|
l.logger = log.New(out, prefix, flag)
|
|
|
|
l.printer = c.New(color)
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l logger) Printf(format string, v ...interface{}) {
|
2021-03-23 21:57:53 +00:00
|
|
|
var out string
|
|
|
|
if l.shortfile {
|
|
|
|
out = Lshortfile()
|
|
|
|
}
|
|
|
|
out += " " + l.printer.Sprintf(format, v...)
|
|
|
|
l.logger.Print(out)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l logger) Print(v ...interface{}) {
|
|
|
|
var out string
|
|
|
|
if l.shortfile {
|
|
|
|
out = Lshortfile()
|
|
|
|
}
|
|
|
|
out += " " + l.printer.Sprint(v...)
|
|
|
|
l.logger.Print(out)
|
2021-02-19 16:12:14 +00:00
|
|
|
}
|
2021-03-23 21:57:53 +00:00
|
|
|
|
|
|
|
func (l logger) Println(v ...interface{}) {
|
|
|
|
var out string
|
|
|
|
if l.shortfile {
|
|
|
|
out = Lshortfile()
|
|
|
|
}
|
|
|
|
out += " " + l.printer.Sprintln(v...)
|
|
|
|
l.logger.Print(out)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l logger) Fatal(v ...interface{}) {
|
|
|
|
var out string
|
|
|
|
if l.shortfile {
|
|
|
|
out = Lshortfile()
|
|
|
|
}
|
|
|
|
out += " " + l.printer.Sprint(v...)
|
|
|
|
l.logger.Fatal(out)
|
|
|
|
}
|
|
|
|
|
2021-02-19 16:12:14 +00:00
|
|
|
func (l logger) Fatalf(format string, v ...interface{}) {
|
2021-03-23 21:57:53 +00:00
|
|
|
var out string
|
|
|
|
if l.shortfile {
|
|
|
|
out = Lshortfile()
|
|
|
|
}
|
|
|
|
out += " " + l.printer.Sprintf(format, v...)
|
|
|
|
l.logger.Fatal(out)
|
2021-02-19 16:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type emptyLogger bool
|
|
|
|
|
|
|
|
func (l emptyLogger) Printf(format string, v ...interface{}) {}
|
|
|
|
func (l emptyLogger) Print(v ...interface{}) {}
|
|
|
|
func (l emptyLogger) Println(v ...interface{}) {}
|
|
|
|
func (l emptyLogger) Fatal(v ...interface{}) {}
|
|
|
|
func (l emptyLogger) Fatalf(format string, v ...interface{}) {}
|