Skip to content

Add trace function to Logger trait - #4155

Merged
nekevss merged 4 commits into
boa-dev:mainfrom
jamesthurley:trace-logging
Feb 15, 2025
Merged

nekevss merged 4 commits into
boa-dev:mainfrom
jamesthurley:trace-logging

Conversation

@jamesthurley

@jamesthurley jamesthurley commented Feb 5, 2025 •

Copy link
Copy Markdown
Contributor

This Pull Request implements the discussion in Matrix about trace logging.

It changes the following:

  • Adds a trace function to the Logger trait.
  • Calls that trace function from Console.trace.

For such a small PR, I encountered a few niggles:

1. Conflicting trace method

The Logger trait derives from the Trace trait, which already has a trace function, but is unrelated to trace logging. This meant I had to call the new trace function by doing:

Logger::trace(logger, formatter(args, context)?, &console.state, context)?;

rather than the following which is ambiguous to the compiler:

logger.trace(formatter(args, context)?, &console.state, context)?;

This is unsatisfactory, but I also felt that renaming the new Logger.trace function would make it inconsistent with the surrounding code.

2. Passing the stack trace

In matrix we discussed passing the stack trace into the Logger.trace function.

  • I could pass in the Vec<string> of stack frame code block names, but this feels quite prescriptive with the Console deciding in how the stack trace is formatted.
  • I could pass in a Vec<&CallFrame>, but the borrow checker points out that this means I'd be passing in both a mutable reference to Context and immutable references to the CallFrame instances inside Context.
  • Therefore it seemed to make sense to just allow the Logger.trace implementation to extract the call stack from Context itself, and format the call frames as it sees fit, and it also keeps the trace signature consistent with the other logging functions.

3. Code block names

There were a couple of unexpected things here. If I comment out the .filter(|name| !name.is_empty()) line which I added and run on the following test javascript:

function run() {
  console.trace("Trace from 'run' function.");
  a();
}

function a() {
  b();
}

function b() {
  c();
}

function c() {
  console.trace("Trace from 'c' function.");
}

run();

I get the following output:

Trace from 'run' function.
<main>

Trace from 'c' function.
b
a
run
<main>

Unexpected things:

  1. The blank lines under are caused by a code block with an empty name, which isn't very useful to log.
  2. The function that is calling console.trace isn't included in the stack trace.

I resolved the first by filtering out empty stack frame names, although I didn't love this solution.

I wasn't sure how to resolve the second point.

@jamesthurley jamesthurley changed the title Add trace method to Logger trait Add trace function to Logger trait Feb 5, 2025
@jamesthurley

jamesthurley commented Feb 5, 2025 •

Copy link
Copy Markdown
Contributor Author

I've added a test which checks the tracing output.

I think ideally the assert would be:

    assert_eq!(
        logs,
        indoc! { r#"
            one
            <main>
            two
            b
            a
            <main>
        "# }

But that would currently fail due to point 3.2 above, so I've modified the test to make it pass as I don't know if point 3 is easily addressable.

@codecov

codecov Bot commented Feb 5, 2025 •

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 85.71429% with 2 lines in your changes missing coverage. Please review.

Project coverage is 54.71%. Comparing base (6ddc2b4) to head (6451f4e).
Report is 367 commits behind head on main.

Files with missing lines Patch % Lines
core/runtime/src/console/mod.rs 77.77% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4155      +/-   ##
==========================================
+ Coverage   47.24%   54.71%   +7.46%     
==========================================
  Files         476      492      +16     
  Lines       46892    49402    +2510     
==========================================
+ Hits        22154    27029    +4875     
+ Misses      24738    22373    -2365     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jedel1043

jedel1043 commented Feb 5, 2025 •

Copy link
Copy Markdown
Member
  1. The blank lines under are caused by a code block with an empty name, which isn't very useful to log.
  2. The function that is calling console.trace isn't included in the stack trace.

TBH, those sound like bugs on stack_trace more than anything, so I wouldn't be against removing the filtering and fixing the bug after this PR.

@jamesthurley

Copy link
Copy Markdown
Contributor Author

I had a look at the stack_trace function and it seemed like a straightforward fix. It's only called from Logger.trace internally, and the function signature hasn't changed, so I included the fix in this PR.

@nekevss
nekevss requested a review from a team February 14, 2025 19:07

@jedel1043 jedel1043 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks pretty nice, thank you for the contribution!

@nekevss nekevss left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@nekevss
nekevss added this pull request to the merge queue Feb 15, 2025
Merged via the queue into boa-dev:main with commit 97a40ac Feb 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants