Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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 library engine.incremental_logger; | 5 library engine.incremental_logger; |
| 6 | 6 |
| 7 | 7 |
| 8 /** | 8 /** |
| 9 * The shared instance of [Logger] used by several incremental resolution | 9 * The shared instance of [Logger] used by several incremental resolution |
| 10 * classes. It is initialized externally by the Analysis Engine client. | 10 * classes. It is initialized externally by the Analysis Engine client. |
| 11 */ | 11 */ |
| 12 Logger logger = new NullLogger(); | 12 Logger logger = NULL_LOGGER; |
| 13 | |
| 14 /** | |
| 15 * An instance of [Logger] that does not print anything. | |
| 16 */ | |
| 17 final Logger NULL_LOGGER = new _NullLogger(); | |
| 18 | |
| 19 /** | |
| 20 * An instance of [Logger] that uses `print` for output. | |
| 21 */ | |
| 22 final Logger PRINT_LOGGER = new StringSinkLogger(new _PrintStringSink()); | |
| 13 | 23 |
| 14 | 24 |
| 15 /** | 25 /** |
| 16 * A simple hierarchical logger. | 26 * A simple hierarchical logger. |
| 17 */ | 27 */ |
| 18 abstract class Logger { | 28 abstract class Logger { |
| 19 /** | 29 /** |
| 20 * Mark an enter to a new section with the given [name]. | 30 * Mark an enter to a new section with the given [name]. |
| 21 */ | 31 */ |
| 22 void enter(String name); | 32 void enter(String name); |
| 23 | 33 |
| 24 /** | 34 /** |
| 25 * Mark an exit from the current sections, logs the duration. | 35 * Mark an exit from the current sections, logs the duration. |
| 26 */ | 36 */ |
| 27 void exit(); | 37 void exit(); |
| 28 | 38 |
| 29 /** | 39 /** |
| 30 * Logs the given [obj]. | 40 * Logs the given [obj]. |
| 31 */ | 41 */ |
| 32 void log(Object obj); | 42 void log(Object obj); |
| 43 | |
| 44 /** | |
| 45 * Starts a new timer. | |
| 46 */ | |
| 47 LoggerTimer startTimer(); | |
| 33 } | 48 } |
| 34 | 49 |
| 35 | 50 |
| 36 /** | 51 /** |
| 37 * A [Logger] that does nothing. | 52 * The handle of a timer. |
| 38 */ | 53 */ |
| 39 class NullLogger implements Logger { | 54 class LoggerTimer { |
|
Brian Wilkerson
2014/12/03 19:31:12
nit: Perhaps "LoggingTimer"?
scheglov
2014/12/03 19:46:35
Done.
| |
| 40 @override | 55 final Logger _logger; |
| 41 void enter(String name) { | 56 final Stopwatch _stopwatch = new Stopwatch(); |
| 57 | |
| 58 LoggerTimer(this._logger) { | |
| 59 _stopwatch.start(); | |
| 42 } | 60 } |
| 43 | 61 |
| 44 @override | 62 /** |
| 45 void exit() { | 63 * This methods stop the timer and logs the elapsed time. |
| 46 } | 64 */ |
| 47 | 65 void stop(String message) { |
| 48 @override | 66 _stopwatch.stop(); |
| 49 void log(Object obj) { | 67 _logger.log('$message in ${_stopwatch.elapsedMilliseconds} ms'); |
| 50 } | 68 } |
| 51 } | 69 } |
| 52 | 70 |
| 53 | 71 |
| 54 /** | 72 /** |
| 55 * A [Logger] that writes to a [StringSink]. | 73 * A [Logger] that writes to a [StringSink]. |
| 56 */ | 74 */ |
| 57 class StringSinkLogger implements Logger { | 75 class StringSinkLogger implements Logger { |
| 58 static const int _MAX_LINE_LENGTH = 512; | 76 static const int _MAX_LINE_LENGTH = 512; |
| 59 final StringSink _sink; | 77 final StringSink _sink; |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 80 | 98 |
| 81 @override | 99 @override |
| 82 void log(Object obj) { | 100 void log(Object obj) { |
| 83 DateTime now = new DateTime.now(); | 101 DateTime now = new DateTime.now(); |
| 84 String indent = _section.indent; | 102 String indent = _section.indent; |
| 85 String objStr = _getObjectString(obj); | 103 String objStr = _getObjectString(obj); |
| 86 String line = '[$now] $indent$objStr'; | 104 String line = '[$now] $indent$objStr'; |
| 87 _sink.writeln(line); | 105 _sink.writeln(line); |
| 88 } | 106 } |
| 89 | 107 |
| 108 @override | |
| 109 LoggerTimer startTimer() { | |
| 110 return new LoggerTimer(this); | |
| 111 } | |
| 112 | |
| 90 String _getObjectString(Object obj) { | 113 String _getObjectString(Object obj) { |
| 91 if (obj == null) { | 114 if (obj == null) { |
| 92 return 'null'; | 115 return 'null'; |
| 93 } | 116 } |
| 117 if (obj is Function) { | |
| 118 obj = obj(); | |
| 119 } | |
| 94 String str = obj.toString(); | 120 String str = obj.toString(); |
| 95 if (str.length < _MAX_LINE_LENGTH) { | 121 if (str.length < _MAX_LINE_LENGTH) { |
| 96 return str; | 122 return str; |
| 97 } | 123 } |
| 98 return str.split('\n').map((String line) { | 124 return str.split('\n').map((String line) { |
| 99 if (line.length > _MAX_LINE_LENGTH) { | 125 if (line.length > _MAX_LINE_LENGTH) { |
| 100 line = line.substring(0, _MAX_LINE_LENGTH) + '...'; | 126 line = line.substring(0, _MAX_LINE_LENGTH) + '...'; |
| 101 } | 127 } |
| 102 return line; | 128 return line; |
| 103 }).join('\n'); | 129 }).join('\n'); |
| 104 } | 130 } |
| 105 } | 131 } |
| 106 | 132 |
| 107 | 133 |
| 108 class _LoggerSection { | 134 class _LoggerSection { |
| 109 final DateTime start = new DateTime.now(); | 135 final DateTime start = new DateTime.now(); |
| 110 final String indent; | 136 final String indent; |
| 111 final String name; | 137 final String name; |
| 112 _LoggerSection(this.indent, this.name); | 138 _LoggerSection(this.indent, this.name); |
| 113 } | 139 } |
| 140 | |
| 141 | |
| 142 /** | |
| 143 * A [Logger] that does nothing. | |
| 144 */ | |
| 145 class _NullLogger implements Logger { | |
| 146 @override | |
| 147 void enter(String name) { | |
| 148 } | |
| 149 | |
| 150 @override | |
| 151 void exit() { | |
| 152 } | |
| 153 | |
| 154 @override | |
| 155 void log(Object obj) { | |
| 156 } | |
| 157 | |
| 158 @override | |
| 159 LoggerTimer startTimer() { | |
| 160 return new LoggerTimer(this); | |
| 161 } | |
| 162 } | |
| 163 | |
| 164 | |
| 165 /** | |
| 166 * A [StringSink] implementation that uses `print`. | |
| 167 */ | |
| 168 class _PrintStringSink implements StringSink { | |
| 169 String _line = ''; | |
| 170 | |
| 171 @override | |
| 172 void write(Object obj) { | |
| 173 if (obj == null) { | |
| 174 _line += 'null'; | |
| 175 } else { | |
| 176 _line += obj.toString(); | |
| 177 } | |
| 178 } | |
| 179 | |
| 180 @override | |
| 181 void writeAll(Iterable objects, [String separator = '']) { | |
| 182 _line += objects.join(separator); | |
| 183 } | |
| 184 | |
| 185 @override | |
| 186 void writeCharCode(int charCode) { | |
| 187 _line += new String.fromCharCode(charCode); | |
| 188 } | |
| 189 | |
| 190 @override | |
| 191 void writeln([Object obj = '']) { | |
| 192 _line += obj; | |
| 193 print(_line); | |
| 194 _line = ''; | |
| 195 } | |
| 196 } | |
| OLD | NEW |