Properly color logs

This commit is contained in:
NeilClack
2026-06-26 18:06:38 -05:00
parent 61b0fbd779
commit a1bc7e1c1e
+28 -11
View File
@@ -4,10 +4,11 @@ import (
"fmt"
"strings"
"time"
"slices"
"github.com/go-gl/gl/v3.3-core/gl"
"github.com/go-gl/mathgl/mgl32"
"github.com/veandco/go-sdl2/sdl"
"rhemawavetech.com/rhemawave/internal/colors"
"rhemawavetech.com/rhemawave/internal/db"
"rhemawavetech.com/rhemawave/internal/fontManager"
"rhemawavetech.com/rhemawave/internal/logger"
@@ -25,6 +26,7 @@ type LogViewerPage struct {
lineTextureIDs []uint32
lineTextureWidths []int32
lineTextureHeights []int32
lineColors []sdl.Color
hintTextureID uint32
hintTextureWidth int32
@@ -47,21 +49,34 @@ func (p *LogViewerPage) Init(ctx *PageCtx) 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 {
// formatted := fmt.Sprintf("[%s] [%s] %s", entry.Timestamp, entry.Level, entry.Message)
// wrapped := wrapText(formatted, 100)
// 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 {
if slices.Contains(displayLevels, entry.Level) {
color, ok := levelColors[entry.Level]
if !ok {
color = fallbackColor
}
logTime := time.UnixMilli(entry.Timestamp)
timeStr := logTime.Format("2006-01-02 15:04")
line := fmt.Sprintf("[%s] [%s] %s", timeStr, entry.Level, entry.Message)
p.lines = append(p.lines, line)
}
p.lineColors = append(p.lineColors, color)
}
applicationFont := "Roboto-Regular"
@@ -167,9 +182,6 @@ func (p *LogViewerPage) Render() error {
sm := shaderManager.Get()
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)
titleScale := float32(titleFontSize) / float32(baseFontSize)
contentFontSize := int32(24)
@@ -189,14 +201,14 @@ func (p *LogViewerPage) Render() error {
titleW := float32(p.titleTextureWidth) * titleScale
titleX := p.Base.ScreenCenterX - int32(titleW/2)
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
hintH := float32(p.hintTextureHeight) * hintScale
hintW := float32(p.hintTextureWidth) * hintScale
hintX := p.Base.ScreenCenterX - int32(hintW/2)
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
contentTop := titleY - margin
@@ -232,7 +244,12 @@ func (p *LogViewerPage) Render() error {
lineW := float32(p.lineTextureWidths[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
}