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