github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/stderrutils/dup_stderr_windows.go (about)

     1  // Copyright 2017 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  package stderrutils
     5  
     6  import (
     7  	"os"
     8  
     9  	"golang.org/x/sys/windows"
    10  )
    11  
    12  // DupStderr duplicates stderr and return it as an *os.File. Use this to
    13  // preserve stderr before any redirection (e.g. from keybase/client/go/logger)
    14  // if needed.
    15  func DupStderr() (*os.File, error) {
    16  	proc, err := windows.GetCurrentProcess()
    17  	if err != nil {
    18  		return nil, err
    19  	}
    20  
    21  	var dupStderrFd windows.Handle
    22  	err = windows.DuplicateHandle(
    23  		proc, windows.Handle(os.Stderr.Fd()), proc, &dupStderrFd, 0,
    24  		true, windows.DUPLICATE_SAME_ACCESS)
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  
    29  	stderrFile := os.NewFile(uintptr(dupStderrFd), "stderr")
    30  	return stderrFile, nil
    31  }