summaryrefslogtreecommitdiff
path: root/internal/toggledlog
diff options
context:
space:
mode:
authorJakob Unterwurzacher2016-06-15 23:17:51 +0200
committerJakob Unterwurzacher2016-06-15 23:17:51 +0200
commit09e88f31d1b4308bf82c7f4eb0dd83855ad67ffc (patch)
treee91a7ea9669fae9c633671ca5ca722fb97f2c254 /internal/toggledlog
parentc89455063cfd9c531c0a671251ccfcd46f09403d (diff)
toggledlog: assume command of color handling
We want to use colored error messages also outside of main, so let's handle it in the logging package. The fatal logger now automatically prints red.
Diffstat (limited to 'internal/toggledlog')
-rw-r--r--internal/toggledlog/log.go45
1 files changed, 38 insertions, 7 deletions
diff --git a/internal/toggledlog/log.go b/internal/toggledlog/log.go
index 23d7347..30fab18 100644
--- a/internal/toggledlog/log.go
+++ b/internal/toggledlog/log.go
@@ -5,6 +5,8 @@ import (
"fmt"
"log"
"os"
+
+ "golang.org/x/crypto/ssh/terminal"
)
const (
@@ -12,6 +14,10 @@ const (
wpanicMsg = "-wpanic turns this warning into a panic: "
)
+// Escape sequences for terminal colors. These will be empty strings if stdout
+// is not a terminal.
+var ColorReset, ColorGrey, ColorRed, ColorGreen, ColorYellow string
+
func JSONDump(obj interface{}) string {
b, err := json.MarshalIndent(obj, "", "\t")
if err != nil {
@@ -27,6 +33,10 @@ type toggledLogger struct {
Enabled bool
// Panic after logging a message, useful in regression tests
Wpanic bool
+ // Private prefix and postfix are used for coloring
+ prefix string
+ postfix string
+
*log.Logger
}
@@ -34,7 +44,7 @@ func (l *toggledLogger) Printf(format string, v ...interface{}) {
if !l.Enabled {
return
}
- l.Logger.Printf(format, v...)
+ l.Logger.Printf(l.prefix + fmt.Sprintf(format, v...) + l.postfix)
if l.Wpanic {
l.Logger.Panic(wpanicMsg + fmt.Sprintf(format, v...))
}
@@ -43,9 +53,9 @@ func (l *toggledLogger) Println(v ...interface{}) {
if !l.Enabled {
return
}
- l.Logger.Println(v...)
+ l.Logger.Println(l.prefix + fmt.Sprint(v...) + l.postfix)
if l.Wpanic {
- l.Logger.Panic(wpanicMsg + fmt.Sprintln(v...))
+ l.Logger.Panic(wpanicMsg + fmt.Sprint(v...))
}
}
@@ -65,8 +75,29 @@ var Warn *toggledLogger
var Fatal *toggledLogger
func init() {
- Debug = &toggledLogger{false, false, log.New(os.Stdout, "", 0)}
- Info = &toggledLogger{true, false, log.New(os.Stdout, "", 0)}
- Warn = &toggledLogger{true, false, log.New(os.Stderr, "", 0)}
- Fatal = &toggledLogger{true, false, log.New(os.Stderr, "", 0)}
+ if terminal.IsTerminal(int(os.Stdout.Fd())) {
+ ColorReset = "\033[0m"
+ ColorGrey = "\033[2m"
+ ColorRed = "\033[31m"
+ ColorGreen = "\033[32m"
+ ColorYellow = "\033[33m"
+ }
+
+ Debug = &toggledLogger{
+ Logger: log.New(os.Stdout, "", 0),
+ }
+ Info = &toggledLogger{
+ Enabled: true,
+ Logger: log.New(os.Stdout, "", 0),
+ }
+ Warn = &toggledLogger{
+ Enabled: true,
+ Logger: log.New(os.Stderr, "", 0),
+ }
+ Fatal = &toggledLogger{
+ Enabled: true,
+ Logger: log.New(os.Stderr, "", 0),
+ prefix: ColorRed,
+ postfix: ColorReset,
+ }
}