[flutter_tools] remove zone level overrides of verbose and daemon logging (#57448)

Make it possible for all FlutterCommands to be global free, by moving instantiation to inside the Zone context. Additionally, provide VerboseLogger and NotifyLogger (daemon) at the top level and remove from command-specific overrides.

This allows removing a work around where web devices needed to look up directly from the context in non-test code.

Technically the output preferences are still zone injected, but these will be moved soon as they were not being used correctly by the top level command (the injection comes after ArgParser reads the overflow values, causing numerous wrap issues)
This commit is contained in:
Jonah Williams
2020-06-04 16:35:36 -07:00
committed by GitHub
parent e9a4759947
commit f5de6aadd4
15 changed files with 131 additions and 92 deletions

View File

@@ -11,7 +11,6 @@ import '../base/file_system.dart';
import '../base/logger.dart';
import '../base/platform.dart';
import '../base/terminal.dart';
import '../globals.dart' as globals;
import '../runner/flutter_command.dart';
import 'analyze_continuously.dart';
import 'analyze_once.dart';
@@ -113,9 +112,7 @@ class AnalyzeCommand extends FlutterCommand {
runner.getRepoRoots(),
runner.getRepoPackages(),
fileSystem: _fileSystem,
// TODO(jonahwilliams): determine a better way to inject the logger,
// since it is constructed on-demand.
logger: _logger ?? globals.logger,
logger: _logger,
platform: _platform,
processManager: _processManager,
terminal: _terminal,
@@ -128,9 +125,7 @@ class AnalyzeCommand extends FlutterCommand {
runner.getRepoPackages(),
workingDirectory: workingDirectory,
fileSystem: _fileSystem,
// TODO(jonahwilliams): determine a better way to inject the logger,
// since it is constructed on-demand.
logger: _logger ?? globals.logger,
logger: _logger,
platform: _platform,
processManager: _processManager,
terminal: _terminal,

View File

@@ -200,7 +200,7 @@ class AttachCommand extends FlutterCommand {
? Daemon(
stdinCommandStream,
stdoutCommandResponse,
notifyingLogger: NotifyingLogger(),
notifyingLogger: NotifyingLogger(verbose: globals.logger.isVerbose),
logToStdout: true,
)
: null;

View File

@@ -52,27 +52,16 @@ class DaemonCommand extends FlutterCommand {
Future<FlutterCommandResult> runCommand() async {
globals.printStatus('Starting device daemon...');
isRunningFromDaemon = true;
final NotifyingLogger notifyingLogger = NotifyingLogger();
Cache.releaseLockEarly();
await context.run<void>(
body: () async {
final Daemon daemon = Daemon(
stdinCommandStream, stdoutCommandResponse,
notifyingLogger: notifyingLogger,
);
final int code = await daemon.onExit;
if (code != 0) {
throwToolExit('Daemon exited with non-zero exit code: $code', exitCode: code);
}
},
overrides: <Type, Generator>{
Logger: () => notifyingLogger,
},
final Daemon daemon = Daemon(
stdinCommandStream, stdoutCommandResponse,
notifyingLogger: globals.logger as NotifyingLogger,
);
final int code = await daemon.onExit;
if (code != 0) {
throwToolExit('Daemon exited with non-zero exit code: $code', exitCode: code);
}
return FlutterCommandResult.success();
}
}
@@ -927,7 +916,22 @@ dynamic _toJsonable(dynamic obj) {
}
class NotifyingLogger extends Logger {
final StreamController<LogMessage> _messageController = StreamController<LogMessage>.broadcast();
NotifyingLogger({ @required this.verbose }) {
_messageController = StreamController<LogMessage>.broadcast(
onListen: _onListen,
);
}
final bool verbose;
final List<LogMessage> messageBuffer = <LogMessage>[];
StreamController<LogMessage> _messageController;
void _onListen() {
if (messageBuffer.isNotEmpty) {
messageBuffer.forEach(_messageController.add);
messageBuffer.clear();
}
}
Stream<LogMessage> get onMessage => _messageController.stream;
@@ -941,7 +945,7 @@ class NotifyingLogger extends Logger {
int hangingIndent,
bool wrap,
}) {
_messageController.add(LogMessage('error', message, stackTrace));
_sendMessage(LogMessage('error', message, stackTrace));
}
@override
@@ -954,12 +958,15 @@ class NotifyingLogger extends Logger {
int hangingIndent,
bool wrap,
}) {
_messageController.add(LogMessage('status', message));
_sendMessage(LogMessage('status', message));
}
@override
void printTrace(String message) {
// This is a lot of traffic to send over the wire.
if (!verbose) {
return;
}
_sendMessage(LogMessage('trace', message));
}
@override
@@ -979,6 +986,13 @@ class NotifyingLogger extends Logger {
);
}
void _sendMessage(LogMessage logMessage) {
if (_messageController.hasListener) {
return _messageController.add(logMessage);
}
messageBuffer.add(logMessage);
}
void dispose() {
_messageController.close();
}

View File

@@ -407,7 +407,7 @@ class RunCommand extends RunCommandBase {
final Daemon daemon = Daemon(
stdinCommandStream,
stdoutCommandResponse,
notifyingLogger: NotifyingLogger(),
notifyingLogger: NotifyingLogger(verbose: globals.logger.isVerbose),
logToStdout: true,
);
AppInstance app;

View File

@@ -103,6 +103,7 @@ class DeviceManager {
fileSystem: globals.fs,
platform: globals.platform,
processManager: globals.processManager,
logger: globals.logger,
),
]);

View File

@@ -15,7 +15,6 @@ import '../artifacts.dart';
import '../base/common.dart';
import '../base/context.dart';
import '../base/file_system.dart';
import '../base/logger.dart';
import '../base/terminal.dart';
import '../base/user_messages.dart';
import '../base/utils.dart';
@@ -236,12 +235,6 @@ class FlutterCommandRunner extends CommandRunner<void> {
Future<void> runCommand(ArgResults topLevelResults) async {
final Map<Type, dynamic> contextOverrides = <Type, dynamic>{};
// Check for verbose.
if (topLevelResults['verbose'] as bool) {
// Override the logger.
contextOverrides[Logger] = VerboseLogger(globals.logger);
}
// Don't set wrapColumns unless the user said to: if it's set, then all
// wrapping will occur at this width explicitly, and won't adapt if the
// terminal size changes during a run.

View File

@@ -14,7 +14,6 @@ import '../base/platform.dart';
import '../build_info.dart';
import '../device.dart';
import '../features.dart';
import '../globals.dart' as globals;
import '../project.dart';
import 'chrome.dart';
@@ -36,7 +35,7 @@ abstract class ChromiumDevice extends Device {
@required String name,
@required this.chromeLauncher,
@required FileSystem fileSystem,
Logger logger,
@required Logger logger,
}) : _fileSystem = fileSystem,
_logger = logger,
super(
@@ -49,7 +48,7 @@ abstract class ChromiumDevice extends Device {
final ChromiumLauncher chromeLauncher;
final FileSystem _fileSystem;
Logger _logger;
final Logger _logger;
/// The active chrome instance.
Chromium _chrome;
@@ -115,7 +114,6 @@ abstract class ChromiumDevice extends Device {
bool prebuiltApplication = false,
bool ipv6 = false,
}) async {
_logger ??= globals.logger;
// See [ResidentWebRunner.run] in flutter_tools/lib/src/resident_web_runner.dart
// for the web initialization and server logic.
final String url = platformArgs['uri'] as String;
@@ -241,10 +239,7 @@ class MicrosoftEdgeDevice extends ChromiumDevice {
class WebDevices extends PollingDeviceDiscovery {
WebDevices({
@required FileSystem fileSystem,
// TODO(jonahwilliams): the logger is overriden by the daemon command
// to support IDE integration. Accessing the global logger too early
// will grab the old stdout logger.
Logger logger,
@required Logger logger,
@required Platform platform,
@required ProcessManager processManager,
@required FeatureFlags featureFlags,
@@ -307,7 +302,7 @@ String parseVersionForWindows(String input) {
/// A special device type to allow serving for arbitrary browsers.
class WebServerDevice extends Device {
WebServerDevice({
Logger logger,
@required Logger logger,
}) : _logger = logger,
super(
'web-server',
@@ -316,7 +311,7 @@ class WebServerDevice extends Device {
ephemeral: false,
);
Logger _logger;
final Logger _logger;
@override
void clearLogs() { }
@@ -375,7 +370,6 @@ class WebServerDevice extends Device {
bool prebuiltApplication = false,
bool ipv6 = false,
}) async {
_logger ??= globals.logger;
final String url = platformArgs['uri'] as String;
if (debuggingOptions.startPaused) {
_logger.printStatus('Waiting for connection from Dart debug extension at $url', emphasis: true);