OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /// This library provides a single function called injectLogs which when called | 5 /// This library provides a single function called injectLogs which when called |
6 /// will request a logs json file and build a small widget out of them which | 6 /// will request a logs json file and build a small widget out of them which |
7 /// groups the logs by level. | 7 /// groups the logs by level. |
8 library polymer.build.log_injector; | 8 library polymer.build.log_injector; |
9 | 9 |
10 import 'dart:async'; | 10 import 'dart:async'; |
11 import 'dart:convert'; | 11 import 'dart:convert'; |
12 import 'dart:html'; | 12 import 'dart:html'; |
13 | 13 |
14 import 'package:path/path.dart' as path; | 14 import 'package:path/path.dart' as path; |
| 15 import 'package:source_span/source_span.dart'; |
| 16 import 'package:code_transformers/messages/messages.dart'; |
15 | 17 |
16 class LogInjector { | 18 class LogInjector { |
17 Element selectedMenu; | 19 Element selectedMenu; |
18 Element selectedContent; | 20 Element selectedContent; |
19 | 21 |
20 // Gets the logs from a url and inject them into the dom. | 22 // Gets the logs from a url and inject them into the dom. |
21 Future injectLogsFromUrl(String url) => | 23 Future injectLogsFromUrl(String url) => |
22 HttpRequest.getString(url).then((data) => injectLogs(data)); | 24 HttpRequest.getString(url).then((data) => injectLogs(data)); |
23 | 25 |
24 // Builds the html for the logs element given some logs, and injects that | 26 // Builds the html for the logs element given some logs, and injects that |
25 // into the dom. Currently, we do not use Polymer just to ensure that the | 27 // into the dom. Currently, we do not use Polymer just to ensure that the |
26 // page works regardless of the state of the app. Ideally, we could have | 28 // page works regardless of the state of the app. Ideally, we could have |
27 // multiple scripts running independently so we could ensure that this would | 29 // multiple scripts running independently so we could ensure that this would |
28 // always be running. | 30 // always be running. |
29 injectLogs(String data) { | 31 injectLogs(String data) { |
| 32 var logs = new LogEntryTable.fromJson(JSON.decode(data)); |
| 33 if (logs.entries.isEmpty) return; |
| 34 |
30 // Group all logs by level. | 35 // Group all logs by level. |
31 var logsByLevel = { | 36 var logsByLevel = { |
32 }; | 37 }; |
33 JSON.decode(data).forEach((log) { | 38 logs.entries.values.forEach((list) => list.forEach((log) { |
34 logsByLevel.putIfAbsent(log['level'], () => []); | 39 logsByLevel.putIfAbsent(log.level, () => []); |
35 logsByLevel[log['level']].add(log); | 40 logsByLevel[log.level].add(log); |
36 }); | 41 })); |
37 if (logsByLevel.isEmpty) return; | 42 if (logsByLevel.isEmpty) return; |
38 | 43 |
39 // Build the wrapper, menu, and content divs. | 44 // Build the wrapper, menu, and content divs. |
40 | 45 |
41 var menuWrapper = new DivElement() | 46 var menuWrapper = new DivElement() |
42 ..classes.add('menu'); | 47 ..classes.add('menu'); |
43 var contentWrapper = new DivElement() | 48 var contentWrapper = new DivElement() |
44 ..classes.add('content'); | 49 ..classes.add('content'); |
45 var wrapperDiv = new DivElement() | 50 var wrapperDiv = new DivElement() |
46 ..classes.add('build-logs') | 51 ..classes.add('build-logs') |
(...skipping 30 matching lines...) Expand all Loading... |
77 } | 82 } |
78 | 83 |
79 menuItem.classes.toggle('active'); | 84 menuItem.classes.toggle('active'); |
80 contentItem.classes.toggle('active'); | 85 contentItem.classes.toggle('active'); |
81 }); | 86 }); |
82 | 87 |
83 // Add the logs to the content item. | 88 // Add the logs to the content item. |
84 for (var log in logs) { | 89 for (var log in logs) { |
85 var logHtml = new StringBuffer(); | 90 var logHtml = new StringBuffer(); |
86 logHtml.write('<div class="log">'); | 91 logHtml.write('<div class="log">'); |
87 var message = log['message'].replaceAllMapped(_urlRegex, | 92 |
| 93 var id = log.message.id; |
| 94 var hashTag = 'msg_${id.package}_${id.id}'; |
| 95 var message = new HtmlEscape().convert(log.message.snippet); |
| 96 message.replaceAllMapped(_urlRegex, |
88 (m) => '<a href="${m.group(0)}" target="blank">${m.group(0)}</a>'); | 97 (m) => '<a href="${m.group(0)}" target="blank">${m.group(0)}</a>'); |
89 logHtml.write('<div class="message $levelClassName">$message</div>'); | 98 logHtml.write('<div class="message $levelClassName">$message ' |
90 var assetId = log['assetId']; | 99 '<a target="blank" href=' |
91 var span = log['span']; | 100 '"/packages/polymer/src/build/generated/messages.html#$hashTag">' |
92 bool hasLocation = assetId != null || span != null; | 101 '(more details)</a></div>'); |
93 if (hasLocation) logHtml.write('<div class="location">'); | 102 var span = log.span; |
94 if (assetId != null) { | 103 if (span != null) { |
| 104 logHtml.write('<div class="location">'); |
| 105 var text = new HtmlEscape().convert(span.text); |
95 logHtml.write( | 106 logHtml.write( |
96 ' <span class="package">${assetId['package']}</span>:'); | 107 ' <span class="location">${span.start.toolString}</span></div>' |
97 if (span == null) { | 108 ' <span class="text">$text</span>''</div>'); |
98 logHtml.write(' <span class="location">${assetId['path']}</span>'); | |
99 } | |
100 } | |
101 if (span != null) { | |
102 logHtml.write( | |
103 ' <span class="location">${span['location']}</span></div>' | |
104 ' <span class="text">${span['text']}</span>''</div>'); | |
105 } else if (hasLocation) { | |
106 logHtml.write('</div>'); | 109 logHtml.write('</div>'); |
107 } | 110 } |
108 logHtml.write('</div>'); | 111 logHtml.write('</div>'); |
109 | 112 |
110 var logElement = new Element.html(logHtml.toString(), | 113 var logElement = new Element.html(logHtml.toString(), |
111 validator: new NodeValidatorBuilder.common() | 114 validator: new NodeValidatorBuilder.common() |
112 ..allowNavigation(new _OpenUriPolicy())); | 115 ..allowNavigation(new _OpenUriPolicy())); |
113 contentItem.append(logElement); | 116 contentItem.append(logElement); |
114 var messageElement = logElement.querySelector('.message'); | 117 var messageElement = logElement.querySelector('.message'); |
115 messageElement.onClick.listen((e) { | 118 messageElement.onClick.listen((e) { |
116 if (e.target == messageElement) { | 119 if (e.target == messageElement) { |
117 messageElement.classes.toggle('expanded'); | 120 messageElement.classes.toggle('expanded'); |
118 } | 121 } |
119 }); | 122 }); |
120 }; | 123 }; |
121 }); | 124 }); |
122 | 125 |
123 document.body.append(wrapperDiv); | 126 document.body.append(wrapperDiv); |
124 } | 127 } |
125 | 128 |
126 } | 129 } |
127 | 130 |
128 final _urlRegex = new RegExp('http://[^ ]*'); | 131 final _urlRegex = new RegExp('http://[^ ]*'); |
129 class _OpenUriPolicy implements UriPolicy { | 132 class _OpenUriPolicy implements UriPolicy { |
130 bool allowsUri(String uri) => true; | 133 bool allowsUri(String uri) => true; |
131 } | 134 } |
OLD | NEW |