1
0
Fork 0
mirror of https://git.sr.ht/~ashkeel/strimertul synced 2024-09-18 01:50:50 +00:00

fix: wrap logs to prevent data races

This commit is contained in:
Ash Keel 2022-12-04 18:35:15 +01:00
parent 3bc662796f
commit 398cf8da47
No known key found for this signature in database
GPG key ID: BAD8D93E7314ED3E
2 changed files with 8 additions and 6 deletions

2
app.go
View file

@ -135,5 +135,5 @@ func (a *App) GetTwitchLoggedUser() (helix.User, error) {
}
func (a *App) GetLastLogs() []LogEntry {
return lastLogs
return lastLogs.Get()
}

View file

@ -4,6 +4,8 @@ import (
"os"
"time"
"git.sr.ht/~hamcha/containers/sync"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
@ -13,12 +15,12 @@ const LogHistory = 50
var (
logger *zap.Logger
lastLogs []LogEntry
lastLogs *sync.Slice[LogEntry]
incomingLogs chan LogEntry
)
func initLogger(level zapcore.Level) {
lastLogs = make([]LogEntry, 0)
lastLogs = sync.NewSlice[LogEntry]()
incomingLogs = make(chan LogEntry, 100)
logStorage := NewLogStorage(level)
consoleLogger := zapcore.NewCore(
@ -90,9 +92,9 @@ func (core *LogStorage) Write(entry zapcore.Entry, fields []zapcore.Field) error
Message: entry.Message,
Data: buf.String(),
}
lastLogs = append(lastLogs, logEntry)
if len(lastLogs) > LogHistory {
lastLogs = lastLogs[1:]
lastLogs.Push(logEntry)
if lastLogs.Size() > LogHistory {
lastLogs.Splice(0, 1)
}
incomingLogs <- logEntry
return nil