Skip to content
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

shell logger: Use fmt functions once #2805

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 16 additions & 20 deletions internal/job/shell/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,39 +69,35 @@ func (wl *WriterLogger) Write(b []byte) (int, error) {
}

func (wl *WriterLogger) Printf(format string, v ...any) {
fmt.Fprintf(wl.Writer, "%s", fmt.Sprintf(format, v...))
fmt.Fprintln(wl.Writer)
fmt.Fprintf(wl.Writer, format+"\n", v...)
}

func (wl *WriterLogger) Headerf(format string, v ...any) {
fmt.Fprintf(wl.Writer, "~~~ %s", fmt.Sprintf(format, v...))
fmt.Fprintln(wl.Writer)
fmt.Fprintf(wl.Writer, "~~~ "+format+"\n", v...)
}

func (wl *WriterLogger) Commentf(format string, v ...any) {
if wl.Ansi {
wl.Printf(ansiColor("# %s", "90"), fmt.Sprintf(format, v...))
wl.Printf(ansiColor("# "+format, "90"), v...)
} else {
wl.Printf("# %s", fmt.Sprintf(format, v...))
wl.Printf("# "+format, v...)
}
}

func (wl *WriterLogger) Errorf(format string, v ...any) {
if wl.Ansi {
wl.Printf(ansiColor("🚨 Error: %s", "31"), fmt.Sprintf(format, v...))
wl.Printf(ansiColor("🚨 Error: "+format+"\n^^^ +++", "31"), v...)
} else {
wl.Printf("🚨 Error: %s", fmt.Sprintf(format, v...))
wl.Printf("🚨 Error: "+format+"\n^^^ +++", v...)
}
wl.Printf("^^^ +++")
}

func (wl *WriterLogger) Warningf(format string, v ...any) {
if wl.Ansi {
wl.Printf(ansiColor("⚠️ Warning: %s", "33"), fmt.Sprintf(format, v...))
wl.Printf(ansiColor("⚠️ Warning: "+format+"\n^^^ +++", "33"), v...)
} else {
wl.Printf("⚠️ Warning: %s", fmt.Sprintf(format, v...))
wl.Printf("⚠️ Warning: "+format+"\n^^^ +++", v...)
}
wl.Printf("^^^ +++")
}

func (wl *WriterLogger) OptionalWarningf(id, format string, v ...any) {
Expand All @@ -120,9 +116,9 @@ func (wl *WriterLogger) Promptf(format string, v ...any) {
prompt = ">"
}
if wl.Ansi {
wl.Printf(ansiColor(prompt, "90")+" %s", fmt.Sprintf(format, v...))
wl.Printf(ansiColor(prompt, "90")+" "+format, v...)
} else {
wl.Printf(prompt+" %s", fmt.Sprintf(format, v...))
wl.Printf(prompt+" "+format, v...)
}
}

Expand All @@ -148,15 +144,15 @@ func (tl TestingLogger) Headerf(format string, v ...any) {
}

func (tl TestingLogger) Commentf(format string, v ...any) {
tl.Logf("# %s", fmt.Sprintf(format, v...))
tl.Logf("# "+format, v...)
}

func (tl TestingLogger) Errorf(format string, v ...any) {
tl.Logf("🚨 Error: %s", fmt.Sprintf(format, v...))
tl.Logf("🚨 Error: "+format, v...)
}

func (tl TestingLogger) Warningf(format string, v ...any) {
tl.Logf("⚠️ Warning: %s", fmt.Sprintf(format, v...))
tl.Logf("⚠️ Warning: "+format, v...)
}

func (tl TestingLogger) OptionalWarningf(_id, format string, v ...any) {
Expand All @@ -169,7 +165,7 @@ func (tl TestingLogger) Promptf(format string, v ...any) {
if runtime.GOOS == "windows" {
prompt = ">"
}
tl.Logf(prompt+" %s", fmt.Sprintf(format, v...))
tl.Logf(prompt+" "+format, v...)
}

type LoggerStreamer struct {
Expand All @@ -185,7 +181,7 @@ var lineRegexp = regexp.MustCompile(`(?m:^(.*)\r?\n)`)
func NewLoggerStreamer(logger Logger) *LoggerStreamer {
return &LoggerStreamer{
Logger: logger,
buf: bytes.NewBuffer([]byte("")),
buf: bytes.NewBuffer(nil),
}
}

Expand All @@ -206,7 +202,7 @@ func (l *LoggerStreamer) Close() error {
if remaining := l.buf.String()[l.offset:]; len(remaining) > 0 {
l.Logger.Printf("%s%s", l.Prefix, remaining)
}
l.buf = bytes.NewBuffer([]byte(""))
l.buf = bytes.NewBuffer(nil)
return nil
}

Expand Down
20 changes: 20 additions & 0 deletions internal/job/shell/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package shell_test
import (
"bytes"
"fmt"
"io"
"runtime"
"testing"

Expand Down Expand Up @@ -69,3 +70,22 @@ func TestLoggerStreamer(t *testing.T) {
t.Fatalf("shell.WriterLogger output buffer diff (-got +want):\n%s", diff)
}
}

func BenchmarkDoubleFmt(b *testing.B) {
logf := func(format string, v ...any) {
fmt.Fprintf(io.Discard, "%s", fmt.Sprintf(format, v...))
fmt.Fprintln(io.Discard)
}
for range b.N {
logf("asdfghjkl %s %d %t", "hi", 42, true)
}
}

func BenchmarkFmtConcat(b *testing.B) {
logf := func(format string, v ...any) {
fmt.Fprintf(io.Discard, format+"\n", v...)
}
for range b.N {
logf("asdfghjkl %s %d %t", "hi", 42, true)
}
}