diff --git a/anvil/anvil.go b/anvil/anvil.go index 73a0bd9f..536c5083 100644 --- a/anvil/anvil.go +++ b/anvil/anvil.go @@ -5,6 +5,7 @@ import ( "context" "errors" "fmt" + "io" "os" "os/exec" "strconv" @@ -26,7 +27,8 @@ type Config struct { type Anvil struct { rpcClient *rpc.Client - log log.Logger + log log.Logger + logFilePath string cfg *Config cmd *exec.Cmd @@ -90,7 +92,12 @@ func (a *Anvil) Start(ctx context.Context) error { anvilPortCh := make(chan uint64) // Handle stdout/stderr - // - TODO: Figure out best way to dump into logger. Some hex isn't showing appropriately + logFile, err := os.CreateTemp("", fmt.Sprintf("anvil-chain-%d-", a.cfg.ChainId)) + if err != nil { + return fmt.Errorf("failed to create temp log file: %w", err) + } + a.logFilePath = logFile.Name() + stdout, err := a.cmd.StdoutPipe() if err != nil { return fmt.Errorf("failed to get handle on stdout: %w", err) @@ -103,9 +110,11 @@ func (a *Anvil) Start(ctx context.Context) error { scanner := bufio.NewScanner(stdout) for scanner.Scan() { txt := scanner.Text() - anvilLog.Info(txt) + if _, err := fmt.Fprintln(logFile, txt); err != nil { + anvilLog.Warn("err piping stdout to log file", "err", err) + } - // scan for port if applicable + // If configured with port 0, extract the port from the log if a.cfg.Port == 0 && strings.HasPrefix(txt, anvilListeningLogStr) { port, err := strconv.ParseInt(strings.Split(txt, ":")[1], 10, 64) if err != nil { @@ -116,9 +125,8 @@ func (a *Anvil) Start(ctx context.Context) error { } }() go func() { - scanner := bufio.NewScanner(stderr) - for scanner.Scan() { - anvilLog.Error(scanner.Text()) + if _, err := io.Copy(logFile, stderr); err != nil { + anvilLog.Warn("err piping stderr to log file", "err", err) } }() @@ -183,11 +191,8 @@ func (a *Anvil) ChainId() uint64 { return a.cfg.ChainId } -func (a *Anvil) EnableLogging() { - var result string - if err := a.rpcClient.Call(&result, "anvil_setLoggingEnabled", true); err != nil { - a.log.Error("failed to enable logging", "error", err) - } +func (a *Anvil) LogPath() string { + return a.logFilePath } func (a *Anvil) WaitUntilReady(ctx context.Context) error { diff --git a/supersim.go b/supersim.go index c3db084a..55e2fa4e 100644 --- a/supersim.go +++ b/supersim.go @@ -98,8 +98,6 @@ func (s *Supersim) Start(ctx context.Context) error { return fmt.Errorf("supersim failed to get ready: %w", err) } - s.EnableLogging() - s.log.Info("supersim is ready") s.log.Info(s.ConfigAsString()) @@ -185,12 +183,6 @@ func (s *Supersim) WaitUntilReady() error { return err } -func (s *Supersim) EnableLogging() { - s.IterateChains(func(chain *anvil.Anvil) { - chain.EnableLogging() - }) -} - func (s *Supersim) IterateChains(fn func(anvil *anvil.Anvil)) { fn(s.l1Anvil) @@ -204,11 +196,11 @@ func (s *Supersim) ConfigAsString() string { fmt.Fprintf(&b, "\nSupersim Config:\n") fmt.Fprintf(&b, "L1:\n") - fmt.Fprintf(&b, " Chain ID: %d RPC: %s\n", s.l1OpSim.ChainId(), s.l1OpSim.Endpoint()) + fmt.Fprintf(&b, " Chain ID: %d RPC: %s LogPath: %s\n", s.l1OpSim.ChainId(), s.l1OpSim.Endpoint(), s.l1Anvil.LogPath()) fmt.Fprintf(&b, "L2:\n") - for _, l2OpSim := range s.l2OpSims { - fmt.Fprintf(&b, " Chain ID: %d RPC: %s\n", l2OpSim.ChainId(), l2OpSim.Endpoint()) + for id, l2OpSim := range s.l2OpSims { + fmt.Fprintf(&b, " Chain ID: %d RPC: %s LogPath: %s\n", l2OpSim.ChainId(), l2OpSim.Endpoint(), s.l2Anvils[id].LogPath()) } return b.String()