github.com/xhghs/rclone@v1.51.1-0.20200430155106-e186a28cced8/fs/log/redirect_stderr_windows.go (about)

     1  // Log the panic under windows to the log file
     2  //
     3  // Code from minix, via
     4  //
     5  // https://play.golang.org/p/kLtct7lSUg
     6  
     7  // +build windows
     8  
     9  package log
    10  
    11  import (
    12  	"log"
    13  	"os"
    14  	"syscall"
    15  )
    16  
    17  var (
    18  	kernel32         = syscall.MustLoadDLL("kernel32.dll")
    19  	procSetStdHandle = kernel32.MustFindProc("SetStdHandle")
    20  )
    21  
    22  func setStdHandle(stdhandle int32, handle syscall.Handle) error {
    23  	r0, _, e1 := syscall.Syscall(procSetStdHandle.Addr(), 2, uintptr(stdhandle), uintptr(handle), 0)
    24  	if r0 == 0 {
    25  		if e1 != 0 {
    26  			return error(e1)
    27  		}
    28  		return syscall.EINVAL
    29  	}
    30  	return nil
    31  }
    32  
    33  // redirectStderr to the file passed in
    34  func redirectStderr(f *os.File) {
    35  	err := setStdHandle(syscall.STD_ERROR_HANDLE, syscall.Handle(f.Fd()))
    36  	if err != nil {
    37  		log.Fatalf("Failed to redirect stderr to file: %v", err)
    38  	}
    39  }