Skip to content

Fix ANSI log styling - #10028

Open
khanak0509 wants to merge 3 commits into
flutter:masterfrom
khanak0509:fix-ansi-log-styling-9521
Open

khanak0509 wants to merge 3 commits into
flutter:masterfrom
khanak0509:fix-ansi-log-styling-9521

Conversation

@khanak0509

Copy link
Copy Markdown
Contributor

Fixes #9521

this PR fixes issue #9521 where logs with ANSI colors were not shown properly in the Logging screen. If a log had only a background color, the text became black, so with a black background it was fully invisible in dark theme. Colored text was also bigger than the normal text, and the Details panel showed the raw escape codes like [45m and [0m.

How I fixed it:
textSpansFromAnsi was creating a new TextStyle for colored spans and dropping the default style, so the color became null (black) and the font size fell back to default. Now it uses defaultStyle.copyWith(...), so colored spans keep the theme color and font size and only change what the ANSI code sets.
For the Details panel, I added a stripAnsi() helper and used it in LogData.prettyPrinted(), so the details text, search and copy all get clean text. For dart:developer logs the details are JSON, and JSON encodes the escape char as \u001b, so I handle that too.

before --
Screenshot 2026-10-02 at 3 35 02 AM

after --
Screenshot 2026-10-02 at 3 44 54 AM
Screenshot 2026-10-02 at 4 04 29 AM

Pre-launch Checklist

General checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read the Flutter Style Guide recently, and have followed its advice.
  • I signed the CLA.
  • I updated/added relevant documentation (doc comments with ///).

Issues checklist

Tests checklist

  • I added new tests to check the change I am making...
  • OR there is a reason for not adding tests, which I explained in the PR description.

AI-tooling checklist

  • I did not use any AI tooling in creating this PR.
  • OR I did use AI tooling, and...
    • I read the AI contributions guidelines and agree to follow them.
    • I reviewed all AI-generated code before opening this PR.
    • I understand and am able to discuss the code in this PR.
    • I have verifed the accuracy of any AI-generated text included in the PR description.
    • I commit to verifying the accuracy of any AI-generated code or text that I upload in response to review comments.

Feature-change checklist

  • This PR does not change the DevTools UI or behavior and...
    • I added the release-notes-not-required label or left a comment requesting the label be added.
  • OR this PR does change the DevTools UI or behavior and...
    • I added an entry to packages/devtools_app/release_notes/NEXT_RELEASE_NOTES.md.
    • I included before/after screenshots and/or a GIF demo of the new UI to my PR description.
    • I ran the DevTools app locally to manually verify my changes.

build.yaml badge

If you need help, consider asking for help on Discord.

@khanak0509
khanak0509 requested a review from a team as a code owner October 1, 2026 22:40
@khanak0509
khanak0509 requested review from srawlins and removed request for a team October 1, 2026 22:40

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request adds support for stripping ANSI escape codes from pretty-printed log details and refactors textSpansFromAnsi to preserve default text styles. It also includes comprehensive unit tests for these new behaviors. The review feedback recommends caching the computed result of prettyPrinted() to prevent redundant, expensive operations on the UI thread, ensuring better performance and consistency.

Comment on lines 1098 to +1105
String? prettyPrinted() {
final text = _prettyPrintedWithAnsi();
if (text == null) return null;
// JSON encoding escapes the ANSI escape character as `\u001b`.
return stripAnsi(text.replaceAll(_jsonEscapedAnsiEscape, '\x1B'));
}

static final _jsonEscapedAnsiEscape = RegExp(r'\\u001[bB]');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

[CONCERN] To avoid performance issues (such as UI jank or lag) when rendering large log details or when the user repeatedly triggers redraws, search, or copy actions, we should cache the result of prettyPrinted() once the details have been fully computed. This is consistent with how _encodedDetails is cached in the same class.

  String? prettyPrinted() {
    if (_prettyPrinted != null) return _prettyPrinted;
    final text = _prettyPrintedWithAnsi();
    if (text == null) return null;
    // JSON encoding escapes the ANSI escape character as \u001b.
    final result = stripAnsi(text.replaceAll(_jsonEscapedAnsiEscape, '\x1B'));
    if (detailsComputed.isCompleted) {
      _prettyPrinted = result;
    }
    return result;
  }

  String? _prettyPrinted;

  static final _jsonEscapedAnsiEscape = RegExp(r'\\u001[bB]');
References
  1. Prioritize logic, performance on the UI thread, and architectural consistency. Caching computed results avoids redundant heavy operations (like JSON decoding, regex replacement, and ANSI parsing) on the UI thread. (link)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

good catch :)

This branch has not been deployed

No deployments
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.

ANSI colors on DevTools log

1 participant