Remove logfile if it's empty before exiting

This commit is contained in:
Kristóf Tóth 2020-09-09 23:50:28 +02:00
parent 4689797c78
commit 98ed8d9a29
2 changed files with 18 additions and 1 deletions

View File

@ -25,7 +25,7 @@ func main() {
defer handleErrors()
f := initLogging()
if f != nil {
defer f.Close()
defer teardownLogging(f)
}
lock := grabExclusiveProcessLock()

View File

@ -18,3 +18,20 @@ func initLogging() *os.File {
log.SetOutput(f)
return f
}
func teardownLogging(f *os.File) {
fi, err := f.Stat()
if err != nil {
panic(err)
}
if fi.Size() == 0 {
err = os.Remove(f.Name())
if err != nil {
panic(err)
}
}
err = f.Close()
if err != nil {
panic(err)
}
}