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