2018-02-17 16:12:41 -02:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2022-06-27 21:47:56 -04:00
|
|
|
"os"
|
|
|
|
"strconv"
|
2019-05-26 18:36:39 -03:00
|
|
|
|
|
|
|
"github.com/fatih/color"
|
|
|
|
)
|
|
|
|
|
2023-03-31 19:13:29 +00:00
|
|
|
type (
|
|
|
|
Color func() PrintFunc
|
|
|
|
PrintFunc func(io.Writer, string, ...any)
|
|
|
|
)
|
2019-05-26 18:36:39 -03:00
|
|
|
|
2022-06-27 21:47:56 -04:00
|
|
|
func Default() PrintFunc {
|
|
|
|
return color.New(envColor("TASK_COLOR_RESET", color.Reset)).FprintfFunc()
|
|
|
|
}
|
2023-03-31 19:13:29 +00:00
|
|
|
|
2022-06-27 21:47:56 -04:00
|
|
|
func Blue() PrintFunc {
|
|
|
|
return color.New(envColor("TASK_COLOR_BLUE", color.FgBlue)).FprintfFunc()
|
|
|
|
}
|
2023-03-31 19:13:29 +00:00
|
|
|
|
2022-06-27 21:47:56 -04:00
|
|
|
func Green() PrintFunc {
|
|
|
|
return color.New(envColor("TASK_COLOR_GREEN", color.FgGreen)).FprintfFunc()
|
|
|
|
}
|
2023-03-31 19:13:29 +00:00
|
|
|
|
2022-06-27 21:47:56 -04:00
|
|
|
func Cyan() PrintFunc {
|
|
|
|
return color.New(envColor("TASK_COLOR_CYAN", color.FgCyan)).FprintfFunc()
|
|
|
|
}
|
2023-03-31 19:13:29 +00:00
|
|
|
|
2022-06-27 21:47:56 -04:00
|
|
|
func Yellow() PrintFunc {
|
|
|
|
return color.New(envColor("TASK_COLOR_YELLOW", color.FgYellow)).FprintfFunc()
|
|
|
|
}
|
2023-03-31 19:13:29 +00:00
|
|
|
|
2022-06-27 21:47:56 -04:00
|
|
|
func Magenta() PrintFunc {
|
|
|
|
return color.New(envColor("TASK_COLOR_MAGENTA", color.FgMagenta)).FprintfFunc()
|
|
|
|
}
|
2023-03-31 19:13:29 +00:00
|
|
|
|
2022-06-27 21:47:56 -04:00
|
|
|
func Red() PrintFunc {
|
|
|
|
return color.New(envColor("TASK_COLOR_RED", color.FgRed)).FprintfFunc()
|
|
|
|
}
|
|
|
|
|
|
|
|
func envColor(env string, defaultColor color.Attribute) color.Attribute {
|
2023-02-17 03:12:44 +03:00
|
|
|
if os.Getenv("FORCE_COLOR") != "" {
|
|
|
|
color.NoColor = false
|
|
|
|
}
|
|
|
|
|
2022-06-27 21:47:56 -04:00
|
|
|
override, err := strconv.Atoi(os.Getenv(env))
|
|
|
|
if err == nil {
|
|
|
|
return color.Attribute(override)
|
|
|
|
}
|
|
|
|
return defaultColor
|
|
|
|
}
|
2018-02-17 16:12:41 -02:00
|
|
|
|
2019-05-26 18:36:39 -03:00
|
|
|
// Logger is just a wrapper that prints stuff to STDOUT or STDERR,
|
|
|
|
// with optional color.
|
2018-02-17 16:12:41 -02:00
|
|
|
type Logger struct {
|
|
|
|
Stdout io.Writer
|
|
|
|
Stderr io.Writer
|
|
|
|
Verbose bool
|
2019-07-07 14:13:53 -03:00
|
|
|
Color bool
|
2018-02-17 16:12:41 -02:00
|
|
|
}
|
|
|
|
|
2019-05-26 18:36:39 -03:00
|
|
|
// Outf prints stuff to STDOUT.
|
2023-03-30 20:03:59 +00:00
|
|
|
func (l *Logger) Outf(color Color, s string, args ...any) {
|
2023-04-27 01:20:06 +01:00
|
|
|
l.FOutf(l.Stdout, color, s, args...)
|
2022-09-29 17:13:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FOutf prints stuff to the given writer.
|
2023-03-30 20:03:59 +00:00
|
|
|
func (l *Logger) FOutf(w io.Writer, color Color, s string, args ...any) {
|
2018-02-17 16:12:41 -02:00
|
|
|
if len(args) == 0 {
|
2023-03-30 20:03:59 +00:00
|
|
|
s, args = "%s", []any{s}
|
2018-02-17 16:12:41 -02:00
|
|
|
}
|
2019-07-07 14:13:53 -03:00
|
|
|
if !l.Color {
|
2021-05-30 22:48:48 -03:00
|
|
|
color = Default
|
2019-07-07 14:13:53 -03:00
|
|
|
}
|
2021-05-30 22:48:48 -03:00
|
|
|
print := color()
|
2022-09-29 17:13:12 +00:00
|
|
|
print(w, s, args...)
|
2018-02-17 16:12:41 -02:00
|
|
|
}
|
|
|
|
|
2019-05-26 18:36:39 -03:00
|
|
|
// VerboseOutf prints stuff to STDOUT if verbose mode is enabled.
|
2023-03-30 20:03:59 +00:00
|
|
|
func (l *Logger) VerboseOutf(color Color, s string, args ...any) {
|
2018-02-17 16:12:41 -02:00
|
|
|
if l.Verbose {
|
2021-05-30 22:48:48 -03:00
|
|
|
l.Outf(color, s, args...)
|
2018-02-17 16:12:41 -02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 18:36:39 -03:00
|
|
|
// Errf prints stuff to STDERR.
|
2023-03-30 20:03:59 +00:00
|
|
|
func (l *Logger) Errf(color Color, s string, args ...any) {
|
2018-02-17 16:12:41 -02:00
|
|
|
if len(args) == 0 {
|
2023-03-30 20:03:59 +00:00
|
|
|
s, args = "%s", []any{s}
|
2018-02-17 16:12:41 -02:00
|
|
|
}
|
2019-07-07 14:13:53 -03:00
|
|
|
if !l.Color {
|
2021-05-30 22:48:48 -03:00
|
|
|
color = Default
|
2019-07-07 14:13:53 -03:00
|
|
|
}
|
2021-05-30 22:48:48 -03:00
|
|
|
print := color()
|
2023-04-27 01:20:06 +01:00
|
|
|
print(l.Stderr, s, args...)
|
2018-02-17 16:12:41 -02:00
|
|
|
}
|
|
|
|
|
2019-05-26 18:36:39 -03:00
|
|
|
// VerboseErrf prints stuff to STDERR if verbose mode is enabled.
|
2023-03-30 20:03:59 +00:00
|
|
|
func (l *Logger) VerboseErrf(color Color, s string, args ...any) {
|
2018-02-17 16:12:41 -02:00
|
|
|
if l.Verbose {
|
2021-05-30 22:48:48 -03:00
|
|
|
l.Errf(color, s, args...)
|
2018-02-17 16:12:41 -02:00
|
|
|
}
|
|
|
|
}
|