Skip to content

Commit

Permalink
cannon: Cleanup logger (ethereum-optimism#10700)
Browse files Browse the repository at this point in the history
- Label log sources
- Avoid adding duplicate slog fields when not isatty
  • Loading branch information
Inphi authored May 31, 2024
1 parent e81a35a commit a533f52
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
29 changes: 28 additions & 1 deletion cannon/cmd/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,39 @@ package cmd

import (
"io"
"os"

"golang.org/x/exp/slog"
"golang.org/x/term"

"github.com/ethereum/go-ethereum/log"
)

func Logger(w io.Writer, lvl slog.Level) log.Logger {
return log.NewLogger(log.LogfmtHandlerWithLevel(w, lvl))
if term.IsTerminal(int(os.Stdout.Fd())) {
return log.NewLogger(log.LogfmtHandlerWithLevel(w, lvl))
} else {
return log.NewLogger(rawLogHandler(w, lvl))
}
}

// rawLogHandler returns a handler that strips out the time attribute
func rawLogHandler(wr io.Writer, lvl slog.Level) slog.Handler {
return slog.NewTextHandler(wr, &slog.HandlerOptions{
ReplaceAttr: replaceAttr,
Level: &leveler{lvl},
})
}

type leveler struct{ minLevel slog.Level }

func (l *leveler) Level() slog.Level {
return l.minLevel
}

func replaceAttr(_ []string, attr slog.Attr) slog.Attr {
if attr.Key == slog.TimeKey {
return slog.Attr{}
}
return attr
}
18 changes: 11 additions & 7 deletions cannon/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ type ProcessPreimageOracle struct {

const clientPollTimeout = time.Second * 15

func NewProcessPreimageOracle(name string, args []string) (*ProcessPreimageOracle, error) {
func NewProcessPreimageOracle(name string, args []string, stdout log.Logger, stderr log.Logger) (*ProcessPreimageOracle, error) {
if name == "" {
return &ProcessPreimageOracle{}, nil
}
Expand All @@ -158,8 +158,8 @@ func NewProcessPreimageOracle(name string, args []string) (*ProcessPreimageOracl
}

cmd := exec.Command(name, args...) // nosemgrep
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdout = &mipsevm.LoggingWriter{Log: stdout}
cmd.Stderr = &mipsevm.LoggingWriter{Log: stderr}
cmd.ExtraFiles = []*os.File{
hOracleRW.Reader(),
hOracleRW.Writer(),
Expand Down Expand Up @@ -254,9 +254,11 @@ func Run(ctx *cli.Context) error {
return err
}

l := Logger(os.Stderr, log.LevelInfo)
outLog := &mipsevm.LoggingWriter{Name: "program std-out", Log: l}
errLog := &mipsevm.LoggingWriter{Name: "program std-err", Log: l}
guestLogger := Logger(os.Stderr, log.LevelInfo)
outLog := &mipsevm.LoggingWriter{Log: guestLogger.With("module", "guest", "stream", "stdout")}
errLog := &mipsevm.LoggingWriter{Log: guestLogger.With("module", "guest", "stream", "stderr")}

l := Logger(os.Stderr, log.LevelInfo).With("module", "vm")

stopAtAnyPreimage := false
var stopAtPreimageKeyPrefix []byte
Expand Down Expand Up @@ -309,7 +311,9 @@ func Run(ctx *cli.Context) error {
args = []string{""}
}

po, err := NewProcessPreimageOracle(args[0], args[1:])
poOut := Logger(os.Stdout, log.LevelInfo).With("module", "host")
poErr := Logger(os.Stderr, log.LevelInfo).With("module", "host")
po, err := NewProcessPreimageOracle(args[0], args[1:], poOut, poErr)
if err != nil {
return fmt.Errorf("failed to create pre-image oracle process: %w", err)
}
Expand Down
3 changes: 1 addition & 2 deletions cannon/mipsevm/logw.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import (
// and expose an io Writer interface,
// for the program running within the VM to write to.
type LoggingWriter struct {
Name string
Log log.Logger
Log log.Logger
}

func logAsText(b string) bool {
Expand Down

0 comments on commit a533f52

Please sign in to comment.