Chromium Code Reviews| Index: pkg/polymer/lib/src/build/wrapped_logger.dart |
| diff --git a/pkg/polymer/lib/src/build/wrapped_logger.dart b/pkg/polymer/lib/src/build/wrapped_logger.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..aca6c342d4f508bcf3af197ad8a8846850ecfd32 |
| --- /dev/null |
| +++ b/pkg/polymer/lib/src/build/wrapped_logger.dart |
| @@ -0,0 +1,100 @@ |
| +library polymer.src.build.wrapped_logger; |
|
Siggi Cherem (dart-lang)
2014/08/01 21:48:29
+ copyright here too
jakemac
2014/08/04 19:49:59
Done.
|
| + |
| +import 'dart:async'; |
| +import 'dart:convert'; |
| + |
| +import 'package:barback/barback.dart'; |
| + |
| +import 'common.dart' as common; |
| + |
| +/// A simple class to wrap one TransformLogger with another one that writes all |
| +/// logs to a file and then forwards the calls to the child. |
| +class WrappedLogger implements TransformLogger { |
| + Transform _transform; |
| + List<Map> _logs = new List<Map>(); |
| + |
| + bool convertErrorsToWarnings; |
| + |
| + WrappedLogger(this._transform, {this.convertErrorsToWarnings: false}); |
| + |
| + void info(String message, {AssetId asset, Span span}) { |
| + _transform.logger.info(message, asset: asset, span: span); |
| + _addLog(asset, LogLevel.INFO, message, span); |
| + } |
| + |
| + void fine(String message, {AssetId asset, Span span}) { |
| + _transform.logger.fine(message, asset: asset, span: span); |
| + _addLog(asset, LogLevel.FINE, message, span); |
| + } |
| + |
| + void warning(String message, {AssetId asset, Span span}) { |
| + _transform.logger.warning(message, asset: asset, span: span); |
| + _addLog(asset, LogLevel.WARNING, message, span); |
| + } |
| + |
| + void error(String message, {AssetId asset, Span span}) { |
| + if (convertErrorsToWarnings) { |
| + _transform.logger.warning(message, asset: asset, span: span); |
| + } else { |
| + _transform.logger.error(message, asset: asset, span: span); |
| + } |
| + _addLog(asset, LogLevel.ERROR, message, span); |
| + } |
| + |
| + /// Outputs the log data to a JSON serialized file. |
| + Future writeOutput() { |
| + return getNextLogAssetPath().then((path) { |
| + _transform.addOutput(new Asset.fromString(path, JSON.encode(_logs))); |
| + }); |
| + } |
| + |
| + // Each phase outputs a new log file with an incrementing # appended, this |
| + // figures out the next # to use. |
|
Siggi Cherem (dart-lang)
2014/08/01 21:31:53
include a TODO that this is a workaround because o
jakemac
2014/08/04 19:49:59
This just seemed easier to work with, I started go
|
| + Future<String> getNextLogAssetPath([int nextNumber = 1]) { |
| + var nextAssetPath = _transform.primaryInput.id.addExtension( |
| + '${common.LOG_EXTENSION}.$nextNumber'); |
| + return _transform.hasInput(nextAssetPath).then((exists) { |
| + if (!exists) return nextAssetPath; |
| + return getNextLogAssetPath(++nextNumber); |
|
Siggi Cherem (dart-lang)
2014/08/01 21:31:53
interesting ... another idea, but I'm not convince
jakemac
2014/08/04 19:49:59
I think I like just the incrementing number approa
|
| + }); |
| + } |
| + |
| + // Combines all existing ._buildLogs.* files into a single ._buildLogs file. |
| + static Future combineLogFiles( |
| + Transform transform, [int nextNumber = 1, List<Map> logs]) { |
| + if (logs == null) logs = new List<Map>(); |
| + var primaryInputId = transform.primaryInput.id; |
| + var nextAssetPath = |
| + primaryInputId.addExtension('${common.LOG_EXTENSION}.$nextNumber'); |
| + return transform.readInputAsString(nextAssetPath).then( |
| + (data) { |
| + logs.addAll(JSON.decode(data)); |
| + return combineLogFiles(transform, ++nextNumber, logs); |
| + }, |
| + onError: (_) { |
| + transform.addOutput(new Asset.fromString( |
| + primaryInputId.addExtension(common.LOG_EXTENSION), |
| + JSON.encode(logs))); |
| + }); |
| + } |
| + |
| + void _addLog(AssetId assetId, LogLevel level, String message, Span span) { |
| + var data = { |
| + 'level': level.name, |
| + 'message': message, |
| + }; |
| + if (assetId != null) { |
| + data['assetId'] = { |
| + 'package': assetId.package, |
| + 'path': assetId.path, |
| + }; |
| + } |
| + if (span != null) { |
| + data['span'] = { |
| + 'location': span.formatLocation, |
| + 'text': new HtmlEscape().convert(span.text), |
| + }; |
| + } |
| + _logs.add(data); |
| + } |
| +} |