Skip to content

Commit ff00313

Browse files
authored
improve the display of json (#21)
* improve the display of json * review comments
1 parent e3ace22 commit ff00313

File tree

2 files changed

+56
-23
lines changed

2 files changed

+56
-23
lines changed

lib/logging/logging.dart

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'dart:convert';
6+
57
import 'package:intl/intl.dart';
68
import 'package:logging/logging.dart';
79
import 'package:vm_service_lib/vm_service_lib.dart';
@@ -58,10 +60,9 @@ class LoggingScreen extends Screen {
5860
..add(logDetailsUI = new LogDetailsUI()),
5961
]);
6062

61-
// TODO(dantup): Can we (should we?) detect when the content is overflowed
62-
// in the table, and only show the defaults?
63-
loggingTable.onSelect
64-
.listen((LogData selection) => logDetailsUI.data = selection);
63+
loggingTable.onSelect.listen((LogData selection) {
64+
logDetailsUI.setData(selection);
65+
});
6566
}
6667

6768
CoreElement _createTableView() {
@@ -92,29 +93,52 @@ class LoggingScreen extends Screen {
9293
return;
9394
}
9495

95-
// TODO(devoncarew): inspect, ...
96+
// TODO(devoncarew): Add support for additional events, like 'inspect', ...
9697

97-
// Log stdout and stderr events.
98+
// Log stdout events.
9899
service.onStdoutEvent.listen((Event e) {
99100
final String message = decodeBase64(e.bytes);
100-
String summary;
101+
String summary = message;
101102
if (message.length > 200) {
102103
summary = message.substring(0, 200) + '…';
103104
}
105+
summary = summary.replaceAll('\t', r'\t');
106+
summary = summary.replaceAll('\r', r'\r');
107+
summary = summary.replaceAll('\n', r'\n');
104108
_log(new LogData('stdout', message, e.timestamp, summary: summary));
105109
});
110+
111+
// Log stderr events.
106112
service.onStderrEvent.listen((Event e) {
107113
final String message = decodeBase64(e.bytes);
108-
_log(new LogData('stderr', message, e.timestamp, isError: true));
114+
String summary = message;
115+
if (message.length > 200) {
116+
summary = message.substring(0, 200) + '…';
117+
}
118+
summary = summary.replaceAll('\t', r'\t');
119+
summary = summary.replaceAll('\r', r'\r');
120+
summary = summary.replaceAll('\n', r'\n');
121+
_log(new LogData(
122+
'stderr',
123+
message,
124+
e.timestamp,
125+
summary: summary,
126+
isError: true,
127+
));
109128
});
110129

111130
// Log GC events.
112131
service.onGCEvent.listen((Event e) {
113-
final dynamic json = e.json;
114-
final String message = 'gc reason: ${json['reason']}\n'
115-
'new: ${json['new']}\n'
116-
'old: ${json['old']}\n';
117-
_log(new LogData('gc', message, e.timestamp));
132+
final String summary =
133+
'${e.json['reason']} collection, ${e.json['isolate']['name']}';
134+
final Map<String, dynamic> event = <String, dynamic>{
135+
'reason': e.json['reason'],
136+
'new': e.json['new'],
137+
'old': e.json['old'],
138+
'isolate': e.json['isolate'],
139+
};
140+
final String message = jsonEncode(event);
141+
_log(new LogData('gc', message, e.timestamp, summary: summary));
118142
});
119143

120144
// Log `dart:developer` `log` events.
@@ -315,7 +339,7 @@ String getCssClassForEventKind(LogData item) {
315339
}
316340

317341
class LogDetailsUI extends CoreElement {
318-
LogData _data;
342+
static const JsonEncoder jsonEncoder = JsonEncoder.withIndent(' ');
319343

320344
CoreElement content, message;
321345

@@ -328,14 +352,22 @@ class LogDetailsUI extends CoreElement {
328352
]);
329353
}
330354

331-
LogData get data => _data;
332-
333-
set data(LogData value) {
334-
_data = value;
335-
336-
if (_data != null) {
337-
// TODO(dantup): Can we format the JSON better?
338-
message.text = _data.message;
355+
void setData(LogData data) {
356+
// Reset the vertical scroll value if any.
357+
content.element.scrollTop = 0;
358+
359+
if (data != null) {
360+
if (data.message.startsWith('{') && data.message.endsWith('}')) {
361+
try {
362+
// If the string decodes properly, than format the json.
363+
final dynamic result = jsonDecode(data.message);
364+
message.text = jsonEncoder.convert(result);
365+
} catch (e) {
366+
message.text = data.message;
367+
}
368+
} else {
369+
message.text = data.message;
370+
}
339371
} else {
340372
message.text = '';
341373
}

web/styles.css

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,8 @@ div.tabnav {
470470

471471
.log-details {
472472
height: 120px;
473-
overflow: scroll;
473+
overflow-y: scroll;
474+
overflow-wrap: break-word;
474475
}
475476

476477
.frame-panel {

0 commit comments

Comments
 (0)