Skip to content

Commit 9e4ed9e

Browse files
authored
[cbuild2cmake] Redirect error messages to stderr
1 parent f32fb28 commit 9e4ed9e

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

cmd/cbuild2cmake/main.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@ package main
88

99
import (
1010
"fmt"
11+
"io"
1112
"os"
1213

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

1516
log "github.com/sirupsen/logrus"
17+
"github.com/sirupsen/logrus/hooks/writer"
1618
)
1719

1820
func main() {
1921
log.SetFormatter(new(LogFormatter))
20-
log.SetOutput(os.Stdout)
22+
log.SetOutput(io.Discard)
23+
initLogHooks()
2124

2225
if len(version) == 0 {
2326
version = "0.0.0-debug"
@@ -39,3 +42,22 @@ func (s *LogFormatter) Format(entry *log.Entry) ([]byte, error) {
3942
msg := fmt.Sprintf("%s cbuild2cmake: %s\n", entry.Level.String(), entry.Message)
4043
return []byte(msg), nil
4144
}
45+
46+
func initLogHooks() {
47+
log.AddHook(&writer.Hook{ // Send logs with level higher than warning to stderr
48+
Writer: os.Stderr,
49+
LogLevels: []log.Level{
50+
log.PanicLevel,
51+
log.FatalLevel,
52+
log.ErrorLevel,
53+
log.WarnLevel,
54+
},
55+
})
56+
log.AddHook(&writer.Hook{ // Send info and debug logs to stdout
57+
Writer: os.Stdout,
58+
LogLevels: []log.Level{
59+
log.InfoLevel,
60+
log.DebugLevel,
61+
},
62+
})
63+
}

0 commit comments

Comments
 (0)