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 | |
8 /** | 7 /** |
9 * The shared instance of [Logger] used by several incremental resolution | 8 * The shared instance of [Logger] used by several incremental resolution |
10 * classes. It is initialized externally by the Analysis Engine client. | 9 * classes. It is initialized externally by the Analysis Engine client. |
11 */ | 10 */ |
12 Logger logger = NULL_LOGGER; | 11 Logger logger = NULL_LOGGER; |
13 | 12 |
14 /** | 13 /** |
15 * An instance of [Logger] that does not print anything. | 14 * An instance of [Logger] that does not print anything. |
16 */ | 15 */ |
17 final Logger NULL_LOGGER = new _NullLogger(); | 16 final Logger NULL_LOGGER = new _NullLogger(); |
18 | 17 |
19 /** | 18 /** |
20 * An instance of [Logger] that uses `print` for output. | 19 * An instance of [Logger] that uses `print` for output. |
21 */ | 20 */ |
22 final Logger PRINT_LOGGER = new StringSinkLogger(new _PrintStringSink()); | 21 final Logger PRINT_LOGGER = new StringSinkLogger(new _PrintStringSink()); |
23 | 22 |
24 | |
25 /** | 23 /** |
26 * A simple hierarchical logger. | 24 * A simple hierarchical logger. |
27 */ | 25 */ |
28 abstract class Logger { | 26 abstract class Logger { |
29 /** | 27 /** |
30 * Mark an enter to a new section with the given [name]. | 28 * Mark an enter to a new section with the given [name]. |
31 */ | 29 */ |
32 void enter(String name); | 30 void enter(String name); |
33 | 31 |
34 /** | 32 /** |
35 * Mark an exit from the current sections, logs the duration. | 33 * Mark an exit from the current sections, logs the duration. |
36 */ | 34 */ |
37 void exit(); | 35 void exit(); |
38 | 36 |
39 /** | 37 /** |
40 * Logs the given [obj]. | 38 * Logs the given [obj]. |
41 */ | 39 */ |
42 void log(Object obj); | 40 void log(Object obj); |
43 | 41 |
44 /** | 42 /** |
45 * Starts a new timer. | 43 * Starts a new timer. |
46 */ | 44 */ |
47 LoggingTimer startTimer(); | 45 LoggingTimer startTimer(); |
48 } | 46 } |
49 | 47 |
50 | |
51 /** | 48 /** |
52 * The handle of a timer. | 49 * The handle of a timer. |
53 */ | 50 */ |
54 class LoggingTimer { | 51 class LoggingTimer { |
55 final Logger _logger; | 52 final Logger _logger; |
56 final Stopwatch _stopwatch = new Stopwatch(); | 53 final Stopwatch _stopwatch = new Stopwatch(); |
57 | 54 |
58 LoggingTimer(this._logger) { | 55 LoggingTimer(this._logger) { |
59 _stopwatch.start(); | 56 _stopwatch.start(); |
60 } | 57 } |
61 | 58 |
62 /** | 59 /** |
63 * This methods stop the timer and logs the elapsed time. | 60 * This methods stop the timer and logs the elapsed time. |
64 */ | 61 */ |
65 void stop(String message) { | 62 void stop(String message) { |
66 _stopwatch.stop(); | 63 _stopwatch.stop(); |
67 _logger.log('$message in ${_stopwatch.elapsedMilliseconds} ms'); | 64 _logger.log('$message in ${_stopwatch.elapsedMilliseconds} ms'); |
68 } | 65 } |
69 } | 66 } |
70 | 67 |
71 | |
72 /** | 68 /** |
73 * A [Logger] that writes to a [StringSink]. | 69 * A [Logger] that writes to a [StringSink]. |
74 */ | 70 */ |
75 class StringSinkLogger implements Logger { | 71 class StringSinkLogger implements Logger { |
76 static const int _MAX_LINE_LENGTH = 512; | 72 static const int _MAX_LINE_LENGTH = 512; |
77 final StringSink _sink; | 73 final StringSink _sink; |
78 final List<_LoggerSection> _sectionStack = <_LoggerSection>[]; | 74 final List<_LoggerSection> _sectionStack = <_LoggerSection>[]; |
79 _LoggerSection _section = new _LoggerSection('', 'ROOT'); | 75 _LoggerSection _section = new _LoggerSection('', 'ROOT'); |
80 | 76 |
81 StringSinkLogger(this._sink); | 77 StringSinkLogger(this._sink); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 } | 119 } |
124 return str.split('\n').map((String line) { | 120 return str.split('\n').map((String line) { |
125 if (line.length > _MAX_LINE_LENGTH) { | 121 if (line.length > _MAX_LINE_LENGTH) { |
126 line = line.substring(0, _MAX_LINE_LENGTH) + '...'; | 122 line = line.substring(0, _MAX_LINE_LENGTH) + '...'; |
127 } | 123 } |
128 return line; | 124 return line; |
129 }).join('\n'); | 125 }).join('\n'); |
130 } | 126 } |
131 } | 127 } |
132 | 128 |
133 | |
134 class _LoggerSection { | 129 class _LoggerSection { |
135 final DateTime start = new DateTime.now(); | 130 final DateTime start = new DateTime.now(); |
136 final String indent; | 131 final String indent; |
137 final String name; | 132 final String name; |
138 _LoggerSection(this.indent, this.name); | 133 _LoggerSection(this.indent, this.name); |
139 } | 134 } |
140 | 135 |
141 | |
142 /** | 136 /** |
143 * A [Logger] that does nothing. | 137 * A [Logger] that does nothing. |
144 */ | 138 */ |
145 class _NullLogger implements Logger { | 139 class _NullLogger implements Logger { |
146 @override | 140 @override |
147 void enter(String name) { | 141 void enter(String name) {} |
148 } | |
149 | 142 |
150 @override | 143 @override |
151 void exit() { | 144 void exit() {} |
152 } | |
153 | 145 |
154 @override | 146 @override |
155 void log(Object obj) { | 147 void log(Object obj) {} |
156 } | |
157 | 148 |
158 @override | 149 @override |
159 LoggingTimer startTimer() { | 150 LoggingTimer startTimer() { |
160 return new LoggingTimer(this); | 151 return new LoggingTimer(this); |
161 } | 152 } |
162 } | 153 } |
163 | 154 |
164 | |
165 /** | 155 /** |
166 * A [StringSink] implementation that uses `print`. | 156 * A [StringSink] implementation that uses `print`. |
167 */ | 157 */ |
168 class _PrintStringSink implements StringSink { | 158 class _PrintStringSink implements StringSink { |
169 String _line = ''; | 159 String _line = ''; |
170 | 160 |
171 @override | 161 @override |
172 void write(Object obj) { | 162 void write(Object obj) { |
173 if (obj == null) { | 163 if (obj == null) { |
174 _line += 'null'; | 164 _line += 'null'; |
(...skipping 12 matching lines...) Expand all Loading... |
187 _line += new String.fromCharCode(charCode); | 177 _line += new String.fromCharCode(charCode); |
188 } | 178 } |
189 | 179 |
190 @override | 180 @override |
191 void writeln([Object obj = '']) { | 181 void writeln([Object obj = '']) { |
192 _line += obj; | 182 _line += obj; |
193 print(_line); | 183 print(_line); |
194 _line = ''; | 184 _line = ''; |
195 } | 185 } |
196 } | 186 } |
OLD | NEW |