Skip to content

Commit

Permalink
[cbuild2cmake] Redirect error messages to stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
brondani authored Jul 19, 2024
1 parent f32fb28 commit 9e4ed9e
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion cmd/cbuild2cmake/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ package main

import (
"fmt"
"io"
"os"

"github.com/Open-CMSIS-Pack/cbuild2cmake/cmd/cbuild2cmake/commands"

log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/writer"
)

func main() {
log.SetFormatter(new(LogFormatter))
log.SetOutput(os.Stdout)
log.SetOutput(io.Discard)
initLogHooks()

if len(version) == 0 {
version = "0.0.0-debug"
Expand All @@ -39,3 +42,22 @@ func (s *LogFormatter) Format(entry *log.Entry) ([]byte, error) {
msg := fmt.Sprintf("%s cbuild2cmake: %s\n", entry.Level.String(), entry.Message)
return []byte(msg), nil
}

func initLogHooks() {
log.AddHook(&writer.Hook{ // Send logs with level higher than warning to stderr
Writer: os.Stderr,
LogLevels: []log.Level{
log.PanicLevel,
log.FatalLevel,
log.ErrorLevel,
log.WarnLevel,
},
})
log.AddHook(&writer.Hook{ // Send info and debug logs to stdout
Writer: os.Stdout,
LogLevels: []log.Level{
log.InfoLevel,
log.DebugLevel,
},
})
}

0 comments on commit 9e4ed9e

Please sign in to comment.