mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
internal/debug: make sure non-terminal output is closed (whether file or lumberjack)
This commit is contained in:
parent
c5cd6fe922
commit
9f616add5f
1 changed files with 12 additions and 8 deletions
|
|
@ -167,9 +167,14 @@ var Flags = []cli.Flag{
|
||||||
traceFlag,
|
traceFlag,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type writerCloser interface {
|
||||||
|
Write([]byte) (int, error)
|
||||||
|
Close() error
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
glogger *log.GlogHandler
|
glogger *log.GlogHandler
|
||||||
logOutputF *os.File
|
logOutputF writerCloser
|
||||||
defaultTerminalHandler *log.TerminalHandler
|
defaultTerminalHandler *log.TerminalHandler
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -191,7 +196,6 @@ func ResetLogging() {
|
||||||
func Setup(ctx *cli.Context) error {
|
func Setup(ctx *cli.Context) error {
|
||||||
var (
|
var (
|
||||||
handler slog.Handler
|
handler slog.Handler
|
||||||
fileOutput io.Writer
|
|
||||||
terminalOutput = io.Writer(os.Stderr)
|
terminalOutput = io.Writer(os.Stderr)
|
||||||
output io.Writer
|
output io.Writer
|
||||||
logFmtFlag = ctx.String(logFormatFlag.Name)
|
logFmtFlag = ctx.String(logFormatFlag.Name)
|
||||||
|
|
@ -219,20 +223,20 @@ func Setup(ctx *cli.Context) error {
|
||||||
} else {
|
} else {
|
||||||
context = append(context, "location", filepath.Join(os.TempDir(), "geth-lumberjack.log"))
|
context = append(context, "location", filepath.Join(os.TempDir(), "geth-lumberjack.log"))
|
||||||
}
|
}
|
||||||
fileOutput = &lumberjack.Logger{
|
logOutputF = &lumberjack.Logger{
|
||||||
Filename: logFile,
|
Filename: logFile,
|
||||||
MaxSize: ctx.Int(logMaxSizeMBsFlag.Name),
|
MaxSize: ctx.Int(logMaxSizeMBsFlag.Name),
|
||||||
MaxBackups: ctx.Int(logMaxBackupsFlag.Name),
|
MaxBackups: ctx.Int(logMaxBackupsFlag.Name),
|
||||||
MaxAge: ctx.Int(logMaxAgeFlag.Name),
|
MaxAge: ctx.Int(logMaxAgeFlag.Name),
|
||||||
Compress: ctx.Bool(logCompressFlag.Name),
|
Compress: ctx.Bool(logCompressFlag.Name),
|
||||||
}
|
}
|
||||||
output = io.MultiWriter(terminalOutput, fileOutput)
|
output = io.MultiWriter(terminalOutput, logOutputF)
|
||||||
} else if logFile != "" {
|
} else if logFile != "" {
|
||||||
var err error
|
var err error
|
||||||
if fileOutput, err = os.OpenFile(logFile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644); err != nil {
|
if logOutputF, err = os.OpenFile(logFile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
output = io.MultiWriter(fileOutput, terminalOutput)
|
output = io.MultiWriter(logOutputF, terminalOutput)
|
||||||
context = append(context, "location", logFile)
|
context = append(context, "location", logFile)
|
||||||
} else {
|
} else {
|
||||||
output = terminalOutput
|
output = terminalOutput
|
||||||
|
|
@ -251,8 +255,8 @@ func Setup(ctx *cli.Context) error {
|
||||||
useColor := (isatty.IsTerminal(os.Stderr.Fd()) || isatty.IsCygwinTerminal(os.Stderr.Fd())) && os.Getenv("TERM") != "dumb"
|
useColor := (isatty.IsTerminal(os.Stderr.Fd()) || isatty.IsCygwinTerminal(os.Stderr.Fd())) && os.Getenv("TERM") != "dumb"
|
||||||
if useColor {
|
if useColor {
|
||||||
terminalOutput = colorable.NewColorableStderr()
|
terminalOutput = colorable.NewColorableStderr()
|
||||||
if fileOutput != nil {
|
if logOutputF != nil {
|
||||||
output = io.MultiWriter(fileOutput, terminalOutput)
|
output = io.MultiWriter(logOutputF, terminalOutput)
|
||||||
} else {
|
} else {
|
||||||
output = terminalOutput
|
output = terminalOutput
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue