OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 /// Message logging. | 5 /// Message logging. |
6 library log; | 6 library log; |
7 | 7 |
| 8 import 'dart:io'; |
8 import 'dart:async'; | 9 import 'dart:async'; |
9 | 10 |
10 import 'io.dart'; | 11 import 'io.dart'; |
11 import 'utils.dart'; | 12 import 'utils.dart'; |
12 | 13 |
13 typedef LogFn(Entry entry); | 14 typedef LogFn(Entry entry); |
14 final Map<Level, LogFn> _loggers = new Map<Level, LogFn>(); | 15 final Map<Level, LogFn> _loggers = new Map<Level, LogFn>(); |
15 | 16 |
16 /// The list of recorded log messages. Will only be recorded if | 17 /// The list of recorded log messages. Will only be recorded if |
17 /// [recordTranscript()] is called. | 18 /// [recordTranscript()] is called. |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 /// Enables recording of log entries. | 158 /// Enables recording of log entries. |
158 void recordTranscript() { | 159 void recordTranscript() { |
159 _transcript = <Entry>[]; | 160 _transcript = <Entry>[]; |
160 } | 161 } |
161 | 162 |
162 /// If [recordTranscript()] was called, then prints the previously recorded log | 163 /// If [recordTranscript()] was called, then prints the previously recorded log |
163 /// transcript to stderr. | 164 /// transcript to stderr. |
164 void dumpTranscript() { | 165 void dumpTranscript() { |
165 if (_transcript == null) return; | 166 if (_transcript == null) return; |
166 | 167 |
167 stderrSink.add('---- Log transcript ----\n'.codeUnits); | 168 stderr.writeln('---- Log transcript ----'); |
168 for (var entry in _transcript) { | 169 for (var entry in _transcript) { |
169 _logToStderrWithLabel(entry); | 170 _logToStderrWithLabel(entry); |
170 } | 171 } |
171 stderrSink.add('---- End log transcript ----\n'.codeUnits); | 172 stderr.writeln('---- End log transcript ----'); |
172 } | 173 } |
173 | 174 |
174 /// Sets the verbosity to "normal", which shows errors, warnings, and messages. | 175 /// Sets the verbosity to "normal", which shows errors, warnings, and messages. |
175 void showNormal() { | 176 void showNormal() { |
176 _loggers[Level.ERROR] = _logToStderr; | 177 _loggers[Level.ERROR] = _logToStderr; |
177 _loggers[Level.WARNING] = _logToStderr; | 178 _loggers[Level.WARNING] = _logToStderr; |
178 _loggers[Level.MESSAGE] = _logToStdout; | 179 _loggers[Level.MESSAGE] = _logToStdout; |
179 _loggers[Level.IO] = null; | 180 _loggers[Level.IO] = null; |
180 _loggers[Level.FINE] = null; | 181 _loggers[Level.FINE] = null; |
181 } | 182 } |
(...skipping 12 matching lines...) Expand all Loading... |
194 void showAll() { | 195 void showAll() { |
195 _loggers[Level.ERROR] = _logToStderrWithLabel; | 196 _loggers[Level.ERROR] = _logToStderrWithLabel; |
196 _loggers[Level.WARNING] = _logToStderrWithLabel; | 197 _loggers[Level.WARNING] = _logToStderrWithLabel; |
197 _loggers[Level.MESSAGE] = _logToStdoutWithLabel; | 198 _loggers[Level.MESSAGE] = _logToStdoutWithLabel; |
198 _loggers[Level.IO] = _logToStderrWithLabel; | 199 _loggers[Level.IO] = _logToStderrWithLabel; |
199 _loggers[Level.FINE] = _logToStderrWithLabel; | 200 _loggers[Level.FINE] = _logToStderrWithLabel; |
200 } | 201 } |
201 | 202 |
202 /// Log function that prints the message to stdout. | 203 /// Log function that prints the message to stdout. |
203 void _logToStdout(Entry entry) { | 204 void _logToStdout(Entry entry) { |
204 _logToStream(stdoutSink, entry, showLabel: false); | 205 _logToStream(stdout, entry, showLabel: false); |
205 } | 206 } |
206 | 207 |
207 /// Log function that prints the message to stdout with the level name. | 208 /// Log function that prints the message to stdout with the level name. |
208 void _logToStdoutWithLabel(Entry entry) { | 209 void _logToStdoutWithLabel(Entry entry) { |
209 _logToStream(stdoutSink, entry, showLabel: true); | 210 _logToStream(stdout, entry, showLabel: true); |
210 } | 211 } |
211 | 212 |
212 /// Log function that prints the message to stderr. | 213 /// Log function that prints the message to stderr. |
213 void _logToStderr(Entry entry) { | 214 void _logToStderr(Entry entry) { |
214 _logToStream(stderrSink, entry, showLabel: false); | 215 _logToStream(stderr, entry, showLabel: false); |
215 } | 216 } |
216 | 217 |
217 /// Log function that prints the message to stderr with the level name. | 218 /// Log function that prints the message to stderr with the level name. |
218 void _logToStderrWithLabel(Entry entry) { | 219 void _logToStderrWithLabel(Entry entry) { |
219 _logToStream(stderrSink, entry, showLabel: true); | 220 _logToStream(stderr, entry, showLabel: true); |
220 } | 221 } |
221 | 222 |
222 void _logToStream(EventSink<List<int>> sink, Entry entry, {bool showLabel}) { | 223 void _logToStream(IOSink sink, Entry entry, {bool showLabel}) { |
223 bool firstLine = true; | 224 bool firstLine = true; |
224 for (var line in entry.lines) { | 225 for (var line in entry.lines) { |
225 if (showLabel) { | 226 if (showLabel) { |
226 if (firstLine) { | 227 if (firstLine) { |
227 sink.add(entry.level.name.codeUnits); | 228 sink.write('${entry.level.name}: '); |
228 sink.add(': '.codeUnits); | |
229 } else { | 229 } else { |
230 sink.add(' | '.codeUnits); | 230 sink.write(' | '); |
231 } | 231 } |
232 } | 232 } |
233 | 233 |
234 sink.add(line.codeUnits); | 234 sink.writeln(line); |
235 sink.add('\n'.codeUnits); | |
236 | 235 |
237 firstLine = false; | 236 firstLine = false; |
238 } | 237 } |
239 } | 238 } |
OLD | NEW |