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:
Tim Blasi
2015-04-09 17:42:14 -07:00
parent cac74c73e1
commit bba849909c
4 changed files with 31 additions and 28 deletions

View File

@ -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)}.';
}
}