Skip to content

Commit

Permalink
Improve AutoColor functionality
Browse files Browse the repository at this point in the history
Cleanup AutoColor to make it never fail, instead do it's best to
activate color if possible only.

Fixes #122
  • Loading branch information
evanphx committed Jan 26, 2023
1 parent fb16e2d commit 7445e89
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 42 deletions.
36 changes: 23 additions & 13 deletions colorize_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,33 @@ import (
"github.com/mattn/go-isatty"
)

// hasFD is used to check if the writer has an Fd value to check
// if it's a terminal.
type hasFD interface {
Fd() uintptr
}

// setColorization will mutate the values of this logger
// to appropriately configure colorization options. It provides
// a wrapper to the output stream on Windows systems.
func (l *intLogger) setColorization(opts *LoggerOptions) {
switch opts.Color {
case ColorOff:
fallthrough
case ForceColor:
if opts.Color != AutoColor {
return
}

if sc, ok := l.writer.w.(SupportsColor); ok && !sc.SupportsColor() {
l.headerColor = ColorOff
l.writer.color = ColorOff
return
case AutoColor:
fi := l.checkWriterIsFile()
isUnixTerm := isatty.IsTerminal(fi.Fd())
isCygwinTerm := isatty.IsCygwinTerminal(fi.Fd())
isTerm := isUnixTerm || isCygwinTerm
if !isTerm {
l.headerColor = ColorOff
l.writer.color = ColorOff
}
}

fi, ok := l.writer.w.(hasFD)
if !ok {
return
}

if isatty.IsTerminal(fi.Fd()) {
l.headerColor = ColorOff
l.writer.color = ColorOff
}
}
30 changes: 12 additions & 18 deletions colorize_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,19 @@ import (
// to appropriately configure colorization options. It provides
// a wrapper to the output stream on Windows systems.
func (l *intLogger) setColorization(opts *LoggerOptions) {
switch opts.Color {
case ColorOff:
if opts.Color == ColorOff {
return
case ForceColor:
fi := l.checkWriterIsFile()
l.writer.w = colorable.NewColorable(fi)
case AutoColor:
fi := l.checkWriterIsFile()
isUnixTerm := isatty.IsTerminal(os.Stdout.Fd())
isCygwinTerm := isatty.IsCygwinTerminal(os.Stdout.Fd())
isTerm := isUnixTerm || isCygwinTerm
if !isTerm {
l.writer.color = ColorOff
l.headerColor = ColorOff
return
}
}

cfi := colorable.NewColorable(fi)

if l.headerColor == ColorOff {
l.writer.w = colorable.NewColorable(fi)
}
// NewColorable detects if color is possible and if it's not, then it
// returns the original value. So we can test if we got the original
// value back to know if color is possible.
if cfi == fi {
l.writer.color = ColorOff
l.headerColor = ColorOff
} else {
l.writer.w = cfi
}
}
11 changes: 0 additions & 11 deletions intlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"io"
"log"
"os"
"reflect"
"runtime"
"sort"
Expand Down Expand Up @@ -876,16 +875,6 @@ func (l *intLogger) StandardWriter(opts *StandardLoggerOptions) io.Writer {
}
}

// checks if the underlying io.Writer is a file, and
// panics if not. For use by colorization.
func (l *intLogger) checkWriterIsFile() *os.File {
fi, ok := l.writer.w.(*os.File)
if !ok {
panic("Cannot enable coloring of non-file Writers")
}
return fi
}

// Accept implements the SinkAdapter interface
func (i *intLogger) Accept(name string, level Level, msg string, args ...interface{}) {
i.log(name, level, msg, args...)
Expand Down
7 changes: 7 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ const (
ForceColor
)

// SupportsColor is an optional interface that can be implemented by the output
// value. If implemented and SupportsColor() returns true, then AutoColor will
// enable colorization.
type SupportsColor interface {
SupportsColor() bool
}

// LevelFromString returns a Level type for the named log level, or "NoLevel" if
// the level string is invalid. This facilitates setting the log level via
// config or environment variable by name in a predictable way.
Expand Down

0 comments on commit 7445e89

Please sign in to comment.