Merge branch 'win10-color-support' of git://github.com/konsorten/logrus into konsorten-win10-color-support

This commit is contained in:
David Bariod 2018-09-16 09:12:22 +02:00
commit c38641a38d
6 changed files with 59 additions and 3 deletions

View File

@ -3,8 +3,15 @@
package logrus package logrus
import "golang.org/x/sys/unix" import (
"io"
"golang.org/x/sys/unix"
)
const ioctlReadTermios = unix.TIOCGETA const ioctlReadTermios = unix.TIOCGETA
type Termios unix.Termios type Termios unix.Termios
func initTerminal(w io.Writer) {
}

View File

@ -1,4 +1,4 @@
// +build !appengine,!js // +build !appengine,!js,!windows
package logrus package logrus

20
terminal_check_windows.go Normal file
View File

@ -0,0 +1,20 @@
// +build !appengine,!gopherjs,windows
package logrus
import (
"io"
"os"
"syscall"
)
func checkIfTerminal(w io.Writer) bool {
switch v := w.(type) {
case *os.File:
var mode uint32
err := syscall.GetConsoleMode(syscall.Handle(v.Fd()), &mode)
return err == nil
default:
return false
}
}

View File

@ -7,8 +7,15 @@
package logrus package logrus
import "golang.org/x/sys/unix" import (
"io"
"golang.org/x/sys/unix"
)
const ioctlReadTermios = unix.TCGETS const ioctlReadTermios = unix.TCGETS
type Termios unix.Termios type Termios unix.Termios
func initTerminal(w io.Writer) {
}

18
terminal_windows.go Normal file
View File

@ -0,0 +1,18 @@
// +build !appengine,!gopherjs,windows
package logrus
import (
"io"
"os"
"syscall"
sequences "github.com/konsorten/go-windows-terminal-sequences"
)
func initTerminal(w io.Writer) {
switch v := w.(type) {
case *os.File:
sequences.EnableVirtualTerminalProcessing(syscall.Handle(v.Fd()), true)
}
}

View File

@ -79,6 +79,10 @@ type TextFormatter struct {
func (f *TextFormatter) init(entry *Entry) { func (f *TextFormatter) init(entry *Entry) {
if entry.Logger != nil { if entry.Logger != nil {
f.isTerminal = checkIfTerminal(entry.Logger.Out) f.isTerminal = checkIfTerminal(entry.Logger.Out)
if f.isTerminal {
initTerminal(entry.Logger.Out)
}
} }
} }