diff --git a/terminal_bsd.go b/terminal_bsd.go index 5b6212d..62ca252 100644 --- a/terminal_bsd.go +++ b/terminal_bsd.go @@ -3,8 +3,15 @@ package logrus -import "golang.org/x/sys/unix" +import ( + "io" + + "golang.org/x/sys/unix" +) const ioctlReadTermios = unix.TIOCGETA type Termios unix.Termios + +func initTerminal(w io.Writer) { +} diff --git a/terminal_check_notappengine.go b/terminal_check_notappengine.go index 87f0b80..cf309d6 100644 --- a/terminal_check_notappengine.go +++ b/terminal_check_notappengine.go @@ -1,4 +1,4 @@ -// +build !appengine,!js +// +build !appengine,!js,!windows package logrus diff --git a/terminal_check_windows.go b/terminal_check_windows.go new file mode 100644 index 0000000..17ebe80 --- /dev/null +++ b/terminal_check_windows.go @@ -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 + } +} diff --git a/terminal_linux.go b/terminal_linux.go index 634e39b..18066f0 100644 --- a/terminal_linux.go +++ b/terminal_linux.go @@ -7,8 +7,15 @@ package logrus -import "golang.org/x/sys/unix" +import ( + "io" + + "golang.org/x/sys/unix" +) const ioctlReadTermios = unix.TCGETS type Termios unix.Termios + +func initTerminal(w io.Writer) { +} diff --git a/terminal_windows.go b/terminal_windows.go new file mode 100644 index 0000000..2494950 --- /dev/null +++ b/terminal_windows.go @@ -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) + } +} diff --git a/text_formatter.go b/text_formatter.go index 4b1f071..6aee14f 100644 --- a/text_formatter.go +++ b/text_formatter.go @@ -79,6 +79,10 @@ type TextFormatter struct { func (f *TextFormatter) init(entry *Entry) { if entry.Logger != nil { f.isTerminal = checkIfTerminal(entry.Logger.Out) + + if f.isTerminal { + initTerminal(entry.Logger.Out) + } } }