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

Pretty printed logs and events in cast run #8207

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
35 changes: 26 additions & 9 deletions crates/evm/traces/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,18 @@ pub async fn render_trace_arena(

// Display trace header
let (trace, return_data) = render_trace(&node.trace, decoder).await?;
writeln!(s, "{left}{trace}")?;

// Prepend our tree structure symbols to each line of the displayed trace
let call_left_prefix = left.to_string();
let call_right_prefix = format!("{child} ");
trace.lines().enumerate().try_for_each(|(i, line)| {
writeln!(
s,
"{}{}",
if i == 0 { &call_left_prefix } else { &call_right_prefix },
line
)
})?;

// Display logs and subcalls
let left_prefix = format!("{child}{BRANCH}");
Expand Down Expand Up @@ -174,7 +185,7 @@ pub async fn render_trace(
let (func_name, inputs) = match &decoded.func {
Some(DecodedCallData { signature, args }) => {
let name = signature.split('(').next().unwrap();
(name.to_string(), args.join(", "))
(name.to_string(), args.join(",\n "))
}
None => {
debug!(target: "evm::traces", trace=?trace, "unhandled raw calldata");
Expand All @@ -197,9 +208,10 @@ pub async fn render_trace(
};

let color = trace_color(trace);
let inputs_padded = if !inputs.is_empty() { format!("\n {inputs}\n") } else { inputs };
write!(
&mut s,
"{addr}::{func_name}{opt_value}({inputs}){action}",
"{addr}::{func_name}{opt_value}({inputs_padded}){action}",
addr = decoded.label.as_deref().unwrap_or(&address).fg(color),
func_name = func_name.fg(color),
opt_value = if trace.value.is_zero() {
Expand Down Expand Up @@ -227,22 +239,27 @@ async fn render_trace_log(
for (i, topic) in log.topics().iter().enumerate() {
writeln!(
s,
"{:>13}: {}",
if i == 0 { "emit topic 0".to_string() } else { format!("topic {i}") },
"{}: {}",
if i == 0 { "emit topic 0".to_string() } else { format!(" topic {i}") },
format!("{topic:?}").cyan()
)?;
}

write!(s, " data: {}", hex::encode_prefixed(&log.data).cyan())?;
write!(s, " data: {}", hex::encode_prefixed(&log.data).cyan())?;
}
DecodedCallLog::Decoded(name, params) => {
let params = params
.iter()
.map(|(name, value)| format!("{name}: {value}"))
.map(|(name, value)| format!(" {name}: {value}"))
.collect::<Vec<String>>()
.join(", ");
.join(",\n");

write!(s, "emit {}({params})", name.cyan())?;
let pretty_name = name.cyan();
if !params.is_empty() {
write!(s, "emit {pretty_name}(\n{params}\n )")?;
} else {
write!(s, "emit {pretty_name}()")?;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ Traces:
[PASS] testEvent() (gas: 1312)
Traces:
[1312] CustomTypesTest::testEvent()
├─ emit MyEvent(a: 100)
├─ emit MyEvent(
│ a: 100
│ )
└─ ← [Stop]

Suite result: FAILED. 1 passed; 1 failed; 0 skipped; finished in 3.88ms
Expand Down
4 changes: 3 additions & 1 deletion crates/forge/tests/fixtures/repro_6531.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ Ran 1 test for test/Contract.t.sol:USDTCallingTest
[PASS] test() (gas: 9559)
Traces:
[9559] USDTCallingTest::test()
├─ [0] VM::createSelectFork("<url>")
├─ [0] VM::createSelectFork(
│ "<url>"
│ )
│ └─ ← [Return] 0
├─ [3110] 0xdAC17F958D2ee523a2206206994597C13D831ec7::name() [staticcall]
│ └─ ← [Return] "Tether USD"
Expand Down