Fix ANSI log styling (#10028)

* Fix ANSI log styling

* Add release note

* review

---------

Co-authored-by: Sam Rawlins <srawlins@google.com>
diff --git a/packages/devtools_app/lib/src/screens/logging/logging_controller.dart b/packages/devtools_app/lib/src/screens/logging/logging_controller.dart
index d0ca370..0737a5a 100644
--- a/packages/devtools_app/lib/src/screens/logging/logging_controller.dart
+++ b/packages/devtools_app/lib/src/screens/logging/logging_controller.dart
@@ -1096,6 +1096,22 @@
   }
 
   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'));
+
+    // Only cache the value if details have already been computed.
+    if (detailsComputed.isCompleted) _prettyPrinted = result;
+    return result;
+  }
+
+  String? _prettyPrinted;
+
+  static final _jsonEscapedAnsiEscape = RegExp(r'\\u001[bB]');
+
+  String? _prettyPrintedWithAnsi() {
     if (!detailsComputed.isCompleted) {
       return details?.trim();
     }
diff --git a/packages/devtools_app/lib/src/shared/primitives/utils.dart b/packages/devtools_app/lib/src/shared/primitives/utils.dart
index 10b90e3..f7bff87 100644
--- a/packages/devtools_app/lib/src/shared/primitives/utils.dart
+++ b/packages/devtools_app/lib/src/shared/primitives/utils.dart
@@ -555,16 +555,22 @@
     return TextSpan(
       text: entry.text,
       style: styled
-          ? TextStyle(
+          ? defaultStyle.copyWith(
               color: ansiToColor(entry.fgColor, background: backgroundColor),
               backgroundColor: ansiToColor(entry.bgColor),
-              fontWeight: entry.bold ? FontWeight.bold : FontWeight.normal,
+              fontWeight: entry.bold ? FontWeight.bold : null,
             )
           : defaultStyle,
     );
   }).toList();
 }
 
+/// Returns [input] with all ANSI escape codes removed.
+String stripAnsi(String input) {
+  if (!input.contains('\x1B')) return input;
+  return AnsiParser(input).parse().map((entry) => entry.text).join();
+}
+
 Color? ansiToColor(List<int>? ansiInput, {Color? background}) {
   if (ansiInput == null) {
     return null;
diff --git a/packages/devtools_app/release_notes/NEXT_RELEASE_NOTES.md b/packages/devtools_app/release_notes/NEXT_RELEASE_NOTES.md
index c0f0038..42ed03d 100644
--- a/packages/devtools_app/release_notes/NEXT_RELEASE_NOTES.md
+++ b/packages/devtools_app/release_notes/NEXT_RELEASE_NOTES.md
@@ -52,6 +52,14 @@
 
 ## Logging updates
 
+* Corrected time units and cumulative nature of GC events.
+  [#9890](https://github.com/flutter/devtools/pull/9890)
+* Fixed a bug where long `dart:developer` log messages lost their structured
+  Details view. -
+  [#10011](https://github.com/flutter/devtools/pull/10011),
+* Fixed ANSI-colored log messages rendering with black text and a larger font,
+  and removed raw ANSI escape codes from the Details view. -
+  [#10028](https://github.com/flutter/devtools/pull/10028)
 TODO: Remove this section if there are not any updates.
 
 ## App size tool updates
diff --git a/packages/devtools_app/test/screens/logging/logging_controller_test.dart b/packages/devtools_app/test/screens/logging/logging_controller_test.dart
index f047fad..8d746dc 100644
--- a/packages/devtools_app/test/screens/logging/logging_controller_test.dart
+++ b/packages/devtools_app/test/screens/logging/logging_controller_test.dart
@@ -5,6 +5,7 @@
 @TestOn('vm')
 library;
 
+import 'dart:async';
 import 'dart:convert';
 
 import 'package:devtools_app/devtools_app.dart';
@@ -505,6 +506,47 @@
       },
     );
 
+    test('prettyPrinted removes ANSI escape codes', () {
+      final log = LogData(
+        'stdout',
+        'flutter: \x1B[45mComponentChanged<AuthState>\x1B[0m updated',
+        0,
+      );
+      expect(
+        log.prettyPrinted(),
+        'flutter: ComponentChanged<AuthState> updated',
+      );
+    });
+
+    test('prettyPrinted is not cached before details are computed', () async {
+      final completer = Completer<String>();
+      final log = LogData(
+        'log',
+        'preview',
+        0,
+        detailsComputer: () => completer.future,
+      );
+      expect(log.prettyPrinted(), 'preview');
+
+      completer.complete('\x1B[32mfull details\x1B[0m');
+      await log.detailsComputed.future;
+      expect(log.prettyPrinted(), 'full details');
+    });
+
+    test('prettyPrinted removes ANSI escape codes from json details', () {
+      final log = LogData(
+        'log',
+        jsonEncode({'message': '\x1B[45mComponentChanged<AuthState>\x1B[0m'}),
+        0,
+      );
+      expect(
+        log.prettyPrinted(),
+        '{\n'
+        '  "message": "ComponentChanged<AuthState>"\n'
+        '}',
+      );
+    });
+
     test('computeDeveloperLogDetailsJson keeps structured details', () async {
       const truncatedPreview = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.';
       const fullMessage =
diff --git a/packages/devtools_app/test/shared/primitives/utils_test.dart b/packages/devtools_app/test/shared/primitives/utils_test.dart
index a1ad897..3a48931 100644
--- a/packages/devtools_app/test/shared/primitives/utils_test.dart
+++ b/packages/devtools_app/test/shared/primitives/utils_test.dart
@@ -697,6 +697,67 @@
     });
   });
 
+  group('textSpansFromAnsi', () {
+    const defaultStyle = TextStyle(
+      color: Color(0xFFEEEEEE),
+      fontSize: 12.0,
+      fontFamily: 'Roboto',
+    );
+
+    test('uses the default style for text without ANSI codes', () {
+      final spans = textSpansFromAnsi('plain', defaultStyle);
+      expect(spans, hasLength(1));
+      expect(spans.first.text, 'plain');
+      expect(spans.first.style, defaultStyle);
+    });
+
+    test('keeps the default color when only a background is set', () {
+      final spans = textSpansFromAnsi('\x1B[45mmagenta\x1B[0m', defaultStyle);
+      expect(spans, hasLength(1));
+      final style = spans.first.style!;
+      expect(style.color, defaultStyle.color);
+      expect(style.backgroundColor, const Color.fromRGBO(187, 0, 187, 1));
+      expect(style.fontSize, defaultStyle.fontSize);
+      expect(style.fontFamily, defaultStyle.fontFamily);
+    });
+
+    test('keeps the default font when a foreground is set', () {
+      final spans = textSpansFromAnsi(
+        'plain \x1B[32mgreen\x1B[0m plain',
+        defaultStyle,
+      );
+      expect(spans.map((s) => s.text), ['plain ', 'green', ' plain']);
+      final style = spans[1].style!;
+      expect(style.color, const Color.fromRGBO(0, 187, 0, 1));
+      expect(style.fontSize, defaultStyle.fontSize);
+      expect(style.fontFamily, defaultStyle.fontFamily);
+    });
+
+    test('applies bold', () {
+      final spans = textSpansFromAnsi('\x1B[1mbold\x1B[0m', defaultStyle);
+      final style = spans.first.style!;
+      expect(style.fontWeight, FontWeight.bold);
+      expect(style.color, defaultStyle.color);
+      expect(style.fontSize, defaultStyle.fontSize);
+    });
+  });
+
+  group('stripAnsi', () {
+    test('returns text without ANSI codes unchanged', () {
+      expect(stripAnsi('plain text'), 'plain text');
+    });
+
+    test('removes ANSI codes', () {
+      expect(
+        stripAnsi(
+          '\x1B[45mComponentChanged<AuthState>\x1B[0m '
+          '\x1B[38;5;208morange\x1B[0m \x1B[1;32mbold green\x1B[0m',
+        ),
+        'ComponentChanged<AuthState> orange bold green',
+      );
+    });
+  });
+
   test('devtoolsAssetsBasePath', () {
     // This is how a DevTools url will be structured when DevTools is served
     // directly from DDS using the `--observe` flag.