Properly color logs
This commit is contained in:
@@ -4,10 +4,11 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"slices"
|
|
||||||
"github.com/go-gl/gl/v3.3-core/gl"
|
"github.com/go-gl/gl/v3.3-core/gl"
|
||||||
"github.com/go-gl/mathgl/mgl32"
|
"github.com/go-gl/mathgl/mgl32"
|
||||||
"github.com/veandco/go-sdl2/sdl"
|
"github.com/veandco/go-sdl2/sdl"
|
||||||
|
"rhemawavetech.com/rhemawave/internal/colors"
|
||||||
"rhemawavetech.com/rhemawave/internal/db"
|
"rhemawavetech.com/rhemawave/internal/db"
|
||||||
"rhemawavetech.com/rhemawave/internal/fontManager"
|
"rhemawavetech.com/rhemawave/internal/fontManager"
|
||||||
"rhemawavetech.com/rhemawave/internal/logger"
|
"rhemawavetech.com/rhemawave/internal/logger"
|
||||||
@@ -25,6 +26,7 @@ type LogViewerPage struct {
|
|||||||
lineTextureIDs []uint32
|
lineTextureIDs []uint32
|
||||||
lineTextureWidths []int32
|
lineTextureWidths []int32
|
||||||
lineTextureHeights []int32
|
lineTextureHeights []int32
|
||||||
|
lineColors []sdl.Color
|
||||||
|
|
||||||
hintTextureID uint32
|
hintTextureID uint32
|
||||||
hintTextureWidth int32
|
hintTextureWidth int32
|
||||||
@@ -47,21 +49,34 @@ func (p *LogViewerPage) Init(ctx *PageCtx) error {
|
|||||||
return fmt.Errorf("%s", result.Error)
|
return fmt.Errorf("%s", result.Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Format and word-wrap each log entry
|
// // Format and word-wrap each log entry
|
||||||
// for _, entry := range logs {
|
// for _, entry := range logs {
|
||||||
// formatted := fmt.Sprintf("[%s] [%s] %s", entry.Timestamp, entry.Level, entry.Message)
|
// formatted := fmt.Sprintf("[%s] [%s] %s", entry.Timestamp, entry.Level, entry.Message)
|
||||||
// wrapped := wrapText(formatted, 100)
|
// wrapped := wrapText(formatted, 100)
|
||||||
// p.lines = append(p.lines, strings.Split(wrapped, "\n")...)
|
// p.lines = append(p.lines, strings.Split(wrapped, "\n")...)
|
||||||
// }
|
// }
|
||||||
displayLevels := []string{"WARNING", "ERROR", "FATAL"}
|
|
||||||
|
levelColors := map[string]sdl.Color{
|
||||||
|
"DEBUG": colors.SkyBlue,
|
||||||
|
"INFO": colors.White,
|
||||||
|
"WARNING": colors.Yellow,
|
||||||
|
"ERROR": colors.RedOrange,
|
||||||
|
}
|
||||||
|
|
||||||
|
fallbackColor := colors.White
|
||||||
|
|
||||||
|
p.lineColors = make([]sdl.Color, len(p.lines))
|
||||||
|
|
||||||
for _, entry := range logs {
|
for _, entry := range logs {
|
||||||
if slices.Contains(displayLevels, entry.Level) {
|
color, ok := levelColors[entry.Level]
|
||||||
|
if !ok {
|
||||||
|
color = fallbackColor
|
||||||
|
}
|
||||||
logTime := time.UnixMilli(entry.Timestamp)
|
logTime := time.UnixMilli(entry.Timestamp)
|
||||||
timeStr := logTime.Format("2006-01-02 15:04")
|
timeStr := logTime.Format("2006-01-02 15:04")
|
||||||
line := fmt.Sprintf("[%s] [%s] %s", timeStr, entry.Level, entry.Message)
|
line := fmt.Sprintf("[%s] [%s] %s", timeStr, entry.Level, entry.Message)
|
||||||
p.lines = append(p.lines, line)
|
p.lines = append(p.lines, line)
|
||||||
}
|
p.lineColors = append(p.lineColors, color)
|
||||||
}
|
}
|
||||||
|
|
||||||
applicationFont := "Roboto-Regular"
|
applicationFont := "Roboto-Regular"
|
||||||
@@ -167,9 +182,6 @@ func (p *LogViewerPage) Render() error {
|
|||||||
sm := shaderManager.Get()
|
sm := shaderManager.Get()
|
||||||
textShader, _ := sm.Get("text")
|
textShader, _ := sm.Get("text")
|
||||||
|
|
||||||
white := sdl.Color{R: 255, G: 255, B: 255, A: 255}
|
|
||||||
grey := sdl.Color{R: 180, G: 180, B: 180, A: 255}
|
|
||||||
|
|
||||||
titleFontSize := int32(48)
|
titleFontSize := int32(48)
|
||||||
titleScale := float32(titleFontSize) / float32(baseFontSize)
|
titleScale := float32(titleFontSize) / float32(baseFontSize)
|
||||||
contentFontSize := int32(24)
|
contentFontSize := int32(24)
|
||||||
@@ -189,14 +201,14 @@ func (p *LogViewerPage) Render() error {
|
|||||||
titleW := float32(p.titleTextureWidth) * titleScale
|
titleW := float32(p.titleTextureWidth) * titleScale
|
||||||
titleX := p.Base.ScreenCenterX - int32(titleW/2)
|
titleX := p.Base.ScreenCenterX - int32(titleW/2)
|
||||||
titleY := p.Base.ScreenHeight - margin - int32(titleH)
|
titleY := p.Base.ScreenHeight - margin - int32(titleH)
|
||||||
p.Base.RenderTexture(textShader, p.titleTextureID, mgl32.Vec2{float32(titleX), float32(titleY)}, mgl32.Vec2{titleW, titleH}, white)
|
p.Base.RenderTexture(textShader, p.titleTextureID, mgl32.Vec2{float32(titleX), float32(titleY)}, mgl32.Vec2{titleW, titleH}, colors.White)
|
||||||
|
|
||||||
// Hint at bottom
|
// Hint at bottom
|
||||||
hintH := float32(p.hintTextureHeight) * hintScale
|
hintH := float32(p.hintTextureHeight) * hintScale
|
||||||
hintW := float32(p.hintTextureWidth) * hintScale
|
hintW := float32(p.hintTextureWidth) * hintScale
|
||||||
hintX := p.Base.ScreenCenterX - int32(hintW/2)
|
hintX := p.Base.ScreenCenterX - int32(hintW/2)
|
||||||
hintY := margin
|
hintY := margin
|
||||||
p.Base.RenderTexture(textShader, p.hintTextureID, mgl32.Vec2{float32(hintX), float32(hintY)}, mgl32.Vec2{hintW, hintH}, grey)
|
p.Base.RenderTexture(textShader, p.hintTextureID, mgl32.Vec2{float32(hintX), float32(hintY)}, mgl32.Vec2{hintW, hintH}, colors.Gray)
|
||||||
|
|
||||||
// Content area bounds
|
// Content area bounds
|
||||||
contentTop := titleY - margin
|
contentTop := titleY - margin
|
||||||
@@ -232,7 +244,12 @@ func (p *LogViewerPage) Render() error {
|
|||||||
lineW := float32(p.lineTextureWidths[i]) * contentScale
|
lineW := float32(p.lineTextureWidths[i]) * contentScale
|
||||||
lineH := float32(p.lineTextureHeights[i]) * contentScale
|
lineH := float32(p.lineTextureHeights[i]) * contentScale
|
||||||
|
|
||||||
p.Base.RenderTexture(textShader, p.lineTextureIDs[i], mgl32.Vec2{float32(contentLeftX), float32(currentY)}, mgl32.Vec2{lineW, lineH}, white)
|
color := colors.White
|
||||||
|
if i < len(p.lineColors) {
|
||||||
|
color = p.lineColors[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
p.Base.RenderTexture(textShader, p.lineTextureIDs[i], mgl32.Vec2{float32(contentLeftX), float32(currentY)}, mgl32.Vec2{lineW, lineH}, color)
|
||||||
currentY -= p.lineHeight
|
currentY -= p.lineHeight
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user