[client] Enable windows stderr logs by default (#3476)

This commit is contained in:
Viktor Liu 2025-03-10 11:30:49 +01:00 committed by GitHub
parent fc1da94520
commit c73481aee4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,7 +3,7 @@ package server
import ( import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path"
"syscall" "syscall"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -12,7 +12,6 @@ import (
) )
const ( const (
windowsPanicLogEnvVar = "NB_WINDOWS_PANIC_LOG"
// STD_ERROR_HANDLE ((DWORD)-12) = 4294967284 // STD_ERROR_HANDLE ((DWORD)-12) = 4294967284
stdErrorHandle = ^uintptr(11) stdErrorHandle = ^uintptr(11)
) )
@ -25,13 +24,10 @@ var (
) )
func handlePanicLog() error { func handlePanicLog() error {
logPath := os.Getenv(windowsPanicLogEnvVar) // TODO: move this to a central location
if logPath == "" { logDir := path.Join(os.Getenv("PROGRAMDATA"), "Netbird")
return nil logPath := path.Join(logDir, "netbird.err")
}
// Ensure the directory exists
logDir := filepath.Dir(logPath)
if err := os.MkdirAll(logDir, 0750); err != nil { if err := os.MkdirAll(logDir, 0750); err != nil {
return fmt.Errorf("create panic log directory: %w", err) return fmt.Errorf("create panic log directory: %w", err)
} }
@ -39,13 +35,11 @@ func handlePanicLog() error {
return fmt.Errorf("enforce permission on panic log file: %w", err) return fmt.Errorf("enforce permission on panic log file: %w", err)
} }
// Open log file with append mode
f, err := os.OpenFile(logPath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644) f, err := os.OpenFile(logPath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil { if err != nil {
return fmt.Errorf("open panic log file: %w", err) return fmt.Errorf("open panic log file: %w", err)
} }
// Redirect stderr to the file
if err = redirectStderr(f); err != nil { if err = redirectStderr(f); err != nil {
if closeErr := f.Close(); closeErr != nil { if closeErr := f.Close(); closeErr != nil {
log.Warnf("failed to close file after redirect error: %v", closeErr) log.Warnf("failed to close file after redirect error: %v", closeErr)
@ -59,7 +53,6 @@ func handlePanicLog() error {
// redirectStderr redirects stderr to the provided file // redirectStderr redirects stderr to the provided file
func redirectStderr(f *os.File) error { func redirectStderr(f *os.File) error {
// Get the current process's stderr handle
if err := setStdHandle(f); err != nil { if err := setStdHandle(f); err != nil {
return fmt.Errorf("failed to set stderr handle: %w", err) return fmt.Errorf("failed to set stderr handle: %w", err)
} }