diff --git a/cannon/cmd/log.go b/cannon/cmd/log.go index 04e168b9ecfb..30e92e3e4007 100644 --- a/cannon/cmd/log.go +++ b/cannon/cmd/log.go @@ -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 } diff --git a/cannon/cmd/run.go b/cannon/cmd/run.go index afdd79f1ddb5..4c0970e3a99c 100644 --- a/cannon/cmd/run.go +++ b/cannon/cmd/run.go @@ -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 } @@ -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(), @@ -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 @@ -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) } diff --git a/cannon/mipsevm/logw.go b/cannon/mipsevm/logw.go index 2622e05068d4..e017ee5f8e30 100644 --- a/cannon/mipsevm/logw.go +++ b/cannon/mipsevm/logw.go @@ -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 {