mirror of
https://git.sr.ht/~ashkeel/strimertul
synced 2024-09-18 01:50:50 +00:00
feat: add stderr logging to its own file
This commit is contained in:
parent
074a8757b7
commit
d28db037ae
3 changed files with 69 additions and 1 deletions
19
main.go
19
main.go
|
@ -34,6 +34,8 @@ func main() {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var panicLog *os.File
|
||||||
|
|
||||||
app := &cli.App{
|
app := &cli.App{
|
||||||
Name: "strimertul",
|
Name: "strimertul",
|
||||||
Usage: "the small broadcasting suite for Twitch",
|
Usage: "the small broadcasting suite for Twitch",
|
||||||
|
@ -83,12 +85,27 @@ func main() {
|
||||||
level = zapcore.InfoLevel
|
level = zapcore.InfoLevel
|
||||||
}
|
}
|
||||||
initLogger(level)
|
initLogger(level)
|
||||||
|
|
||||||
|
// Create file for panics
|
||||||
|
panicLog, err = os.OpenFile("strimertul-panic.log", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0o666)
|
||||||
|
if err != nil {
|
||||||
|
logger.Warn("could not create panic log", zap.Error(err))
|
||||||
|
} else {
|
||||||
|
utils.RedirectStderr(panicLog)
|
||||||
|
}
|
||||||
|
|
||||||
|
zap.RedirectStdLog(logger)()
|
||||||
|
|
||||||
ctx.Context = context.WithValue(ctx.Context, utils.ContextLogger, logger)
|
ctx.Context = context.WithValue(ctx.Context, utils.ContextLogger, logger)
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
After: func(ctx *cli.Context) error {
|
After: func(ctx *cli.Context) error {
|
||||||
|
if panicLog != nil {
|
||||||
|
utils.Close(panicLog, logger)
|
||||||
|
}
|
||||||
|
|
||||||
_ = logger.Sync()
|
_ = logger.Sync()
|
||||||
zap.RedirectStdLog(logger)()
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
17
utils/stderr_unix.go
Normal file
17
utils/stderr_unix.go
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
//go:build unix
|
||||||
|
|
||||||
|
package utils
|
||||||
|
|
||||||
|
import "os"
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
func RedirectStderr(f *os.File) {
|
||||||
|
err := syscall.Dup2(int(f.Fd()), int(os.Stderr.Fd()))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to redirect stderr to file: %v", err)
|
||||||
|
}
|
||||||
|
}
|
34
utils/stderr_windows.go
Normal file
34
utils/stderr_windows.go
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
//go:build windows
|
||||||
|
|
||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
kernel32 = syscall.MustLoadDLL("kernel32.dll")
|
||||||
|
procSetStdHandle = kernel32.MustFindProc("SetStdHandle")
|
||||||
|
)
|
||||||
|
|
||||||
|
func setStdHandle(stdhandle int32, handle syscall.Handle) error {
|
||||||
|
r0, _, e1 := syscall.Syscall(procSetStdHandle.Addr(), 2, uintptr(stdhandle), uintptr(handle), 0)
|
||||||
|
if r0 == 0 {
|
||||||
|
if e1 != 0 {
|
||||||
|
return error(e1)
|
||||||
|
}
|
||||||
|
return syscall.EINVAL
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func RedirectStderr(f *os.File) {
|
||||||
|
err := setStdHandle(syscall.STD_ERROR_HANDLE, syscall.Handle(f.Fd()))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to redirect stderr to file: %v", err)
|
||||||
|
}
|
||||||
|
// SetStdHandle does not affect prior references to stderr
|
||||||
|
os.Stderr = f
|
||||||
|
}
|
Loading…
Reference in a new issue