From bb487e068c121ad7129d3d4b6628507038d85d7a Mon Sep 17 00:00:00 2001 From: Felix Kollmann Date: Tue, 3 Apr 2018 00:25:30 +0200 Subject: [PATCH 1/7] Added support for text coloring on Windows 10 --- text_formatter.go | 2 ++ text_formatter_linux.go | 6 ++++++ text_formatter_windows.go | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+) create mode 100644 text_formatter_linux.go create mode 100644 text_formatter_windows.go diff --git a/text_formatter.go b/text_formatter.go index ae91edd..afd9ffb 100644 --- a/text_formatter.go +++ b/text_formatter.go @@ -67,6 +67,8 @@ type TextFormatter struct { func (f *TextFormatter) init(entry *Entry) { if entry.Logger != nil { f.isTerminal = checkIfTerminal(entry.Logger.Out) + + f.initTerminal(entry) } } diff --git a/text_formatter_linux.go b/text_formatter_linux.go new file mode 100644 index 0000000..e5fa6a9 --- /dev/null +++ b/text_formatter_linux.go @@ -0,0 +1,6 @@ +// +build !appengine,!gopherjs + +package logrus + +func (f *TextFormatter) initTerminal(entry *Entry) { +} diff --git a/text_formatter_windows.go b/text_formatter_windows.go new file mode 100644 index 0000000..552c5f3 --- /dev/null +++ b/text_formatter_windows.go @@ -0,0 +1,19 @@ +// +build !appengine,!gopherjs + +package logrus + +import ( + "os" + "syscall" + + sequences "github.com/konsorten/go-windows-terminal-sequences" +) + +func (f *TextFormatter) initTerminal(entry *Entry) { + switch v := entry.Logger.Out.(type) { + case *os.File: + handle := syscall.Handle(v.Fd()) + + sequences.EnableVirtualTerminalProcessing(handle, true) + } +} From f142d8145bc6c072eb15937ab37cb6ede92d579e Mon Sep 17 00:00:00 2001 From: Felix Kollmann Date: Tue, 3 Apr 2018 01:15:45 +0200 Subject: [PATCH 2/7] Improved building of non-windows code --- text_formatter_linux.go => text_formatter_nonwindows.go | 2 +- text_formatter_windows.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename text_formatter_linux.go => text_formatter_nonwindows.go (64%) diff --git a/text_formatter_linux.go b/text_formatter_nonwindows.go similarity index 64% rename from text_formatter_linux.go rename to text_formatter_nonwindows.go index e5fa6a9..f732b8b 100644 --- a/text_formatter_linux.go +++ b/text_formatter_nonwindows.go @@ -1,4 +1,4 @@ -// +build !appengine,!gopherjs +// +build !appengine,!gopherjs,!windows package logrus diff --git a/text_formatter_windows.go b/text_formatter_windows.go index 552c5f3..c9045a0 100644 --- a/text_formatter_windows.go +++ b/text_formatter_windows.go @@ -1,4 +1,4 @@ -// +build !appengine,!gopherjs +// +build !appengine,!gopherjs,windows package logrus From 7d2a5214bf851c9c13289e475a486b1761375216 Mon Sep 17 00:00:00 2001 From: Felix Kollmann Date: Tue, 3 Apr 2018 01:23:44 +0200 Subject: [PATCH 3/7] Extended conditions to include non-native builds --- text_formatter_nonwindows.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/text_formatter_nonwindows.go b/text_formatter_nonwindows.go index f732b8b..0c0802a 100644 --- a/text_formatter_nonwindows.go +++ b/text_formatter_nonwindows.go @@ -1,4 +1,4 @@ -// +build !appengine,!gopherjs,!windows +// +build appengine gopherjs !windows package logrus From c9a46a1e7c34ec07a1ffd66147e654d85ac94e36 Mon Sep 17 00:00:00 2001 From: Felix Kollmann Date: Tue, 3 Apr 2018 04:40:58 +0200 Subject: [PATCH 4/7] Added terminal check on Windows --- terminal_check_notappengine.go | 2 +- terminal_check_windows.go | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 terminal_check_windows.go diff --git a/terminal_check_notappengine.go b/terminal_check_notappengine.go index 067047a..e5a5186 100644 --- a/terminal_check_notappengine.go +++ b/terminal_check_notappengine.go @@ -1,4 +1,4 @@ -// +build !appengine,!gopherjs +// +build !appengine,!gopherjs,!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 + } +} From cf5eba7dfdefae2d33eb00af1e805bc31f6c4151 Mon Sep 17 00:00:00 2001 From: Felix Kollmann Date: Tue, 3 Apr 2018 04:47:29 +0200 Subject: [PATCH 5/7] Simplified file structure --- terminal_bsd.go | 3 +++ terminal_linux.go | 3 +++ text_formatter_windows.go => terminal_windows.go | 0 text_formatter_nonwindows.go | 6 ------ 4 files changed, 6 insertions(+), 6 deletions(-) rename text_formatter_windows.go => terminal_windows.go (100%) delete mode 100644 text_formatter_nonwindows.go diff --git a/terminal_bsd.go b/terminal_bsd.go index 4880d13..3b20604 100644 --- a/terminal_bsd.go +++ b/terminal_bsd.go @@ -8,3 +8,6 @@ import "golang.org/x/sys/unix" const ioctlReadTermios = unix.TIOCGETA type Termios unix.Termios + +func (f *TextFormatter) initTerminal(entry *Entry) { +} diff --git a/terminal_linux.go b/terminal_linux.go index f29a009..78c4298 100644 --- a/terminal_linux.go +++ b/terminal_linux.go @@ -12,3 +12,6 @@ import "golang.org/x/sys/unix" const ioctlReadTermios = unix.TCGETS type Termios unix.Termios + +func (f *TextFormatter) initTerminal(entry *Entry) { +} diff --git a/text_formatter_windows.go b/terminal_windows.go similarity index 100% rename from text_formatter_windows.go rename to terminal_windows.go diff --git a/text_formatter_nonwindows.go b/text_formatter_nonwindows.go deleted file mode 100644 index 0c0802a..0000000 --- a/text_formatter_nonwindows.go +++ /dev/null @@ -1,6 +0,0 @@ -// +build appengine gopherjs !windows - -package logrus - -func (f *TextFormatter) initTerminal(entry *Entry) { -} From 9bc59a596929f70e795b6a59bdf95b8c90cb9a7f Mon Sep 17 00:00:00 2001 From: Felix Kollmann Date: Tue, 3 Apr 2018 04:50:50 +0200 Subject: [PATCH 6/7] Fixed initTerminal() was run for non-terminals --- text_formatter.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/text_formatter.go b/text_formatter.go index afd9ffb..c62c412 100644 --- a/text_formatter.go +++ b/text_formatter.go @@ -51,7 +51,6 @@ type TextFormatter struct { // be desired. DisableSorting bool - // Disables the truncation of the level text to 4 characters. DisableLevelTruncation bool @@ -68,7 +67,9 @@ func (f *TextFormatter) init(entry *Entry) { if entry.Logger != nil { f.isTerminal = checkIfTerminal(entry.Logger.Out) - f.initTerminal(entry) + if f.isTerminal { + f.initTerminal(entry) + } } } From 2f58bc83cb1aaa88d642d561af062b7a04d15129 Mon Sep 17 00:00:00 2001 From: Felix Kollmann Date: Tue, 3 Apr 2018 04:55:52 +0200 Subject: [PATCH 7/7] Unified terminal initialization code handling --- terminal_bsd.go | 8 ++++++-- terminal_linux.go | 8 ++++++-- terminal_windows.go | 9 ++++----- text_formatter.go | 2 +- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/terminal_bsd.go b/terminal_bsd.go index 3b20604..c498bcd 100644 --- a/terminal_bsd.go +++ b/terminal_bsd.go @@ -3,11 +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 (f *TextFormatter) initTerminal(entry *Entry) { +func initTerminal(w io.Writer) { } diff --git a/terminal_linux.go b/terminal_linux.go index 78c4298..6eb013e 100644 --- a/terminal_linux.go +++ b/terminal_linux.go @@ -7,11 +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 (f *TextFormatter) initTerminal(entry *Entry) { +func initTerminal(w io.Writer) { } diff --git a/terminal_windows.go b/terminal_windows.go index c9045a0..2494950 100644 --- a/terminal_windows.go +++ b/terminal_windows.go @@ -3,17 +3,16 @@ package logrus import ( + "io" "os" "syscall" sequences "github.com/konsorten/go-windows-terminal-sequences" ) -func (f *TextFormatter) initTerminal(entry *Entry) { - switch v := entry.Logger.Out.(type) { +func initTerminal(w io.Writer) { + switch v := w.(type) { case *os.File: - handle := syscall.Handle(v.Fd()) - - sequences.EnableVirtualTerminalProcessing(handle, true) + sequences.EnableVirtualTerminalProcessing(syscall.Handle(v.Fd()), true) } } diff --git a/text_formatter.go b/text_formatter.go index c62c412..b4e1f0a 100644 --- a/text_formatter.go +++ b/text_formatter.go @@ -68,7 +68,7 @@ func (f *TextFormatter) init(entry *Entry) { f.isTerminal = checkIfTerminal(entry.Logger.Out) if f.isTerminal { - f.initTerminal(entry) + initTerminal(entry.Logger.Out) } } }