logging and fixes

This commit is contained in:
2025-08-20 13:29:12 +02:00
parent 52399acb42
commit 83d6aa10cb
7 changed files with 238 additions and 62 deletions

View File

@@ -3,6 +3,7 @@ package app
import (
"strings"
"git.blackforestbytes.com/BlackForestBytes/goext/dataext"
"git.blackforestbytes.com/BlackForestBytes/goext/exerr"
"git.blackforestbytes.com/BlackForestBytes/goext/langext"
"git.blackforestbytes.com/BlackForestBytes/goext/termext"
@@ -20,11 +21,11 @@ func (app *Application) LogFatal(msg string) {
func (app *Application) LogFatalErr(msg string, err error) {
if err != nil {
app.logInternal("[F] ", msg+"\n"+err.Error()+"\n"+exerr.FromError(err).FormatLog(exerr.LogPrintOverview), termext.Red)
panic(0)
} else {
app.logInternal("[F] ", msg, termext.Red)
panic(0)
}
panic(0)
}
func (app *Application) LogError(msg string, err error) {
@@ -48,6 +49,9 @@ func (app *Application) LogDebug(msg string) {
}
func (app *Application) logInternal(pf string, msg string, c func(_ string) string) {
app.logLock.Lock()
defer app.logLock.Unlock()
if !termext.SupportsColors() && !app.config.ForceColors {
c = func(s string) string { return s }
}
@@ -55,12 +59,74 @@ func (app *Application) logInternal(pf string, msg string, c func(_ string) stri
for i, s := range strings.Split(msg, "\n") {
if i == 0 {
println(c(pf + s))
app.logList = append(app.logList, dataext.NewTriple(strings.TrimSpace(pf), s, c))
if app.logFile != nil {
_, err := app.logFile.WriteString(pf + s + "\n")
if err != nil {
app.fallbackLog("[!] Failed to write logfile: " + err.Error())
}
}
} else {
println(c(langext.StrRepeat(" ", len(pf)) + s))
app.logList = append(app.logList, dataext.NewTriple(strings.TrimSpace(pf), s, c))
if app.logFile != nil {
_, err := app.logFile.WriteString(langext.StrRepeat(" ", len(pf)) + s + "\n")
if err != nil {
app.fallbackLog("[!] Failed to write logfile: " + err.Error())
}
}
}
}
if app.logFile != nil {
err := app.logFile.Sync()
if err != nil {
app.fallbackLog("[!] Failed to flush logfile: " + err.Error())
}
}
}
func (app *Application) LogLine() {
app.logLock.Lock()
defer app.logLock.Unlock()
println()
if app.logFile != nil {
_, err := app.logFile.WriteString("\n")
if err != nil {
app.fallbackLog("[!] Failed to write logfile: " + err.Error())
}
err = app.logFile.Sync()
if err != nil {
app.fallbackLog("[!] Failed to flush logfile: " + err.Error())
}
}
app.logList = append(app.logList, dataext.NewTriple("", "", func(v string) string { return v }))
}
func (app *Application) fallbackLog(s string) {
if termext.SupportsColors() || app.config.ForceColors {
s = termext.Red(s)
}
println(s)
}
func (app *Application) writeOutStartupLogs() {
app.logLock.Lock()
defer app.logLock.Unlock()
for _, v := range app.logList {
_, err := app.logFile.WriteString(v.V1 + " " + v.V2 + "\n")
if err != nil {
app.fallbackLog("[!] Failed to write logfile: " + err.Error())
}
}
err := app.logFile.Sync()
if err != nil {
app.fallbackLog("[!] Failed to flush logfile: " + err.Error())
}
}