Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2015, 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 logging_page; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 import 'dart:html'; | |
| 9 import 'observatory_element.dart'; | |
| 10 import 'package:logging/logging.dart'; | |
| 11 import 'package:observatory/service.dart'; | |
| 12 import 'package:observatory/app.dart'; | |
| 13 import 'package:observatory/elements.dart'; | |
| 14 import 'package:observatory/utils.dart'; | |
| 15 import 'package:polymer/polymer.dart'; | |
| 16 | |
| 17 @CustomTag('logging-page') | |
| 18 class LoggingPageElement extends ObservatoryElement { | |
| 19 static const _kPageSelector = '#page'; | |
| 20 static const _kLogSelector = '#log'; | |
| 21 static const _kSeverityLevelSelector = '#severityLevelSelector'; | |
| 22 | |
| 23 LoggingPageElement.created() : super.created(); | |
| 24 | |
| 25 domReady() { | |
| 26 super.domReady(); | |
| 27 _insertLevels(); | |
| 28 } | |
| 29 | |
| 30 attached() { | |
| 31 super.attached(); | |
| 32 _sub(); | |
| 33 _resizeSubscription = window.onResize.listen((_) => _updatePageHeight()); | |
| 34 _updatePageHeight(); | |
| 35 // Turn on the periodic poll timer for this page. | |
| 36 pollPeriod = const Duration(milliseconds:100); | |
| 37 } | |
| 38 | |
| 39 detached() { | |
| 40 super.detached(); | |
| 41 if (_resizeSubscription != null) { | |
| 42 _resizeSubscription.cancel(); | |
| 43 _resizeSubscription = null; | |
| 44 } | |
| 45 _unsub(); | |
| 46 } | |
| 47 | |
| 48 void onPoll() { | |
| 49 _flushPendingLogs(); | |
| 50 } | |
| 51 | |
| 52 _updatePageHeight() { | |
| 53 HtmlElement e = shadowRoot.querySelector(_kPageSelector); | |
| 54 final totalHeight = window.innerHeight; | |
| 55 final top = e.offset.top; | |
| 56 final bottomMargin = 32; | |
| 57 final mainHeight = totalHeight - top - bottomMargin; | |
| 58 e.style.setProperty('height', '${mainHeight}px'); | |
| 59 } | |
| 60 | |
| 61 _insertLevels() { | |
| 62 SelectElement severityLevelSelector = | |
| 63 shadowRoot.querySelector(_kSeverityLevelSelector); | |
| 64 severityLevelSelector.children.clear(); | |
| 65 _maxLevelLabelLength = 0; | |
| 66 for (var level in Level.LEVELS) { | |
| 67 var option = new OptionElement(); | |
| 68 option.value = level.value.toString(); | |
| 69 option.label = level.name; | |
| 70 if (level.name.length > _maxLevelLabelLength) { | |
| 71 _maxLevelLabelLength = level.name.length; | |
| 72 } | |
| 73 severityLevelSelector.children.add(option); | |
| 74 } | |
| 75 severityLevelSelector.selectedIndex = 0; | |
| 76 severityLevel = Level.ALL.value.toString(); | |
| 77 } | |
| 78 | |
| 79 _reset() { | |
| 80 logRecords.clear(); | |
| 81 _unsub(); | |
| 82 _sub(); | |
| 83 _renderFull(); | |
| 84 } | |
| 85 | |
| 86 _unsub() { | |
| 87 cancelFutureSubscription(_loggingSubscriptionFuture); | |
| 88 _loggingSubscriptionFuture = null; | |
| 89 } | |
| 90 | |
| 91 _sub() { | |
| 92 if (_loggingSubscriptionFuture != null) { | |
| 93 // Already subscribed. | |
| 94 return; | |
| 95 } | |
| 96 _loggingSubscriptionFuture = | |
| 97 app.vm.listenEventStream(Isolate.kLoggingStream, _onEvent); | |
| 98 } | |
| 99 | |
| 100 _append(Map logRecord) { | |
| 101 logRecords.add(logRecord); | |
| 102 if (_shouldDisplay(logRecord)) { | |
| 103 // Queue for display. | |
| 104 pendingLogRecords.add(logRecord); | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 Element _renderAppend(Map logRecord) { | |
| 109 DivElement logContainer = shadowRoot.querySelector(_kLogSelector); | |
| 110 var element = new DivElement(); | |
| 111 element.classes.add('logItem'); | |
| 112 element.classes.add(logRecord['level'].name); | |
| 113 element.appendText( | |
| 114 '${logRecord["level"].name.padLeft(_maxLevelLabelLength)} ' | |
| 115 '${Utils.formatDateTime(logRecord["time"])} ' | |
| 116 '${logRecord["message"].valueAsString}\n'); | |
| 117 logContainer.children.add(element); | |
| 118 return element; | |
| 119 } | |
| 120 | |
| 121 _renderFull() { | |
| 122 DivElement logContainer = shadowRoot.querySelector(_kLogSelector); | |
| 123 logContainer.children.clear(); | |
| 124 pendingLogRecords.clear(); | |
| 125 var lastElement; | |
| 126 for (var logRecord in logRecords) { | |
| 127 if (_shouldDisplay(logRecord)) { | |
| 128 lastElement = _renderAppend(logRecord); | |
| 129 } | |
| 130 } | |
| 131 if (lastElement != null) { | |
| 132 lastElement.scrollIntoView(); | |
| 133 } | |
| 134 } | |
| 135 | |
| 136 _flushPendingLogs() { | |
| 137 var lastElement; | |
| 138 for (var logRecord in pendingLogRecords) { | |
| 139 lastElement = _renderAppend(logRecord); | |
| 140 } | |
| 141 if (lastElement != null) { | |
|
rmacnak
2015/07/22 00:29:26
Should only scroll if already positioned at the en
Cutch
2015/07/22 21:33:32
Done.
| |
| 142 lastElement.scrollIntoView(); | |
| 143 } | |
| 144 pendingLogRecords.clear(); | |
| 145 } | |
| 146 | |
| 147 _onEvent(ServiceEvent event) { | |
| 148 assert(event.kind == VM.kLoggingStream); | |
|
rmacnak
2015/07/22 00:29:26
Isolate.kLoggingStream
Cutch
2015/07/22 21:33:32
Done.
| |
| 149 _append(event.logRecord); | |
| 150 } | |
| 151 | |
| 152 void isolateChanged(oldValue) { | |
| 153 _reset(); | |
| 154 } | |
| 155 | |
| 156 void severityLevelChanged(oldValue) { | |
| 157 _severityLevelValue = int.parse(severityLevel); | |
| 158 _renderFull(); | |
| 159 } | |
| 160 | |
| 161 Future clear() { | |
| 162 logRecords.clear(); | |
| 163 pendingLogRecords.clear(); | |
| 164 _renderFull(); | |
| 165 return new Future.value(null); | |
| 166 } | |
| 167 | |
| 168 bool _shouldDisplay(Map logRecord) { | |
| 169 return logRecord['level'].value >= _severityLevelValue; | |
| 170 } | |
| 171 | |
| 172 @observable Isolate isolate; | |
| 173 @observable String severityLevel; | |
| 174 int _severityLevelValue = 0; | |
| 175 int _maxLevelLabelLength = 0; | |
| 176 Future<StreamSubscription> _loggingSubscriptionFuture; | |
| 177 StreamSubscription _resizeSubscription; | |
| 178 final List<Map> logRecords = new List<Map>(); | |
| 179 final List<Map> pendingLogRecords = new List<Map>(); | |
| 180 } | |
| OLD | NEW |