| 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 // Reads all log files for an Asset into [logs]. | |
| 68 static Future _readLogFilesForAsset( | |
| 69 AssetId id, Transform transform, List<Map> logs, [nextNumber = 1]) { | |
| 70 var nextAssetPath = id.addExtension('${common.LOG_EXTENSION}.$nextNumber'); | |
| 71 return transform.hasInput(nextAssetPath).then((exists) { | |
| 72 if (!exists) return null; | |
| 73 return transform.readInputAsString(nextAssetPath).then((data) { | |
| 74 logs.addAll(JSON.decode(data)); | |
| 75 return _readLogFilesForAsset(id, transform, logs, ++nextNumber); | |
| 76 }); | |
| 77 }); | |
| 78 } | |
| 79 | |
| 80 // Combines all existing ._buildLogs.* files into a single ._buildLogs file. | |
| 81 static Future combineLogFiles(Transform transform) { | |
| 82 var logs = new List<Map>(); | |
| 83 var id = transform.primaryInput.id; | |
| 84 return _readLogFilesForAsset(id, transform, logs).then((_) { | |
| 85 return transform.addOutput(new Asset.fromString( | |
| 86 id.addExtension(common.LOG_EXTENSION), | |
| 87 JSON.encode(logs))); | |
| 88 }); | |
| 89 } | |
| 90 | |
| 91 // Reads all logs for an asset and adds them to this loggers log output. | |
| 92 Future addLogFilesFromAsset(AssetId id, [int nextNumber = 1]) { | |
| 93 return _readLogFilesForAsset(id, _transform, _logs); | |
| 94 } | |
| 95 | |
| 96 void _addLog(AssetId assetId, LogLevel level, String message, | |
| 97 SourceSpan span) { | |
| 98 var data = { | |
| 99 'level': level.name, | |
| 100 'message': const HtmlEscape().convert(message), | |
| 101 }; | |
| 102 if (assetId != null) { | |
| 103 data['assetId'] = { | |
| 104 'package': assetId.package, | |
| 105 'path': assetId.path, | |
| 106 }; | |
| 107 } | |
| 108 if (span != null) { | |
| 109 data['span'] = { | |
| 110 'location': span.start.toolString, | |
| 111 'text': new HtmlEscape().convert(span.text), | |
| 112 }; | |
| 113 } | |
| 114 _logs.add(data); | |
| 115 } | |
| 116 } | |
| OLD | NEW |