2015-03-20 15:37:09 -07:00
|
|
|
library angular2.transform.common.logging;
|
2015-02-19 12:00:09 -08:00
|
|
|
|
2015-04-13 16:54:09 -07:00
|
|
|
import 'dart:async';
|
2015-08-05 17:39:37 -07:00
|
|
|
|
|
|
|
import 'package:analyzer/analyzer.dart';
|
2015-02-19 12:00:09 -08:00
|
|
|
import 'package:barback/barback.dart';
|
|
|
|
import 'package:code_transformers/messages/build_logger.dart';
|
2015-04-13 16:54:09 -07:00
|
|
|
import 'package:source_span/source_span.dart';
|
2015-02-19 12:00:09 -08:00
|
|
|
|
2015-07-23 09:06:23 -07:00
|
|
|
typedef _SimpleCallback();
|
2015-02-19 12:00:09 -08:00
|
|
|
|
2015-07-23 09:06:23 -07:00
|
|
|
// The key used to store the logger on the current zone.
|
|
|
|
final _key = #loggingZonedLoggerKey;
|
2015-02-19 12:00:09 -08:00
|
|
|
|
2015-07-23 09:06:23 -07:00
|
|
|
/// Executes {@link fn} inside a new {@link Zone} with its own logger.
|
2015-08-05 17:39:37 -07:00
|
|
|
Future<dynamic> initZoned(Transform t, _SimpleCallback fn) =>
|
2015-07-29 14:28:14 -07:00
|
|
|
setZoned(new BuildLogger(t), fn);
|
2015-07-23 09:06:23 -07:00
|
|
|
|
2015-08-05 17:39:37 -07:00
|
|
|
Future<dynamic> setZoned(BuildLogger logger, _SimpleCallback fn) async {
|
|
|
|
return runZoned(() async {
|
|
|
|
try {
|
|
|
|
await fn();
|
|
|
|
} on AnalyzerError catch (e) {
|
|
|
|
// Do not worry about printing the stack trace, barback will handle that
|
|
|
|
// on its own when it catches the rethrown exception.
|
|
|
|
logger
|
|
|
|
.error(' Failed with ${e.runtimeType}\n${_friendlyError(e.error)}');
|
|
|
|
rethrow;
|
|
|
|
} on AnalyzerErrorGroup catch (eGroup) {
|
|
|
|
// See above re: stack trace.
|
|
|
|
var numErrors = eGroup.errors.length;
|
|
|
|
if (numErrors == 1) {
|
|
|
|
logger.error(_friendlyError(eGroup.errors[0].error));
|
|
|
|
} else {
|
|
|
|
var buf = new StringBuffer();
|
|
|
|
buf.writeln(' Failed with ${numErrors} errors');
|
|
|
|
for (var i = 0; i < numErrors; ++i) {
|
|
|
|
buf.writeln(
|
|
|
|
'Error ${i + 1}: ${_friendlyError(eGroup.errors[i].error)}');
|
|
|
|
}
|
|
|
|
logger.error('$buf');
|
|
|
|
}
|
|
|
|
rethrow;
|
|
|
|
}
|
|
|
|
}, zoneValues: {_key: logger});
|
2015-07-23 15:18:30 -07:00
|
|
|
}
|
2015-03-18 11:03:41 -07:00
|
|
|
|
2015-07-23 09:06:23 -07:00
|
|
|
/// The logger for the current {@link Zone}.
|
2015-02-19 12:00:09 -08:00
|
|
|
BuildLogger get logger {
|
2015-07-23 09:06:23 -07:00
|
|
|
var current = Zone.current[_key] as BuildLogger;
|
|
|
|
return current == null ? new PrintLogger() : current;
|
2015-02-19 12:00:09 -08:00
|
|
|
}
|
2015-04-09 17:42:14 -07:00
|
|
|
|
|
|
|
class PrintLogger implements BuildLogger {
|
2015-04-13 16:54:09 -07:00
|
|
|
@override
|
|
|
|
final String detailsUri = '';
|
|
|
|
@override
|
|
|
|
final bool convertErrorsToWarnings = false;
|
|
|
|
|
2015-04-09 17:42:14 -07:00
|
|
|
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);
|
|
|
|
}
|
2015-08-04 12:05:30 -07:00
|
|
|
|
2015-04-09 17:42:14 -07:00
|
|
|
Future writeOutput() => null;
|
|
|
|
Future addLogFilesFromAsset(AssetId id, [int nextNumber = 1]) => null;
|
|
|
|
}
|
|
|
|
|
|
|
|
class PrintLoggerError extends Error {
|
|
|
|
final String message;
|
|
|
|
final AssetId asset;
|
|
|
|
final SourceSpan span;
|
|
|
|
|
2015-04-13 16:54:09 -07:00
|
|
|
PrintLoggerError(this.message, this.asset, this.span);
|
2015-04-09 17:42:14 -07:00
|
|
|
|
|
|
|
@override
|
|
|
|
String toString() {
|
|
|
|
return 'Message: ${Error.safeToString(message)}, '
|
|
|
|
'Asset: ${Error.safeToString(asset)}, '
|
|
|
|
'Span: ${Error.safeToString(span)}.';
|
|
|
|
}
|
|
|
|
}
|
2015-08-05 17:39:37 -07:00
|
|
|
|
|
|
|
/// Generate a human-readable error message from `error`.
|
|
|
|
String _friendlyError(AnalysisError error) {
|
|
|
|
if (error.source != null) {
|
|
|
|
var file =
|
|
|
|
new SourceFile(error.source.contents.data, url: error.source.fullName);
|
|
|
|
|
|
|
|
return file
|
|
|
|
.span(error.offset, error.offset + error.length)
|
|
|
|
.message(error.message, color: false);
|
|
|
|
} else {
|
|
|
|
return '<unknown location>: ${error.message}';
|
|
|
|
}
|
|
|
|
}
|