diff --git a/cmd/crowdsec-cli/tables.go b/cmd/crowdsec-cli/tables.go index 2c3173d0b0be..91536ef62ab0 100644 --- a/cmd/crowdsec-cli/tables.go +++ b/cmd/crowdsec-cli/tables.go @@ -6,7 +6,8 @@ import ( "os" "github.com/aquasecurity/table" - isatty "github.com/mattn/go-isatty" + + "github.com/crowdsecurity/go-cs-lib/cstty" ) func shouldWeColorize() bool { @@ -16,7 +17,7 @@ func shouldWeColorize() bool { if csConfig.Cscli.Color == "no" { return false } - return isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) + return cstty.IsTTY(os.Stdout.Fd()) } func newTable(out io.Writer) *table.Table { diff --git a/go.mod b/go.mod index 340c3e418033..e75ea8916a2b 100644 --- a/go.mod +++ b/go.mod @@ -26,7 +26,7 @@ require ( github.com/cespare/xxhash/v2 v2.2.0 github.com/crowdsecurity/coraza/v3 v3.0.0-20240108124027-a62b8d8e5607 github.com/crowdsecurity/dlog v0.0.0-20170105205344-4fb5f8204f26 - github.com/crowdsecurity/go-cs-lib v0.0.6 + github.com/crowdsecurity/go-cs-lib v0.0.7-0.20240130143103-452318e69ef9 github.com/crowdsecurity/grokky v0.2.1 github.com/crowdsecurity/machineid v1.0.2 github.com/davecgh/go-spew v1.1.1 @@ -60,7 +60,6 @@ require ( github.com/jarcoal/httpmock v1.1.0 github.com/jszwec/csvutil v1.5.1 github.com/lithammer/dedent v1.1.0 - github.com/mattn/go-isatty v0.0.20 github.com/mattn/go-sqlite3 v1.14.16 github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 github.com/nxadm/tail v1.4.8 @@ -157,6 +156,7 @@ require ( github.com/magefile/mage v1.15.0 // indirect github.com/mailru/easyjson v0.7.7 // indirect github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-runewidth v0.0.13 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect diff --git a/go.sum b/go.sum index bd89178b688d..3f7b21043a9a 100644 --- a/go.sum +++ b/go.sum @@ -102,8 +102,8 @@ github.com/crowdsecurity/coraza/v3 v3.0.0-20240108124027-a62b8d8e5607 h1:hyrYw3h github.com/crowdsecurity/coraza/v3 v3.0.0-20240108124027-a62b8d8e5607/go.mod h1:br36fEqurGYZQGit+iDYsIzW0FF6VufMbDzyyLxEuPA= github.com/crowdsecurity/dlog v0.0.0-20170105205344-4fb5f8204f26 h1:r97WNVC30Uen+7WnLs4xDScS/Ex988+id2k6mDf8psU= github.com/crowdsecurity/dlog v0.0.0-20170105205344-4fb5f8204f26/go.mod h1:zpv7r+7KXwgVUZnUNjyP22zc/D7LKjyoY02weH2RBbk= -github.com/crowdsecurity/go-cs-lib v0.0.6 h1:Ef6MylXe0GaJE9vrfvxEdbHb31+JUP1os+murPz7Pos= -github.com/crowdsecurity/go-cs-lib v0.0.6/go.mod h1:8FMKNGsh3hMZi2SEv6P15PURhEJnZV431XjzzBSuf0k= +github.com/crowdsecurity/go-cs-lib v0.0.7-0.20240130143103-452318e69ef9 h1:xHCBAGYm34lFc/brTurSYUn6o4yxvOBX8L8HeUuwydA= +github.com/crowdsecurity/go-cs-lib v0.0.7-0.20240130143103-452318e69ef9/go.mod h1:xA3j72N5Vd+pXKAshebmwNiJn53jOi7CfZhEDrMTJsI= github.com/crowdsecurity/grokky v0.2.1 h1:t4VYnDlAd0RjDM2SlILalbwfCrQxtJSMGdQOR0zwkE4= github.com/crowdsecurity/grokky v0.2.1/go.mod h1:33usDIYzGDsgX1kHAThCbseso6JuWNJXOzRQDGXHtWM= github.com/crowdsecurity/machineid v1.0.2 h1:wpkpsUghJF8Khtmn/tg6GxgdhLA1Xflerh5lirI+bdc= diff --git a/pkg/acquisition/progressbar.go b/pkg/acquisition/progressbar.go index f0e81d618549..83558f79bc49 100644 --- a/pkg/acquisition/progressbar.go +++ b/pkg/acquisition/progressbar.go @@ -4,19 +4,24 @@ import ( "os" "time" - isatty "github.com/mattn/go-isatty" progressbar "github.com/schollz/progressbar/v3" tomb "gopkg.in/tomb.v2" + "github.com/crowdsecurity/go-cs-lib/cstty" + "github.com/crowdsecurity/crowdsec/pkg/types" ) // injectProgressBar will inject a progress bar in the acquisition pipeline if stderr is a terminal func injectProgressBar(output chan types.Event, acquisTomb *tomb.Tomb) chan types.Event { - if !isatty.IsTerminal(os.Stderr.Fd()) && !isatty.IsCygwinTerminal(os.Stderr.Fd()) { + // assume we are logging to stderr + if !cstty.IsTTY(os.Stderr.Fd()) { return output } + // windows may need this + _ = cstty.EnableVirtualTerminalProcessing(os.Stderr.Fd()) + ret := make(chan types.Event) go func() { diff --git a/pkg/types/logging.go b/pkg/types/logging.go index f10ea6417a7a..bb35ff2b13ed 100644 --- a/pkg/types/logging.go +++ b/pkg/types/logging.go @@ -2,34 +2,24 @@ package types import ( "fmt" + "os" "path/filepath" "time" log "github.com/sirupsen/logrus" "gopkg.in/natefinch/lumberjack.v2" + + "github.com/crowdsecurity/go-cs-lib/cslog" + "github.com/crowdsecurity/go-cs-lib/cstty" ) 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) { - const 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" { + switch cfgMode { + case "file": _maxsize := 500 if maxSize != 0 { _maxsize = maxSize @@ -55,13 +45,20 @@ func SetDefaultLoggerConfig(cfgMode string, cfgFolder string, cfgLevel log.Level Compress: _compress, } log.SetOutput(LogOutput) - } else if cfgMode != "stdout" { + logFormatter = &log.TextFormatter{TimestampFormat: time.RFC3339, FullTimestamp: true, ForceColors: forceColors} + case "stdout": + if cstty.IsTTY(os.Stderr.Fd()) { + logFormatter = &cslog.ClearLineFormatter{TextFormatter: log.TextFormatter{TimestampFormat: time.RFC3339, FullTimestamp: true, ForceColors: forceColors}} + } else { + logFormatter = &log.TextFormatter{TimestampFormat: time.RFC3339, FullTimestamp: true, ForceColors: forceColors} + } + default: return fmt.Errorf("log mode '%s' unknown", cfgMode) } - logLevel = cfgLevel - log.SetLevel(logLevel) - logFormatter = &CustomFormatter{TextFormatter: log.TextFormatter{TimestampFormat: time.RFC3339, FullTimestamp: true, ForceColors: forceColors}} + + log.SetLevel(cfgLevel) log.SetFormatter(logFormatter) + return nil }