sing-box/log/format.go

156 lines
3.8 KiB
Go
Raw Permalink Normal View History

2022-07-12 07:17:29 +00:00
package log
import (
"context"
"strconv"
"strings"
"time"
F "github.com/sagernet/sing/common/format"
"github.com/logrusorgru/aurora"
)
type Formatter struct {
BaseTime time.Time
DisableColors bool
DisableTimestamp bool
FullTimestamp bool
TimestampFormat string
}
func (f Formatter) Format(ctx context.Context, level Level, tag string, message string, timestamp time.Time) string {
levelString := strings.ToUpper(FormatLevel(level))
if !f.DisableColors {
switch level {
case LevelDebug, LevelTrace:
levelString = aurora.White(levelString).String()
case LevelInfo:
levelString = aurora.Cyan(levelString).String()
case LevelWarn:
levelString = aurora.Yellow(levelString).String()
case LevelError, LevelFatal, LevelPanic:
levelString = aurora.Red(levelString).String()
}
}
if tag != "" {
message = tag + ": " + message
}
var id uint32
var hasId bool
if ctx != nil {
id, hasId = IDFromContext(ctx)
}
if hasId {
if !f.DisableColors {
var color aurora.Color
color = aurora.Color(uint8(id))
color %= 215
row := uint(color / 36)
column := uint(color % 36)
2022-07-12 07:17:29 +00:00
var r, g, b float32
r = float32(row * 51)
g = float32(column / 6 * 51)
b = float32((column % 6) * 51)
luma := 0.2126*r + 0.7152*g + 0.0722*b
if luma < 60 {
row = 5 - row
column = 35 - column
color = aurora.Color(row*36 + column)
}
color += 16
color = color << 16
color |= 1 << 14
message = F.ToString("[", aurora.Colorize(id, color).String(), "] ", message)
} else {
message = F.ToString("[", id, "] ", message)
2022-07-12 07:17:29 +00:00
}
}
switch {
case f.DisableTimestamp:
message = levelString + " " + message
case f.FullTimestamp:
2022-08-31 15:35:43 +00:00
message = timestamp.Format(f.TimestampFormat) + " " + levelString + " " + message
2022-07-12 07:17:29 +00:00
default:
message = levelString + "[" + xd(int(timestamp.Sub(f.BaseTime)/time.Second), 4) + "] " + message
}
2022-07-22 06:28:29 +00:00
if message[len(message)-1] != '\n' {
message += "\n"
}
return message
2022-07-12 07:17:29 +00:00
}
2022-07-19 14:16:49 +00:00
func (f Formatter) FormatWithSimple(ctx context.Context, level Level, tag string, message string, timestamp time.Time) (string, string) {
levelString := strings.ToUpper(FormatLevel(level))
if !f.DisableColors {
switch level {
case LevelDebug, LevelTrace:
levelString = aurora.White(levelString).String()
case LevelInfo:
levelString = aurora.Cyan(levelString).String()
case LevelWarn:
levelString = aurora.Yellow(levelString).String()
case LevelError, LevelFatal, LevelPanic:
levelString = aurora.Red(levelString).String()
}
}
if tag != "" {
message = tag + ": " + message
}
messageSimple := message
var id uint32
var hasId bool
if ctx != nil {
id, hasId = IDFromContext(ctx)
}
if hasId {
if !f.DisableColors {
var color aurora.Color
color = aurora.Color(uint8(id))
color %= 215
row := uint(color / 36)
column := uint(color % 36)
var r, g, b float32
r = float32(row * 51)
g = float32(column / 6 * 51)
b = float32((column % 6) * 51)
luma := 0.2126*r + 0.7152*g + 0.0722*b
if luma < 60 {
row = 5 - row
column = 35 - column
color = aurora.Color(row*36 + column)
}
color += 16
color = color << 16
color |= 1 << 14
message = F.ToString("[", aurora.Colorize(id, color).String(), "] ", message)
} else {
message = F.ToString("[", id, "] ", message)
}
2022-07-20 01:41:44 +00:00
messageSimple = F.ToString("[", id, "] ", messageSimple)
2022-07-19 14:16:49 +00:00
}
switch {
case f.DisableTimestamp:
message = levelString + " " + message
case f.FullTimestamp:
2022-08-31 15:35:43 +00:00
message = timestamp.Format(f.TimestampFormat) + " " + levelString + " " + message
2022-07-19 14:16:49 +00:00
default:
message = levelString + "[" + xd(int(timestamp.Sub(f.BaseTime)/time.Second), 4) + "] " + message
}
2022-07-22 06:28:29 +00:00
if message[len(message)-1] != '\n' {
message += "\n"
}
return message, messageSimple
2022-07-19 14:16:49 +00:00
}
2022-07-12 07:17:29 +00:00
func xd(value int, x int) string {
message := strconv.Itoa(value)
for len(message) < x {
message = "0" + message
}
return message
}