| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2014, 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 engine.incremental_logger; |
| 6 |
| 7 |
| 8 /** |
| 9 * The shared instance of [Logger] used by several incremental resolution |
| 10 * classes. It is initialized externally by the Analysis Engine client. |
| 11 */ |
| 12 Logger logger = new NullLogger(); |
| 13 |
| 14 |
| 15 /** |
| 16 * A simple hierarchical logger. |
| 17 */ |
| 18 abstract class Logger { |
| 19 /** |
| 20 * Mark an enter to a new section with the given [name]. |
| 21 */ |
| 22 void enter(String name); |
| 23 |
| 24 /** |
| 25 * Mark an exit from the current sections, logs the duration. |
| 26 */ |
| 27 void exit(); |
| 28 |
| 29 /** |
| 30 * Logs the given [obj]. |
| 31 */ |
| 32 void log(Object obj); |
| 33 } |
| 34 |
| 35 |
| 36 /** |
| 37 * A [Logger] that does nothing. |
| 38 */ |
| 39 class NullLogger implements Logger { |
| 40 @override |
| 41 void enter(String name) { |
| 42 } |
| 43 |
| 44 @override |
| 45 void exit() { |
| 46 } |
| 47 |
| 48 @override |
| 49 void log(Object obj) { |
| 50 } |
| 51 } |
| 52 |
| 53 |
| 54 /** |
| 55 * A [Logger] that writes to a [StringSink]. |
| 56 */ |
| 57 class StringSinkLogger implements Logger { |
| 58 static const int _MAX_LINE_LENGTH = 512; |
| 59 final StringSink _sink; |
| 60 final List<_LoggerSection> _sectionStack = <_LoggerSection>[]; |
| 61 _LoggerSection _section = new _LoggerSection('', 'ROOT'); |
| 62 |
| 63 StringSinkLogger(this._sink); |
| 64 |
| 65 @override |
| 66 void enter(String name) { |
| 67 log('+++ $name'); |
| 68 _sectionStack.add(_section); |
| 69 _section = new _LoggerSection(_section.indent + '\t', name); |
| 70 } |
| 71 |
| 72 @override |
| 73 void exit() { |
| 74 DateTime now = new DateTime.now(); |
| 75 Duration duration = now.difference(_section.start); |
| 76 String message = '--- ${_section.name} in ${duration.inMilliseconds} ms'; |
| 77 _section = _sectionStack.removeLast(); |
| 78 log(message); |
| 79 } |
| 80 |
| 81 @override |
| 82 void log(Object obj) { |
| 83 DateTime now = new DateTime.now(); |
| 84 String indent = _section.indent; |
| 85 String objStr = _getObjectString(obj); |
| 86 String line = '[$now] $indent$objStr'; |
| 87 _sink.writeln(line); |
| 88 } |
| 89 |
| 90 String _getObjectString(Object obj) { |
| 91 if (obj == null) { |
| 92 return 'null'; |
| 93 } |
| 94 String str = obj.toString(); |
| 95 if (str.length < _MAX_LINE_LENGTH) { |
| 96 return str; |
| 97 } |
| 98 return str.split('\n').map((String line) { |
| 99 if (line.length > _MAX_LINE_LENGTH) { |
| 100 line = line.substring(0, _MAX_LINE_LENGTH) + '...'; |
| 101 } |
| 102 return line; |
| 103 }).join('\n'); |
| 104 } |
| 105 } |
| 106 |
| 107 |
| 108 class _LoggerSection { |
| 109 final DateTime start = new DateTime.now(); |
| 110 final String indent; |
| 111 final String name; |
| 112 _LoggerSection(this.indent, this.name); |
| 113 } |
| OLD | NEW |