forked from alexflint/gallium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redirect.go
46 lines (39 loc) · 1.32 KB
/
redirect.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
package gallium
import (
"os"
"syscall"
)
// RedirectStdoutStderr overwrites the stdout and stderr streams with a file
// descriptor that writes to the given path. This is done with os.Dup2,
// meaning that even C functions that write to stdout or stderr will be
// redirected to the file.
func RedirectStdoutStderr(path string) (*os.File, error) {
return redirect(path, os.Stdout.Fd(), os.Stderr.Fd())
}
// RedirectStdout overwrites the stdout streams with a file
// descriptor that writes to the given path. This is done with os.Dup2,
// meaning that even C functions that write to stdout will be
// redirected to the file.
func RedirectStdout(path string) (*os.File, error) {
return redirect(path, os.Stdout.Fd())
}
// RedirectStderr overwrites the stderr streams with a file
// descriptor that writes to the given path. This is done with os.Dup2,
// meaning that even C functions that write to stderr will be
// redirected to the file.
func RedirectStderr(path string) (*os.File, error) {
return redirect(path, os.Stderr.Fd())
}
func redirect(path string, fds ...uintptr) (*os.File, error) {
f, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0777)
if err != nil {
return nil, err
}
for _, fd := range fds {
err = syscall.Dup2(int(f.Fd()), int(fd))
if err != nil {
return nil, err
}
}
return f, nil
}