Unverified Commit 53d8cba3 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[flutter_tools] let the logger know about machine mode (#86116)

parent b1e1dd68
......@@ -77,8 +77,9 @@ Future<void> main(List<String> args) async {
final bool muteCommandLogging = (help || doctor) && !veryVerbose;
final bool verboseHelp = help && verbose;
final bool daemon = args.contains('daemon');
final bool runMachine = (args.contains('--machine') && args.contains('run')) ||
(args.contains('--machine') && args.contains('attach'));
final bool machine = args.contains('--machine');
final bool runMachine = (machine && args.contains('run')) ||
(machine && args.contains('attach'));
// Cache.flutterRoot must be set early because other features use it (e.g.
// enginePath's initializer uses it). This can only work with the real
......@@ -121,10 +122,11 @@ Future<void> main(List<String> args) async {
);
return loggerFactory.createLogger(
daemon: daemon,
machine: runMachine,
runMachine: runMachine,
verbose: verbose && !muteCommandLogging,
prefixedErrors: prefixedErrors,
windows: globals.platform.isWindows,
machine: machine,
);
},
},
......@@ -225,9 +227,10 @@ class LoggerFactory {
Logger createLogger({
@required bool verbose,
@required bool prefixedErrors,
@required bool machine,
@required bool runMachine,
@required bool daemon,
@required bool windows,
@required bool machine,
}) {
Logger logger;
if (windows) {
......@@ -236,13 +239,15 @@ class LoggerFactory {
stdio: _stdio,
outputPreferences: _outputPreferences,
stopwatchFactory: _stopwatchFactory,
machine: machine,
);
} else {
logger = StdoutLogger(
terminal: _terminal,
stdio: _stdio,
outputPreferences: _outputPreferences,
stopwatchFactory: _stopwatchFactory
stopwatchFactory: _stopwatchFactory,
machine: machine,
);
}
if (verbose) {
......@@ -254,7 +259,7 @@ class LoggerFactory {
if (daemon) {
return NotifyingLogger(verbose: verbose, parent: logger);
}
if (machine) {
if (runMachine) {
return AppRunLogger(parent: logger);
}
return logger;
......
......@@ -226,6 +226,7 @@ Future<String> _doctorText() async {
final BufferLogger logger = BufferLogger(
terminal: globals.terminal,
outputPreferences: globals.outputPreferences,
machine: false,
);
final Doctor doctor = Doctor(logger: logger);
......
......@@ -36,6 +36,12 @@ abstract class Logger {
bool get hasTerminal;
/// Whether the current flutter command invocation also contains a `--machine` flag.
///
/// This should be used to elide output to stdout that is not formatted in newline
/// delimited JSON.
bool get machine;
Terminal get terminal;
OutputPreferences get _outputPreferences;
......@@ -159,6 +165,9 @@ class DelegatingLogger implements Logger {
@override
bool get quiet => _delegate.quiet;
@override
bool get machine => _delegate.machine;
@override
set quiet(bool value) => _delegate.quiet = value;
......@@ -269,6 +278,7 @@ class StdoutLogger extends Logger {
required this.terminal,
required Stdio stdio,
required OutputPreferences outputPreferences,
required this.machine,
StopwatchFactory stopwatchFactory = const StopwatchFactory(),
})
: _stdio = stdio,
......@@ -279,6 +289,8 @@ class StdoutLogger extends Logger {
final Terminal terminal;
@override
final OutputPreferences _outputPreferences;
@override
final bool machine;
final Stdio _stdio;
final StopwatchFactory _stopwatchFactory;
......@@ -443,12 +455,14 @@ class WindowsStdoutLogger extends StdoutLogger {
required Terminal terminal,
required Stdio stdio,
required OutputPreferences outputPreferences,
required bool machine,
StopwatchFactory stopwatchFactory = const StopwatchFactory(),
}) : super(
terminal: terminal,
stdio: stdio,
outputPreferences: outputPreferences,
stopwatchFactory: stopwatchFactory,
machine: machine,
);
@override
......@@ -470,6 +484,7 @@ class BufferLogger extends Logger {
BufferLogger({
required this.terminal,
required OutputPreferences outputPreferences,
this.machine = false,
StopwatchFactory stopwatchFactory = const StopwatchFactory(),
}) : _outputPreferences = outputPreferences,
_stopwatchFactory = stopwatchFactory;
......@@ -478,6 +493,7 @@ class BufferLogger extends Logger {
BufferLogger.test({
Terminal? terminal,
OutputPreferences? outputPreferences,
this.machine = false,
}) : terminal = terminal ?? Terminal.test(),
_outputPreferences = outputPreferences ?? OutputPreferences.test(),
_stopwatchFactory = const StopwatchFactory();
......@@ -489,6 +505,9 @@ class BufferLogger extends Logger {
@override
final Terminal terminal;
@override
final bool machine;
final StopwatchFactory _stopwatchFactory;
@override
......
......@@ -273,11 +273,13 @@ Future<T> runInContext<T>(
terminal: globals.terminal,
stdio: globals.stdio,
outputPreferences: globals.outputPreferences,
machine: false,
)
: StdoutLogger(
terminal: globals.terminal,
stdio: globals.stdio,
outputPreferences: globals.outputPreferences,
machine: false,
),
MacOSWorkflow: () => MacOSWorkflow(
featureFlags: featureFlags,
......
......@@ -44,7 +44,7 @@ void main() {
platform = const LocalPlatform();
processManager = const LocalProcessManager();
terminal = AnsiTerminal(platform: platform, stdio: Stdio());
logger = BufferLogger(outputPreferences: OutputPreferences.test(), terminal: terminal);
logger = BufferLogger(outputPreferences: OutputPreferences.test(), terminal: terminal, machine: false);
tempDir = fileSystem.systemTempDirectory.createTempSync('flutter_analysis_test.');
});
......
......@@ -558,6 +558,9 @@ class StreamLogger extends Logger {
@override
bool get isVerbose => true;
@override
bool get machine => true;
@override
void printError(
String message, {
......
......@@ -186,6 +186,7 @@ void main() {
terminal: Terminal.test(supportsColor: true),
stdio: FakeStdio(),
outputPreferences: OutputPreferences.test(),
machine: false,
);
final ArtifactUpdater artifactUpdater = ArtifactUpdater(
fileSystem: fileSystem,
......
......@@ -26,7 +26,7 @@ final String resetBold = RegExp.escape(AnsiTerminal.resetBold);
final String resetColor = RegExp.escape(AnsiTerminal.resetColor);
void main() {
testWithoutContext('correct logger instance is created', () {
testWithoutContext('correct logger instance is created with machine: false', () {
final LoggerFactory loggerFactory = LoggerFactory(
terminal: Terminal.test(),
stdio: FakeStdio(),
......@@ -36,52 +36,124 @@ void main() {
expect(loggerFactory.createLogger(
verbose: false,
prefixedErrors: false,
machine: false,
runMachine: false,
daemon: false,
windows: false,
), isA<StdoutLogger>());
machine: false
), isA<StdoutLogger>().having((Logger logger) => logger.machine, 'machine', false));
expect(loggerFactory.createLogger(
verbose: false,
prefixedErrors: false,
machine: false,
runMachine: false,
daemon: false,
windows: true,
), isA<WindowsStdoutLogger>());
machine: false
), isA<WindowsStdoutLogger>().having((Logger logger) => logger.machine, 'machine', false));
expect(loggerFactory.createLogger(
verbose: true,
prefixedErrors: false,
machine: false,
runMachine: false,
daemon: false,
windows: true,
), isA<VerboseLogger>());
machine: false
), isA<VerboseLogger>().having((Logger logger) => logger.machine, 'machine', false));
expect(loggerFactory.createLogger(
verbose: true,
prefixedErrors: false,
machine: false,
runMachine: false,
daemon: false,
windows: false,
), isA<VerboseLogger>());
machine: false
), isA<VerboseLogger>().having((Logger logger) => logger.machine, 'machine', false));
expect(loggerFactory.createLogger(
verbose: false,
prefixedErrors: true,
machine: false,
runMachine: false,
daemon: false,
windows: false,
), isA<PrefixedErrorLogger>());
machine: false
), isA<PrefixedErrorLogger>().having((Logger logger) => logger.machine, 'machine', false));
expect(loggerFactory.createLogger(
verbose: false,
prefixedErrors: false,
machine: false,
runMachine: false,
daemon: true,
windows: false,
machine: false
), isA<NotifyingLogger>().having((Logger logger) => logger.machine, 'machine', false));
expect(loggerFactory.createLogger(
verbose: false,
prefixedErrors: false,
runMachine: true,
daemon: false,
windows: false,
machine: false
), isA<AppRunLogger>().having((Logger logger) => logger.machine, 'machine', false));
});
testWithoutContext('correct logger instance is created with machine: true', () {
final LoggerFactory loggerFactory = LoggerFactory(
terminal: Terminal.test(),
stdio: FakeStdio(),
outputPreferences: OutputPreferences.test(),
);
expect(loggerFactory.createLogger(
verbose: false,
prefixedErrors: false,
runMachine: false,
daemon: false,
windows: false,
machine: true
), isA<StdoutLogger>().having((Logger logger) => logger.machine, 'machine', true));
expect(loggerFactory.createLogger(
verbose: false,
prefixedErrors: false,
runMachine: false,
daemon: false,
windows: true,
machine: true
), isA<WindowsStdoutLogger>().having((Logger logger) => logger.machine, 'machine', true));
expect(loggerFactory.createLogger(
verbose: true,
prefixedErrors: false,
runMachine: false,
daemon: false,
windows: true,
machine: true
), isA<VerboseLogger>().having((Logger logger) => logger.machine, 'machine', true));
expect(loggerFactory.createLogger(
verbose: true,
prefixedErrors: false,
runMachine: false,
daemon: false,
windows: false,
machine: true
), isA<VerboseLogger>().having((Logger logger) => logger.machine, 'machine', true));
expect(loggerFactory.createLogger(
verbose: false,
prefixedErrors: true,
runMachine: false,
daemon: false,
windows: false,
machine: true
), isA<PrefixedErrorLogger>().having((Logger logger) => logger.machine, 'machine', true));
expect(loggerFactory.createLogger(
verbose: false,
prefixedErrors: false,
runMachine: false,
daemon: true,
windows: false,
), isA<NotifyingLogger>());
machine: true
), isA<NotifyingLogger>().having((Logger logger) => logger.machine, 'machine', true));
expect(loggerFactory.createLogger(
verbose: false,
prefixedErrors: false,
machine: true,
runMachine: true,
daemon: false,
windows: false,
), isA<AppRunLogger>());
machine: true
), isA<AppRunLogger>().having((Logger logger) => logger.machine, 'machine', true));
});
testWithoutContext('WindowsStdoutLogger rewrites emojis when terminal does not support emoji', () {
......@@ -90,6 +162,7 @@ void main() {
outputPreferences: OutputPreferences.test(),
stdio: stdio,
terminal: Terminal.test(supportsColor: false, supportsEmoji: false),
machine: false,
);
logger.printStatus('🔥🖼️✗✓🔨💪✏️');
......@@ -103,6 +176,7 @@ void main() {
outputPreferences: OutputPreferences.test(),
stdio: stdio,
terminal: Terminal.test(supportsColor: true, supportsEmoji: true),
machine: false,
);
logger.printStatus('🔥🖼️✗✓🔨💪✏️');
......@@ -295,6 +369,7 @@ void main() {
),
stdio: stdio,
outputPreferences: OutputPreferences.test(),
machine: false,
);
logger.printStatus('message');
......@@ -312,6 +387,7 @@ void main() {
),
stdio: stdio,
outputPreferences: OutputPreferences.test(),
machine: false,
);
logger.printStatus('message');
logger.printError('error message');
......@@ -331,6 +407,7 @@ void main() {
),
stdio: stdio,
outputPreferences: OutputPreferences.test(),
machine: false,
);
logger.printStatus('message');
logger.printError('error message');
......@@ -461,6 +538,7 @@ void main() {
stdio: mockStdio,
outputPreferences: OutputPreferences.test(showColor: true),
stopwatchFactory: stopwatchFactory,
machine: false,
);
final Status status = logger.startProgress(
'Hello',
......@@ -498,6 +576,7 @@ void main() {
stdio: mockStdio,
outputPreferences: OutputPreferences.test(showColor: true),
stopwatchFactory: stopwatchFactory,
machine: false,
);
const String message = "Knock Knock, Who's There";
final Status status = logger.startProgress(
......@@ -539,6 +618,7 @@ void main() {
stdio: mockStdio,
outputPreferences: OutputPreferences.test(showColor: true),
stopwatchFactory: stopwatchFactory,
machine: false,
);
const String message = "Knock Knock, Who's There";
final Status status = logger.startProgress(
......@@ -671,6 +751,7 @@ void main() {
),
stdio: fakeStdio,
outputPreferences: OutputPreferences.test(wrapText: true, wrapColumn: 40, showColor: false),
machine: false,
);
logger.printError('0123456789' * 15);
final List<String> lines = outputStderr();
......@@ -699,6 +780,7 @@ void main() {
platform: _kNoAnsiPlatform,
),
stdio: fakeStdio,
machine: false,
outputPreferences: OutputPreferences.test(wrapText: true, wrapColumn: 40, showColor: false),
);
logger.printError('0123456789' * 15, indent: 5);
......@@ -722,6 +804,7 @@ void main() {
platform: _kNoAnsiPlatform,
),
stdio: fakeStdio,
machine: false,
outputPreferences: OutputPreferences.test(wrapText: true, wrapColumn: 40, showColor: false),
);
logger.printError('0123456789' * 15, hangingIndent: 5);
......@@ -745,6 +828,7 @@ void main() {
platform: _kNoAnsiPlatform,
),
stdio: fakeStdio,
machine: false,
outputPreferences: OutputPreferences.test(wrapText: true, wrapColumn: 40, showColor: false),
);
logger.printError('0123456789' * 15, indent: 4, hangingIndent: 5);
......@@ -768,6 +852,7 @@ void main() {
platform: _kNoAnsiPlatform,
),
stdio: fakeStdio,
machine: false,
outputPreferences: OutputPreferences.test(wrapText: true, wrapColumn: 40, showColor: false),
);
logger.printStatus('0123456789' * 15);
......@@ -789,6 +874,7 @@ void main() {
),
stdio: fakeStdio,
outputPreferences: OutputPreferences.test(wrapText: true, wrapColumn: 40, showColor: false),
machine: false,
);
logger.printStatus('0123456789' * 15, indent: 5);
final List<String> lines = outputStdout();
......@@ -811,6 +897,7 @@ void main() {
platform: _kNoAnsiPlatform,
),
stdio: fakeStdio,
machine: false,
outputPreferences: OutputPreferences.test(wrapText: true, wrapColumn: 40, showColor: false)
);
logger.printStatus('0123456789' * 15, hangingIndent: 5);
......@@ -834,6 +921,7 @@ void main() {
platform: _kNoAnsiPlatform,
),
stdio: fakeStdio,
machine: false,
outputPreferences: OutputPreferences.test(wrapText: true, wrapColumn: 40, showColor: false),
);
logger.printStatus('0123456789' * 15, indent: 4, hangingIndent: 5);
......@@ -857,6 +945,7 @@ void main() {
platform: FakePlatform(stdoutSupportsAnsi: true),
),
stdio: fakeStdio,
machine: false,
outputPreferences: OutputPreferences.test(showColor: true),
);
logger.printError('Pants on fire!');
......@@ -874,6 +963,7 @@ void main() {
platform: FakePlatform(),
),
stdio: fakeStdio,
machine: false,
outputPreferences: OutputPreferences.test(showColor: true),
);
logger.printStatus('All good.');
......@@ -892,6 +982,7 @@ void main() {
),
stdio: fakeStdio,
outputPreferences: OutputPreferences.test(showColor: true),
machine: false,
);
logger.printStatus(
null,
......@@ -915,6 +1006,7 @@ void main() {
),
stdio: fakeStdio,
outputPreferences: OutputPreferences.test(showColor: false),
machine: false,
);
logger.printStatus(
null,
......@@ -939,6 +1031,7 @@ void main() {
stdio: fakeStdio,
outputPreferences: OutputPreferences.test(showColor: false),
stopwatchFactory: FakeStopwatchFactory(stopwatch: fakeStopwatch),
machine: false,
);
final Status status = logger.startProgress(
'Hello',
......@@ -1008,6 +1101,7 @@ void main() {
),
stdio: fakeStdio,
outputPreferences: OutputPreferences.test(showColor: false),
machine: false,
);
logger.startProgress('AAA').stop();
logger.startProgress('BBB').stop();
......@@ -1031,6 +1125,7 @@ void main() {
),
stdio: fakeStdio,
outputPreferences: OutputPreferences.test(),
machine: false,
),
stopwatchFactory: FakeStopwatchFactory(),
);
......
......@@ -16,6 +16,7 @@ void main() {
final BufferLogger bufferLogger = BufferLogger(
outputPreferences: OutputPreferences.test(wrapText: true, wrapColumn: 40),
terminal: TestTerminal(platform: FakePlatform()..stdoutSupportsAnsi = true),
machine: false,
);
bufferLogger.printStatus('0123456789' * 8);
......@@ -26,6 +27,7 @@ void main() {
final BufferLogger bufferLogger = BufferLogger(
outputPreferences: OutputPreferences.test(wrapText: false),
terminal: TestTerminal(platform: FakePlatform()..stdoutSupportsAnsi = true),
machine: false,
);
final String testString = '0123456789' * 20;
bufferLogger.printStatus(testString);
......@@ -124,6 +126,7 @@ void main() {
final BufferLogger bufferLogger = BufferLogger(
terminal: terminalUnderTest,
outputPreferences: OutputPreferences.test(),
machine: false,
);
terminalUnderTest.usesTerminalUi = true;
mockStdInStream = Stream<String>.fromFutures(<Future<String>>[
......@@ -148,6 +151,7 @@ void main() {
final BufferLogger bufferLogger = BufferLogger(
terminal: terminalUnderTest,
outputPreferences: OutputPreferences.test(),
machine: false,
);
terminalUnderTest.usesTerminalUi = true;
mockStdInStream = Stream<String>.fromFutures(<Future<String>>[
......
......@@ -40,6 +40,7 @@ void main () {
testLogger = BufferLogger(
terminal: Terminal.test(),
outputPreferences: OutputPreferences.test(),
machine: false,
);
mockCmakeProject = FakeCmakeProject(managedCmakeFile);
......
......@@ -24,6 +24,7 @@ final ProcessUtils processUtils = ProcessUtils(processManager: processManager, l
),
stdio: stdio,
outputPreferences: OutputPreferences.test(wrapText: true),
machine: false,
));
final String flutterBin = fileSystem.path.join(getFlutterRoot(), 'bin', platform.isWindows ? 'flutter.bat' : 'flutter');
......
......@@ -38,6 +38,7 @@ final Map<Type, Generator> _testbedDefaults = <Type, Generator>{
Logger: () => BufferLogger(
terminal: AnsiTerminal(stdio: globals.stdio, platform: globals.platform), // Danger, using real stdio.
outputPreferences: OutputPreferences.test(),
machine: false,
), // Allows reading logs and prevents stdout.
OperatingSystemUtils: () => FakeOperatingSystemUtils(),
OutputPreferences: () => OutputPreferences.test(), // configures BufferLogger to avoid color codes.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment