From c755a0015ed5f96fedf2342fa21b80c2fc47c538 Mon Sep 17 00:00:00 2001 From: marco Date: Fri, 26 Jan 2024 15:16:42 +0100 Subject: [PATCH] Fix time machine output with progress bar (linux only) This has a log formatter that clears each line before printing overflow information. TODO: fix for windows as well --- pkg/types/utils.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/pkg/types/utils.go b/pkg/types/utils.go index e42c36d8a..2844f7c57 100644 --- a/pkg/types/utils.go +++ b/pkg/types/utils.go @@ -13,6 +13,20 @@ var logFormatter log.Formatter var LogOutput *lumberjack.Logger //io.Writer var logLevel log.Level +type CustomFormatter struct { + log.TextFormatter +} + +func (f *CustomFormatter) Format(entry *log.Entry) ([]byte, error) { + // Clear line escape sequence + clearLine := "\r\033[K" + result, err := f.TextFormatter.Format(entry) + if err != nil { + return nil, err + } + return append([]byte(clearLine), result...), nil +} + func SetDefaultLoggerConfig(cfgMode string, cfgFolder string, cfgLevel log.Level, maxSize int, maxFiles int, maxAge int, compress *bool, forceColors bool) error { /*Configure logs*/ if cfgMode == "file" { @@ -46,7 +60,7 @@ func SetDefaultLoggerConfig(cfgMode string, cfgFolder string, cfgLevel log.Level } logLevel = cfgLevel log.SetLevel(logLevel) - logFormatter = &log.TextFormatter{TimestampFormat: time.RFC3339, FullTimestamp: true, ForceColors: forceColors} + logFormatter = &CustomFormatter{TextFormatter: log.TextFormatter{TimestampFormat: time.RFC3339, FullTimestamp: true, ForceColors: forceColors}} log.SetFormatter(logFormatter) return nil }