Skip to content

Commit

Permalink
anvil logs to a tmp file (#42)
Browse files Browse the repository at this point in the history
* logfile

* remove EnableLogging
  • Loading branch information
hamdiallam authored Jul 11, 2024
1 parent 13aa0ec commit 4093e98
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 23 deletions.
29 changes: 17 additions & 12 deletions anvil/anvil.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"io"
"os"
"os/exec"
"strconv"
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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)
}
}()

Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 3 additions & 11 deletions supersim.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down Expand Up @@ -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)

Expand All @@ -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()
Expand Down

0 comments on commit 4093e98

Please sign in to comment.