diff --git a/backend/log/dup_write_darwin.go b/backend/log/dup_write_darwin.go new file mode 100644 index 000000000..2a09ac50a --- /dev/null +++ b/backend/log/dup_write_darwin.go @@ -0,0 +1,20 @@ +package log + +import ( + "os" + "runtime" + "syscall" +) + +var stdErrFileHandler *os.File + +func dupWrite(file *os.File) error { + stdErrFileHandler = file + if err := syscall.Dup2(int(file.Fd()), int(os.Stderr.Fd())); err != nil { + return err + } + runtime.SetFinalizer(stdErrFileHandler, func(fd *os.File) { + fd.Close() + }) + return nil +} diff --git a/backend/log/dup_write_linux_amd64.go b/backend/log/dup_write_linux_amd64.go new file mode 100644 index 000000000..2a09ac50a --- /dev/null +++ b/backend/log/dup_write_linux_amd64.go @@ -0,0 +1,20 @@ +package log + +import ( + "os" + "runtime" + "syscall" +) + +var stdErrFileHandler *os.File + +func dupWrite(file *os.File) error { + stdErrFileHandler = file + if err := syscall.Dup2(int(file.Fd()), int(os.Stderr.Fd())); err != nil { + return err + } + runtime.SetFinalizer(stdErrFileHandler, func(fd *os.File) { + fd.Close() + }) + return nil +} diff --git a/backend/log/dup_write_linux_arm64.go b/backend/log/dup_write_linux_arm64.go new file mode 100644 index 000000000..d35a3873f --- /dev/null +++ b/backend/log/dup_write_linux_arm64.go @@ -0,0 +1,20 @@ +package log + +import ( + "os" + "runtime" + "syscall" +) + +var stdErrFileHandler *os.File + +func dupWrite(file *os.File) error { + stdErrFileHandler = file + if err := syscall.Dup3(int(file.Fd()), int(os.Stderr.Fd())); err != nil { + return err + } + runtime.SetFinalizer(stdErrFileHandler, func(fd *os.File) { + fd.Close() + }) + return nil +} diff --git a/backend/log/dup_write_windows.go b/backend/log/dup_write_windows.go new file mode 100644 index 000000000..59a56ed32 --- /dev/null +++ b/backend/log/dup_write_windows.go @@ -0,0 +1,5 @@ +package log + +func dupWrite(file *os.File) error { + return nil +} diff --git a/backend/log/writer.go b/backend/log/writer.go index 8e922a491..974482af4 100644 --- a/backend/log/writer.go +++ b/backend/log/writer.go @@ -8,12 +8,10 @@ import ( "log" "os" "path" - "runtime" "sort" "strings" "sync" "sync/atomic" - "syscall" "time" "unsafe" ) @@ -80,33 +78,21 @@ var _asyncBufferPool = sync.Pool{ }, } -var stdErrFileHandler *os.File - func NewWriterFromConfig(c *Config) (RollingWriter, error) { if c.LogPath == "" || c.FileName == "" { return nil, ErrInvalidArgument } - if err := os.MkdirAll(c.LogPath, 0700); err != nil { return nil, err } - filepath := FilePath(c) file, err := os.OpenFile(filepath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644) if err != nil { return nil, err } - stdErrFileHandler = file - - if runtime.GOOS != "windows" { - if err = syscall.Dup2(int(file.Fd()), int(os.Stderr.Fd())); err != nil { - return nil, err - } - runtime.SetFinalizer(stdErrFileHandler, func(fd *os.File) { - fd.Close() - }) + if err := dupWrite(file); err != nil { + return nil, err } - mng, err := NewManager(c) if err != nil { return nil, err @@ -120,7 +106,6 @@ func NewWriterFromConfig(c *Config) (RollingWriter, error) { fire: mng.Fire(), cf: c, } - if c.MaxRemain > 0 { writer.rollingfilech = make(chan string, c.MaxRemain) dir, err := ioutil.ReadDir(c.LogPath)