OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library polymer.src.build.wrapped_logger; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:convert'; |
| 9 |
| 10 import 'package:barback/barback.dart'; |
| 11 |
| 12 import 'common.dart' as common; |
| 13 |
| 14 /// A simple class to wrap one TransformLogger with another one that writes all |
| 15 /// logs to a file and then forwards the calls to the child. |
| 16 class WrappedLogger implements TransformLogger { |
| 17 Transform _transform; |
| 18 List<Map> _logs = new List<Map>(); |
| 19 |
| 20 bool convertErrorsToWarnings; |
| 21 |
| 22 WrappedLogger(this._transform, {this.convertErrorsToWarnings: false}); |
| 23 |
| 24 void info(String message, {AssetId asset, Span span}) { |
| 25 _transform.logger.info(message, asset: asset, span: span); |
| 26 _addLog(asset, LogLevel.INFO, message, span); |
| 27 } |
| 28 |
| 29 void fine(String message, {AssetId asset, Span span}) { |
| 30 _transform.logger.fine(message, asset: asset, span: span); |
| 31 _addLog(asset, LogLevel.FINE, message, span); |
| 32 } |
| 33 |
| 34 void warning(String message, {AssetId asset, Span span}) { |
| 35 _transform.logger.warning(message, asset: asset, span: span); |
| 36 _addLog(asset, LogLevel.WARNING, message, span); |
| 37 } |
| 38 |
| 39 void error(String message, {AssetId asset, Span span}) { |
| 40 if (convertErrorsToWarnings) { |
| 41 _transform.logger.warning(message, asset: asset, span: span); |
| 42 } else { |
| 43 _transform.logger.error(message, asset: asset, span: span); |
| 44 } |
| 45 _addLog(asset, LogLevel.ERROR, message, span); |
| 46 } |
| 47 |
| 48 /// Outputs the log data to a JSON serialized file. |
| 49 Future writeOutput() { |
| 50 return getNextLogAssetPath().then((path) { |
| 51 _transform.addOutput(new Asset.fromString(path, JSON.encode(_logs))); |
| 52 }); |
| 53 } |
| 54 |
| 55 // Each phase outputs a new log file with an incrementing # appended, this |
| 56 // figures out the next # to use. |
| 57 Future<String> getNextLogAssetPath([int nextNumber = 1]) { |
| 58 var nextAssetPath = _transform.primaryInput.id.addExtension( |
| 59 '${common.LOG_EXTENSION}.$nextNumber'); |
| 60 return _transform.hasInput(nextAssetPath).then((exists) { |
| 61 if (!exists) return nextAssetPath; |
| 62 return getNextLogAssetPath(++nextNumber); |
| 63 }); |
| 64 } |
| 65 |
| 66 // Combines all existing ._buildLogs.* files into a single ._buildLogs file. |
| 67 static Future combineLogFiles( |
| 68 Transform transform, [int nextNumber = 1, List<Map> logs]) { |
| 69 if (logs == null) logs = new List<Map>(); |
| 70 var primaryInputId = transform.primaryInput.id; |
| 71 var nextAssetPath = |
| 72 primaryInputId.addExtension('${common.LOG_EXTENSION}.$nextNumber'); |
| 73 return transform.readInputAsString(nextAssetPath).then( |
| 74 (data) { |
| 75 logs.addAll(JSON.decode(data)); |
| 76 return combineLogFiles(transform, ++nextNumber, logs); |
| 77 }, |
| 78 onError: (_) { |
| 79 transform.addOutput(new Asset.fromString( |
| 80 primaryInputId.addExtension(common.LOG_EXTENSION), |
| 81 JSON.encode(logs))); |
| 82 }); |
| 83 } |
| 84 |
| 85 void _addLog(AssetId assetId, LogLevel level, String message, Span span) { |
| 86 var data = { |
| 87 'level': level.name, |
| 88 'message': message, |
| 89 }; |
| 90 if (assetId != null) { |
| 91 data['assetId'] = { |
| 92 'package': assetId.package, |
| 93 'path': assetId.path, |
| 94 }; |
| 95 } |
| 96 if (span != null) { |
| 97 data['span'] = { |
| 98 'location': span.formatLocation, |
| 99 'text': new HtmlEscape().convert(span.text), |
| 100 }; |
| 101 } |
| 102 _logs.add(data); |
| 103 } |
| 104 } |
OLD | NEW |