[flutter tools] Add a DelegatingLogger class (#67581)

[flutter tools] Add a DelegatingLogger class

Move most of `DelegateLogger` `from test/src/testbed.dart` to
`lib/src/base/logger.dart` to better formalize the common practice of
chaining `Logger`s together.  I renamed the class since it isn't
itself the delegate and to better match the `Delegating...` classes
from `package:collection`.

Additionally, add a freestanding `asLogger<T>` function to "cast" a
`Logger` into a matching delegate if possible.  This will allow
`Logger` chains to be ordered a *bit* more freely (e.g.
`NotifyingLogger` and `AppRunLogger` will no longer required to be
at the end of the chain, an unwritten rule that has led to breakage in
google3).  Chain order still matters since lack of virtual dispatch
means that parent `Logger`s can never invoke child methods, however.

I made `asLogger<T>` a freestanding function because I didn't want to
make it part of the `Logger` interface (and I thought that making it
an extension method might be weird).

Bonus cleanup:
There no longer appears to be a way to construct an `AppRunLogger`
with a null parent, so remove all of code paths for that case and
make the `parent` construction parameter required.
This commit is contained in:
James D. Lin
2020-10-09 15:55:24 -07:00
committed by GitHub
parent bdb830a833
commit e4206ac5dd
6 changed files with 299 additions and 186 deletions

View File

@@ -783,68 +783,15 @@ class TestFeatureFlags implements FeatureFlags {
}
}
class DelegateLogger implements Logger {
DelegateLogger(this.delegate);
class FakeStatusLogger extends DelegatingLogger {
FakeStatusLogger(Logger delegate) : super(delegate);
final Logger delegate;
Status status;
@override
bool get quiet => delegate.quiet;
@override
set quiet(bool value) => delegate.quiet;
@override
bool get hasTerminal => delegate.hasTerminal;
@override
bool get isVerbose => delegate.isVerbose;
@override
void printError(String message, {StackTrace stackTrace, bool emphasis, TerminalColor color, int indent, int hangingIndent, bool wrap}) {
delegate.printError(
message,
stackTrace: stackTrace,
emphasis: emphasis,
color: color,
indent: indent,
hangingIndent: hangingIndent,
wrap: wrap,
);
}
@override
void printStatus(String message, {bool emphasis, TerminalColor color, bool newline, int indent, int hangingIndent, bool wrap}) {
delegate.printStatus(message,
emphasis: emphasis,
color: color,
indent: indent,
hangingIndent: hangingIndent,
wrap: wrap,
);
}
@override
void printTrace(String message) {
delegate.printTrace(message);
}
@override
void sendEvent(String name, [Map<String, dynamic> args]) {
delegate.sendEvent(name, args);
}
@override
Status startProgress(String message, {Duration timeout, String progressId, bool multilineOutput = false, int progressIndicatorPadding = kDefaultStatusPadding}) {
return status;
}
@override
bool get supportsColor => delegate.supportsColor;
@override
void clear() => delegate.clear();
}
/// An implementation of the Cache which does not download or require locking.