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

anvil logs to a tmp file #42

Merged
merged 2 commits into from
Jul 11, 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
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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra - at the end?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah this was a mistake, will remove

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah wait no actually the extra - is there becase CreateTemp appends a random string of characters at the end for name collision

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())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i doubt this will happen the way everything is setup but i'm wondering if we should add a nil check for this specifically s.l2Anvils[id].LogPath()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm given the setup there's no good way to gracefully recover from a programming mistake here so better to simply panic

we can add a unit test though that asserts the key is present in both maps when setup. That should cover this case

}

return b.String()
Expand Down
Loading