all: disable log message colors outside of geth

Also tweak behaviour so colors are only enabled when stderr is a terminal.
This commit is contained in:
Felix Lange
2017-02-23 19:31:13 +01:00
parent 43362ab4fb
commit d0eba23af3
11 changed files with 38 additions and 27 deletions

View File

@ -18,12 +18,15 @@ package debug
import (
"fmt"
"io"
"net/http"
_ "net/http/pprof"
"os"
"runtime"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/log/term"
colorable "github.com/mattn/go-colorable"
"gopkg.in/urfave/cli.v1"
)
@ -87,16 +90,22 @@ var Flags = []cli.Flag{
memprofilerateFlag, blockprofilerateFlag, cpuprofileFlag, traceFlag,
}
// glogger is the glog handler used by Geth, allowing the debug APIs to modify
// verbosity levels, vmodules and backtrace locations.
var glogger = log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat()))
var glogger *log.GlogHandler
func init() {
usecolor := term.IsTty(os.Stderr.Fd()) && os.Getenv("TERM") != "dumb"
output := io.Writer(os.Stderr)
if usecolor {
output = colorable.NewColorableStderr()
}
glogger = log.NewGlogHandler(log.StreamHandler(output, log.TerminalFormat(usecolor)))
}
// Setup initializes profiling and logging based on the CLI flags.
// It should be called as early as possible in the program.
func Setup(ctx *cli.Context) error {
// logging
log.PrintOrigins(ctx.GlobalBool(debugFlag.Name))
glogger.Verbosity(log.Lvl(ctx.GlobalInt(verbosityFlag.Name)))
glogger.Vmodule(ctx.GlobalString(vmoduleFlag.Name))
glogger.BacktraceAt(ctx.GlobalString(backtraceAtFlag.Name))