From a79061c7c285d0d93b385c2b57c3adb3250a10dd Mon Sep 17 00:00:00 2001 From: Sergey Krashevich Date: Tue, 28 May 2024 09:10:32 +0300 Subject: [PATCH] feat(logging): add interactive shell detection for console output --- internal/app/log.go | 3 ++- pkg/shell/tty.go | 7 +++++++ pkg/shell/tty_unix.go | 10 ++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 pkg/shell/tty.go create mode 100644 pkg/shell/tty_unix.go diff --git a/internal/app/log.go b/internal/app/log.go index e8d4bc88..e656737c 100644 --- a/internal/app/log.go +++ b/internal/app/log.go @@ -4,6 +4,7 @@ import ( "io" "os" + "github.com/AlexxIT/go2rtc/pkg/shell" "github.com/rs/zerolog" "github.com/rs/zerolog/log" ) @@ -15,7 +16,7 @@ func NewLogger(format string, level string) zerolog.Logger { if format != "json" { writer = zerolog.ConsoleWriter{ - Out: writer, TimeFormat: "15:04:05.000", NoColor: format == "text", + Out: writer, TimeFormat: "15:04:05.000", NoColor: (format == "text" || !shell.IsInteractive(os.Stdout.Fd())), } } diff --git a/pkg/shell/tty.go b/pkg/shell/tty.go new file mode 100644 index 00000000..d433369f --- /dev/null +++ b/pkg/shell/tty.go @@ -0,0 +1,7 @@ +//go:build !unix + +package shell + +func IsInteractive(fd uintptr) bool { + return false +} diff --git a/pkg/shell/tty_unix.go b/pkg/shell/tty_unix.go new file mode 100644 index 00000000..07b68a60 --- /dev/null +++ b/pkg/shell/tty_unix.go @@ -0,0 +1,10 @@ +//go:build unix + +package shell + +import "golang.org/x/sys/unix" + +func IsInteractive(fd uintptr) bool { + _, err := unix.IoctlGetTermios(int(fd), unix.TIOCGETA) + return err == nil +}