forked from golang/glog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterminal_darwin.go
45 lines (39 loc) · 950 Bytes
/
terminal_darwin.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
// +build darwin
package glog
import (
"os"
"syscall"
"unsafe"
)
var (
colorCyan = []byte{'\x1b', '[', '3', '6', 'm'}
colorRed = []byte{'\x1b', '[', '3', '1', 'm'}
colorYellow = []byte{'\x1b', '[', '3', '3', 'm'}
colorReset = []byte{'\x1b', '[', '0', 'm'}
)
func WriteFileWithColor(file *os.File, data []byte, s severity) {
var colorPrefix []byte
switch s {
case fatalLog:
colorPrefix = colorRed
case errorLog:
colorPrefix = colorRed
case warningLog:
colorPrefix = colorYellow
case infoLog:
colorPrefix = colorCyan
}
file.Write(colorPrefix)
file.Write(data[:5])
file.Write(colorReset)
file.Write(data[5:])
}
func IsTerminal(fd uintptr) bool {
var termios syscall.Termios
_, _, err := syscall.Syscall6(syscall.SYS_IOCTL, fd, syscall.TIOCGETA, uintptr(unsafe.Pointer(&termios)), 0, 0, 0)
return err == 0
}
func RedirectStderrTo(file *os.File) error {
os.Stderr = file
return syscall.Dup2(int(file.Fd()), 2)
}