Fix logging on windows

This commit is contained in:
Daniel 2019-04-26 11:36:45 +02:00
parent e50553951f
commit 2da792a08d
4 changed files with 64 additions and 7 deletions

View file

@ -40,14 +40,14 @@ func formatLine(line *logLine, useColor bool) string {
var fLine string var fLine string
if line.line == 0 { if line.line == 0 {
fLine = fmt.Sprintf("%s%s ? %s %03d%s %s", colorStart, line.time.Format("060102 15:04:05.000"), line.level.String(), counter, colorEnd, line.msg) fLine = fmt.Sprintf("%s%s ? %s %s %03d%s %s", colorStart, line.time.Format("060102 15:04:05.000"), rightArrow, line.level.String(), counter, colorEnd, line.msg)
} else { } else {
fLen := len(line.file) fLen := len(line.file)
fPartStart := fLen - 10 fPartStart := fLen - 10
if fPartStart < 0 { if fPartStart < 0 {
fPartStart = 0 fPartStart = 0
} }
fLine = fmt.Sprintf("%s%s %s:%03d %s %03d%s %s", colorStart, line.time.Format("060102 15:04:05.000"), line.file[fPartStart:], line.line, line.level.String(), counter, colorEnd, line.msg) fLine = fmt.Sprintf("%s%s %s:%03d %s %s %03d%s %s", colorStart, line.time.Format("060102 15:04:05.000"), line.file[fPartStart:], line.line, rightArrow, line.level.String(), counter, colorEnd, line.msg)
} }
if counter >= maxCount { if counter >= maxCount {

View file

@ -1,8 +1,9 @@
// Copyright Safing ICS Technologies GmbH. Use of this source code is governed by the AGPL license that can be found in the LICENSE file.
package log package log
type color int const (
rightArrow = "▶"
leftArrow = "◀"
)
const ( const (
// colorBlack = "\033[30m" // colorBlack = "\033[30m"

56
log/formatting_windows.go Normal file
View file

@ -0,0 +1,56 @@
package log
import (
"github.com/Safing/portbase/utils/osdetail"
)
const (
rightArrow = ">"
leftArrow = "<"
)
const (
// colorBlack = "\033[30m"
colorRed = "\033[31m"
// colorGreen = "\033[32m"
colorYellow = "\033[33m"
colorBlue = "\033[34m"
colorMagenta = "\033[35m"
colorCyan = "\033[36m"
// colorWhite = "\033[37m"
)
var (
colorsSupported bool
)
func init() {
colorsSupported = osdetail.EnableColorSupport()
}
func (s severity) color() string {
if colorsSupported {
switch s {
case DebugLevel:
return colorCyan
case InfoLevel:
return colorBlue
case WarningLevel:
return colorYellow
case ErrorLevel:
return colorRed
case CriticalLevel:
return colorMagenta
default:
return ""
}
}
return ""
}
func endColor() string {
if colorsSupported {
return "\033[0m"
}
return ""
}

View file

@ -20,7 +20,7 @@ func writeLine(line *logLine) {
func startWriter() { func startWriter() {
shutdownWaitGroup.Add(1) shutdownWaitGroup.Add(1)
fmt.Println(fmt.Sprintf("%s%s BOF%s", InfoLevel.color(), time.Now().Format("060102 15:04:05.000"), endColor())) fmt.Println(fmt.Sprintf("%s%s %s BOF%s", InfoLevel.color(), time.Now().Format("060102 15:04:05.000"), rightArrow, endColor()))
go writer() go writer()
} }
@ -49,7 +49,7 @@ func writer() {
case line = <-logBuffer: case line = <-logBuffer:
writeLine(line) writeLine(line)
case <-time.After(10 * time.Millisecond): case <-time.After(10 * time.Millisecond):
fmt.Println(fmt.Sprintf("%s%s EOF%s", InfoLevel.color(), time.Now().Format("060102 15:04:05.000"), endColor())) fmt.Println(fmt.Sprintf("%s%s %s EOF%s", InfoLevel.color(), time.Now().Format("060102 15:04:05.000"), leftArrow, endColor()))
return return
} }
} }