8000 [breaking] legacy: refactoring of the old `Logger` (part 1 of 2) by cmaglie · Pull Request #1621 · arduino/arduino-cli · GitHub
[go: up one dir, main page]

Skip to content

[breaking] legacy: refactoring of the old Logger (part 1 of 2) #1621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jan 11, 2022
Merged
Prev Previous commit
Next Next commit
Removed Logger.Name method (use type-assertions instead)
  • Loading branch information
cmaglie committed Jan 10, 2022
commit b54e7e4f4c7aeca30cd36e4b00611aa20b5ec3ac
2 changes: 1 addition & 1 deletion legacy/builder/i18n/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func ErrorfWithLogger(logger Logger, format string, a ...interface{}) error {
if logger.Name() == "machine" {
if _, isMachineLogger := logger.(*MachineLogger); isMachineLogger {
logger.Fprintln(os.Stderr, constants.LOG_LEVEL_ERROR, format, a...)
return errors.New("")
}
Expand Down
37 changes: 8 additions & 29 deletions legacy/builder/i18n/i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ var PLACEHOLDER = regexp.MustCompile(`{(\d)}`)
type Logger interface {
Fprintln(w io.Writer, level string, format string, a ...interface{})
Println(level string, format string, a ...interface{})
Name() string
}

type LoggerToCustomStreams struct {
Expand All @@ -54,63 +53,43 @@ func (s *LoggerToCustomStreams) Println(level string, format string, a ...interf
s.Fprintln(nil, level, format, a...)
}

func (s *LoggerToCustomStreams) Name() string {
return "LoggerToCustomStreams"
}

type NoopLogger struct{}

func (s NoopLogger) Fprintln(w io.Writer, level string, format string, a ...interface{}) {}
func (s *NoopLogger) Fprintln(w io.Writer, level string, format string, a ...interface{}) {}

func (s NoopLogger) Println(level string, format string, a ...interface{}) {}

func (s NoopLogger) Name() string {
return "noop"
}
func (s *NoopLogger) Println(level string, format string, a ...interface{}) {}

type HumanTagsLogger struct{}

func (s HumanTagsLogger) Fprintln(w io.Writer, level string, format string, a ...interface{}) {
func (s *HumanTagsLogger) Fprintln(w io.Writer, level string, format string, a ...interface{}) {
format = "[" + level + "] " + format
fprintln(w, Format(format, a...))
}

func (s HumanTagsLogger) Println(level string, format string, a ...interface{}) {
func (s *HumanTagsLogger) Println(level string, format string, a ...interface{}) {
s.Fprintln(os.Stdout, level, format, a...)
}

func (s HumanTagsLogger) Name() string {
return "humantags"
}

type HumanLogger struct{}

func (s HumanLogger) Fprintln(w io.Writer, level string, format string, a ...interface{}) {
func (s *HumanLogger) Fprintln(w io.Writer, level string, format string, a ...interface{}) {
fprintln(w, Format(format, a...))
}

func (s HumanLogger) Println(level string, format string, a ...interface{}) {
func (s *HumanLogger) Println(level string, format string, a ...interface{}) {
s.Fprintln(os.Stdout, level, format, a...)
}

func (s HumanLogger) Name() string {
return "human"
}

type MachineLogger struct{}

func (s MachineLogger) Fprintln(w io.Writer, level string, format string, a ...interface{}) {
func (s *MachineLogger) Fprintln(w io.Writer, level string, format string, a ...interface{}) {
printMachineFormattedLogLine(w, level, format, a)
}

func (s MachineLogger) Println(level string, format string, a ...interface{}) {
func (s *MachineLogger) Println(level string, format string, a ...interface{}) {
printMachineFormattedLogLine(os.Stdout, level, format, a)
}

func (s MachineLogger) Name() string {
return "machine"
}

func printMachineFormattedLogLine(w io.Writer, level string, format string, a []interface{}) {
a = append([]interface{}(nil), a...)
for idx, value := range a {
Expand Down
7 changes: 4 additions & 3 deletions legacy/builder/test/i18n_test.go
46E4
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ package test

import (
"fmt"
"testing"

"github.com/arduino/arduino-cli/legacy/builder/constants"
"github.com/arduino/arduino-cli/legacy/builder/i18n"
"github.com/stretchr/testify/require"
"testing"
)

func TestI18NSyntax(t *testing.T) {
Expand Down Expand Up @@ -50,9 +51,9 @@ func TestI18NSyntax(t *testing.T) {

func TestI18NInheritance(t *testing.T) {
var logger i18n.Logger
logger = i18n.HumanLogger{}
logger = &i18n.HumanLogger{}
logger.Println(constants.LOG_LEVEL_INFO, "good {0} {1}", "morning", "vietnam!")

logger = i18n.MachineLogger{}
logger = &i18n.MachineLogger{}
logger.Println(constants.LOG_LEVEL_INFO, "good {0} {1}", "morning", "vietnam!")
}
0