Write logfiles on stop only

master
HimbeerserverDE 2021-04-06 15:10:36 +02:00
parent fc3c359147
commit 901a916462
No known key found for this signature in database
GPG Key ID: 1A651504791E6A8B
3 changed files with 18 additions and 32 deletions

View File

@ -124,7 +124,7 @@ func initCurses(l *Logger) {
consoleInput = append(consoleInput, ch)
}
draw([]string(*l))
draw(l.visible)
}
}()
}

2
end.go
View File

@ -31,6 +31,8 @@ func End(crash, reconnect bool) {
Announce(AnnounceDelete)
log.Writer().(*Logger).Close()
gocurses.End()
if crash {

46
log.go
View File

@ -9,30 +9,7 @@ import (
var logReady chan struct{}
var sep []byte = []byte(`
+-----------+
| Seperator |
+-----------+
`)
func WriteAppend(name string, data []byte, perm os.FileMode) error {
os.Mkdir("log", 0777)
b, err := os.ReadFile(name)
if err != nil && !os.IsNotExist(err) {
return err
}
err = os.WriteFile(name, append(b, data...), perm)
if err != nil {
return err
}
return nil
}
func appendPop(max int, a Logger, v ...string) Logger {
func appendPop(max int, a []string, v ...string) []string {
if len(a) < max {
return append(a, v...)
} else {
@ -45,11 +22,12 @@ func appendPop(max int, a Logger, v ...string) Logger {
}
}
type Logger []string
type Logger struct {
visible []string
all []byte
}
func newLogger() *Logger {
os.Rename("log/latest.txt", "log/last.txt")
l := &Logger{}
initCurses(l)
return l
@ -57,15 +35,21 @@ func newLogger() *Logger {
func (l *Logger) Write(p []byte) (int, error) {
row, _ := gocurses.Getmaxyx()
*l = appendPop(row-1, *l, string(p))
draw([]string(*l))
l.visible = appendPop(row-1, l.visible, string(p))
draw(l.visible)
// Write to file
WriteAppend("log/latest.txt", p, 0666)
l.all = append(l.all, p...)
return len(p), nil
}
func (l *Logger) Close() {
os.Mkdir("log", 0777)
os.Rename("log/latest.txt", "log/last.txt")
os.WriteFile("log/latest.txt", l.all, 0666)
}
func LogReady() <-chan struct{} {
if logReady == nil {
logReady = make(chan struct{})