2018-02-17 20:12:41 +02:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2019-05-26 23:36:39 +02:00
|
|
|
|
|
|
|
"github.com/fatih/color"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PrintFunc func(io.Writer, string, ...interface{})
|
|
|
|
|
|
|
|
var (
|
|
|
|
Default PrintFunc = color.New(color.Reset).FprintfFunc()
|
2019-11-25 02:07:12 +02:00
|
|
|
Blue PrintFunc = color.New(color.FgBlue).FprintfFunc()
|
|
|
|
Green PrintFunc = color.New(color.FgGreen).FprintfFunc()
|
|
|
|
Cyan PrintFunc = color.New(color.FgCyan).FprintfFunc()
|
|
|
|
Yellow PrintFunc = color.New(color.FgYellow).FprintfFunc()
|
|
|
|
Magenta PrintFunc = color.New(color.FgMagenta).FprintfFunc()
|
|
|
|
Red PrintFunc = color.New(color.FgRed).FprintfFunc()
|
2018-02-17 20:12:41 +02:00
|
|
|
)
|
|
|
|
|
2019-05-26 23:36:39 +02:00
|
|
|
// Logger is just a wrapper that prints stuff to STDOUT or STDERR,
|
|
|
|
// with optional color.
|
2018-02-17 20:12:41 +02:00
|
|
|
type Logger struct {
|
|
|
|
Stdout io.Writer
|
|
|
|
Stderr io.Writer
|
|
|
|
Verbose bool
|
2019-07-07 19:13:53 +02:00
|
|
|
Color bool
|
2018-02-17 20:12:41 +02:00
|
|
|
}
|
|
|
|
|
2019-05-26 23:36:39 +02:00
|
|
|
// Outf prints stuff to STDOUT.
|
|
|
|
func (l *Logger) Outf(print PrintFunc, s string, args ...interface{}) {
|
2018-02-17 20:12:41 +02:00
|
|
|
if len(args) == 0 {
|
|
|
|
s, args = "%s", []interface{}{s}
|
|
|
|
}
|
2019-07-07 19:13:53 +02:00
|
|
|
if !l.Color {
|
|
|
|
print = Default
|
|
|
|
}
|
2019-05-26 23:36:39 +02:00
|
|
|
print(l.Stdout, s+"\n", args...)
|
2018-02-17 20:12:41 +02:00
|
|
|
}
|
|
|
|
|
2019-05-26 23:36:39 +02:00
|
|
|
// VerboseOutf prints stuff to STDOUT if verbose mode is enabled.
|
|
|
|
func (l *Logger) VerboseOutf(print PrintFunc, s string, args ...interface{}) {
|
2018-02-17 20:12:41 +02:00
|
|
|
if l.Verbose {
|
2019-05-26 23:36:39 +02:00
|
|
|
l.Outf(print, s, args...)
|
2018-02-17 20:12:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 23:36:39 +02:00
|
|
|
// Errf prints stuff to STDERR.
|
|
|
|
func (l *Logger) Errf(print PrintFunc, s string, args ...interface{}) {
|
2018-02-17 20:12:41 +02:00
|
|
|
if len(args) == 0 {
|
|
|
|
s, args = "%s", []interface{}{s}
|
|
|
|
}
|
2019-07-07 19:13:53 +02:00
|
|
|
if !l.Color {
|
|
|
|
print = Default
|
|
|
|
}
|
2019-05-26 23:36:39 +02:00
|
|
|
print(l.Stderr, s+"\n", args...)
|
2018-02-17 20:12:41 +02:00
|
|
|
}
|
|
|
|
|
2019-05-26 23:36:39 +02:00
|
|
|
// VerboseErrf prints stuff to STDERR if verbose mode is enabled.
|
|
|
|
func (l *Logger) VerboseErrf(print PrintFunc, s string, args ...interface{}) {
|
2018-02-17 20:12:41 +02:00
|
|
|
if l.Verbose {
|
2019-05-26 23:36:39 +02:00
|
|
|
l.Errf(print, s, args...)
|
2018-02-17 20:12:41 +02:00
|
|
|
}
|
|
|
|
}
|