fix(dart/transform): Gracefully handle log calls before init
- Lazily create and use a logger that prints instead of `throw`ing. - Use this logger in unit tests.
This commit is contained in:
@ -18,7 +18,37 @@ void setLogger(BuildLogger logger) {
|
||||
/// The logger the transformer should use for messaging.
|
||||
BuildLogger get logger {
|
||||
if (_logger == null) {
|
||||
throw new StateError('Logger never initialized.');
|
||||
_logger = new PrintLogger();
|
||||
}
|
||||
return _logger;
|
||||
}
|
||||
|
||||
class PrintLogger implements BuildLogger {
|
||||
void _printWithPrefix(prefix, msg) => print('$prefix: $msg');
|
||||
void info(msg, {AssetId asset, SourceSpan span}) =>
|
||||
_printWithPrefix('INFO', msg);
|
||||
void fine(msg, {AssetId asset, SourceSpan span}) =>
|
||||
_printWithPrefix('FINE', msg);
|
||||
void warning(msg, {AssetId asset, SourceSpan span}) =>
|
||||
_printWithPrefix('WARN', msg);
|
||||
void error(msg, {AssetId asset, SourceSpan span}) {
|
||||
throw new PrintLoggerError(msg, asset, span);
|
||||
}
|
||||
Future writeOutput() => null;
|
||||
Future addLogFilesFromAsset(AssetId id, [int nextNumber = 1]) => null;
|
||||
}
|
||||
|
||||
class PrintLoggerError extends Error {
|
||||
final String message;
|
||||
final AssetId asset;
|
||||
final SourceSpan span;
|
||||
|
||||
PrintLoggerError(message, asset, span);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'Message: ${Error.safeToString(message)}, '
|
||||
'Asset: ${Error.safeToString(asset)}, '
|
||||
'Span: ${Error.safeToString(span)}.';
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user