From 04e4b117f66fb6c5c61676a74e6b24a5fa453692 Mon Sep 17 00:00:00 2001 From: Devon Carew Date: Tue, 15 Sep 2020 12:44:05 -0700 Subject: [PATCH] fix an issue where raw json output is written to IDE clients (#65508) fix an issues where raw json output was written to IDE clients --- .../lib/src/commands/daemon.dart | 48 +++++++++++++------ .../test/general.shard/base/logger_test.dart | 10 ++++ 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/packages/flutter_tools/lib/src/commands/daemon.dart b/packages/flutter_tools/lib/src/commands/daemon.dart index d279d40482..d8c1e9fa9f 100644 --- a/packages/flutter_tools/lib/src/commands/daemon.dart +++ b/packages/flutter_tools/lib/src/commands/daemon.dart @@ -545,6 +545,12 @@ class AppDomain extends Domain { final AppInstance app = AppInstance(_getNewAppId(), runner: runner, logToStdout: daemon.logToStdout, logger: logger); _apps.add(app); + + // Set the domain and app for the given AppRunLogger. This allows the logger + // to log messages containing the app ID to the host. + logger.domain = this; + logger.app = app; + _sendAppEvent(app, 'start', { 'deviceId': device.id, 'directory': projectDirectory, @@ -1105,8 +1111,6 @@ class AppInstance { } Future _runInZone(AppDomain domain, FutureOr method()) async { - _logger.domain = domain; - _logger.app = this; return method(); } } @@ -1253,22 +1257,22 @@ class AppRunLogger extends Logger { }) { final int id = _nextProgressId++; - _sendProgressEvent({ - 'id': id.toString(), - 'progressId': progressId, - 'message': message, - }); + _sendProgressEvent( + eventId: id.toString(), + eventType: progressId, + message: message, + ); _status = SilentStatus( timeout: timeout, timeoutConfiguration: timeoutConfiguration, onFinish: () { _status = null; - _sendProgressEvent({ - 'id': id.toString(), - 'progressId': progressId, - 'finished': true, - }); + _sendProgressEvent( + eventId: id.toString(), + eventType: progressId, + finished: true, + ); }, stopwatch: Stopwatch())..start(); return _status; } @@ -1285,10 +1289,26 @@ class AppRunLogger extends Logger { } } - void _sendProgressEvent(Map event) { + void _sendProgressEvent({ + @required String eventId, + @required String eventType, + bool finished = false, + String message, + }) { if (domain == null) { - printStatus('event sent after app closed: $event'); + // If we're sending progress events before an app has started, send the + // progress messages as plain status messages. + if (message != null) { + printStatus(message); + } } else { + final Map event = { + 'id': eventId, + 'progressId': eventType, + if (message != null) 'message': message, + if (finished != null) 'finished': finished, + }; + domain._sendAppEvent(app, 'progress', event); } } diff --git a/packages/flutter_tools/test/general.shard/base/logger_test.dart b/packages/flutter_tools/test/general.shard/base/logger_test.dart index 514bc83a8c..0237ff098e 100644 --- a/packages/flutter_tools/test/general.shard/base/logger_test.dart +++ b/packages/flutter_tools/test/general.shard/base/logger_test.dart @@ -15,6 +15,7 @@ import 'package:mockito/mockito.dart'; import 'package:fake_async/fake_async.dart'; import '../../src/common.dart'; +import '../../src/context.dart'; import '../../src/mocks.dart' as mocks; final Platform _kNoAnsiPlatform = FakePlatform(stdoutSupportsAnsi: false); @@ -628,6 +629,15 @@ void main() { expect(lines[3], equals('0123456789' * 3)); }); + testUsingContext('AppRunLogger writes plain text statuses when no app is active', () async { + final BufferLogger buffer = BufferLogger.test(); + final AppRunLogger logger = AppRunLogger(parent: buffer); + + logger.startProgress('Test status...', timeout: null).stop(); + + expect(buffer.statusText.trim(), equals('Test status...')); + }); + testWithoutContext('Error logs are wrapped and can be indented.', () async { final Logger logger = StdoutLogger( terminal: AnsiTerminal(