-
Notifications
You must be signed in to change notification settings - Fork 6
/
redirect_win.go
53 lines (46 loc) · 1.03 KB
/
redirect_win.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//go:build windows
package tau
import (
"fmt"
"io"
"os"
"golang.org/x/sys/windows"
)
func redirectStdout(w io.Writer) {
pr, pw, err := os.Pipe()
if err != nil {
fmt.Println("Error creating pipe:", err)
return
}
// Set the pipe writer as the stdout
var stdHandle windows.Handle
err = windows.DuplicateHandle(windows.CurrentProcess(), windows.Handle(pw.Fd()), windows.CurrentProcess(), &stdHandle, 0, true, windows.DUPLICATE_SAME_ACCESS)
if err != nil {
fmt.Println("Error duplicating handle:", err)
return
}
err = windows.SetStdHandle(windows.STD_OUTPUT_HANDLE, stdHandle)
if err != nil {
fmt.Println("Error setting stdout:", err)
return
}
go func() {
var buf = make([]byte, 4096)
for {
n, err := pr.Read(buf)
if err != nil {
if err != io.EOF {
fmt.Println("error reading from pipe:", err)
}
break
}
// Write the captured output to the provided writer
_, err = w.Write(buf[:n])
if err != nil {
fmt.Println("error writing to writer:", err)
break
}
}
pr.Close()
}()
}