mirror of https://github.com/sirupsen/logrus.git
use x/sys for non-unix files
This commit is contained in:
parent
f4125cea1b
commit
8a90bf3fff
|
@ -10,5 +10,6 @@ install:
|
||||||
- go get github.com/stretchr/testify/assert
|
- go get github.com/stretchr/testify/assert
|
||||||
- go get gopkg.in/gemnasium/logrus-airbrake-hook.v2
|
- go get gopkg.in/gemnasium/logrus-airbrake-hook.v2
|
||||||
- go get golang.org/x/sys/unix
|
- go get golang.org/x/sys/unix
|
||||||
|
- go get golang.org/x/sys/windows
|
||||||
script:
|
script:
|
||||||
- go test -race -v ./...
|
- go test -race -v ./...
|
||||||
|
|
|
@ -3,8 +3,8 @@
|
||||||
|
|
||||||
package logrus
|
package logrus
|
||||||
|
|
||||||
import "syscall"
|
import "golang.org/x/sys/unix"
|
||||||
|
|
||||||
const ioctlReadTermios = syscall.TIOCGETA
|
const ioctlReadTermios = unix.TIOCGETA
|
||||||
|
|
||||||
type Termios syscall.Termios
|
type Termios unix.Termios
|
||||||
|
|
|
@ -11,8 +11,9 @@ package logrus
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"syscall"
|
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
)
|
)
|
||||||
|
|
||||||
// IsTerminal returns true if stderr's file descriptor is a terminal.
|
// IsTerminal returns true if stderr's file descriptor is a terminal.
|
||||||
|
@ -20,7 +21,7 @@ func IsTerminal(f io.Writer) bool {
|
||||||
var termios Termios
|
var termios Termios
|
||||||
switch v := f.(type) {
|
switch v := f.(type) {
|
||||||
case *os.File:
|
case *os.File:
|
||||||
_, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(v.Fd()), ioctlReadTermios, uintptr(unsafe.Pointer(&termios)), 0, 0, 0)
|
_, _, err := unix.Syscall6(unix.SYS_IOCTL, uintptr(v.Fd()), ioctlReadTermios, uintptr(unsafe.Pointer(&termios)), 0, 0, 0)
|
||||||
return err == 0
|
return err == 0
|
||||||
default:
|
default:
|
||||||
return false
|
return false
|
||||||
|
|
|
@ -15,11 +15,12 @@ import (
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
|
"golang.org/x/sys/windows"
|
||||||
)
|
)
|
||||||
|
|
||||||
var kernel32 = syscall.NewLazyDLL("kernel32.dll")
|
var kernel32 = windows.NewLazyDLL("kernel32.dll")
|
||||||
|
|
||||||
var (
|
var (
|
||||||
procGetConsoleMode = kernel32.NewProc("GetConsoleMode")
|
procGetConsoleMode = kernel32.NewProc("GetConsoleMode")
|
||||||
|
@ -41,7 +42,7 @@ func getVersion() (float64, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return -1, err
|
return -1, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// The output should be like "Microsoft Windows [Version XX.X.XXXXXX]"
|
// The output should be like "Microsoft Windows [Version XX.X.XXXXXX]"
|
||||||
version := strings.Replace(stdout.String(), "\n", "", -1)
|
version := strings.Replace(stdout.String(), "\n", "", -1)
|
||||||
version = strings.Replace(version, "\r\n", "", -1)
|
version = strings.Replace(version, "\r\n", "", -1)
|
||||||
|
@ -64,7 +65,7 @@ func init() {
|
||||||
// Activate Virtual Processing for Windows CMD
|
// Activate Virtual Processing for Windows CMD
|
||||||
// Info: https://msdn.microsoft.com/en-us/library/windows/desktop/ms686033(v=vs.85).aspx
|
// Info: https://msdn.microsoft.com/en-us/library/windows/desktop/ms686033(v=vs.85).aspx
|
||||||
if ver >= 10 {
|
if ver >= 10 {
|
||||||
handle := syscall.Handle(os.Stderr.Fd())
|
handle := windows.Handle(os.Stderr.Fd())
|
||||||
procSetConsoleMode.Call(uintptr(handle), enableProcessedOutput|enableWrapAtEolOutput|enableVirtualTerminalProcessing)
|
procSetConsoleMode.Call(uintptr(handle), enableProcessedOutput|enableWrapAtEolOutput|enableVirtualTerminalProcessing)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -74,7 +75,7 @@ func IsTerminal(f io.Writer) bool {
|
||||||
switch v := f.(type) {
|
switch v := f.(type) {
|
||||||
case *os.File:
|
case *os.File:
|
||||||
var st uint32
|
var st uint32
|
||||||
r, _, e := syscall.Syscall(procGetConsoleMode.Addr(), 2, uintptr(v.Fd()), uintptr(unsafe.Pointer(&st)), 0)
|
r, _, e := windows.Syscall(procGetConsoleMode.Addr(), 2, uintptr(v.Fd()), uintptr(unsafe.Pointer(&st)), 0)
|
||||||
return r != 0 && e == 0
|
return r != 0 && e == 0
|
||||||
default:
|
default:
|
||||||
return false
|
return false
|
||||||
|
|
Loading…
Reference in New Issue