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:async'; | 8 import 'dart:async'; |
| 9 import 'dart:io'; |
9 import 'io.dart'; | 10 import 'io.dart'; |
10 | 11 |
11 typedef LogFn(Entry entry); | 12 typedef LogFn(Entry entry); |
12 final Map<Level, LogFn> _loggers = new Map<Level, LogFn>(); | 13 final Map<Level, LogFn> _loggers = new Map<Level, LogFn>(); |
13 | 14 |
14 /// The list of recorded log messages. Will only be recorded if | 15 /// The list of recorded log messages. Will only be recorded if |
15 /// [recordTranscript()] is called. | 16 /// [recordTranscript()] is called. |
16 List<Entry> _transcript; | 17 List<Entry> _transcript; |
17 | 18 |
18 /// An enum type for defining the different logging levels. By default, [ERROR] | 19 /// An enum type for defining the different logging levels. By default, [ERROR] |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 /// Enables recording of log entries. | 147 /// Enables recording of log entries. |
147 void recordTranscript() { | 148 void recordTranscript() { |
148 _transcript = <Entry>[]; | 149 _transcript = <Entry>[]; |
149 } | 150 } |
150 | 151 |
151 /// If [recordTranscript()] was called, then prints the previously recorded log | 152 /// If [recordTranscript()] was called, then prints the previously recorded log |
152 /// transcript to stderr. | 153 /// transcript to stderr. |
153 void dumpTranscript() { | 154 void dumpTranscript() { |
154 if (_transcript == null) return; | 155 if (_transcript == null) return; |
155 | 156 |
156 stderrSink.add('---- Log transcript ----\n'.charCodes); | 157 stderr.writeString('---- Log transcript ----\n'); |
157 for (var entry in _transcript) { | 158 for (var entry in _transcript) { |
158 _logToStderrWithLabel(entry); | 159 _logToStderrWithLabel(entry); |
159 } | 160 } |
160 stderrSink.add('---- End log transcript ----\n'.charCodes); | 161 stderr.writeString('---- End log transcript ----\n'); |
161 } | 162 } |
162 | 163 |
163 /// Sets the verbosity to "normal", which shows errors, warnings, and messages. | 164 /// Sets the verbosity to "normal", which shows errors, warnings, and messages. |
164 void showNormal() { | 165 void showNormal() { |
165 _loggers[Level.ERROR] = _logToStderr; | 166 _loggers[Level.ERROR] = _logToStderr; |
166 _loggers[Level.WARNING] = _logToStderr; | 167 _loggers[Level.WARNING] = _logToStderr; |
167 _loggers[Level.MESSAGE] = _logToStdout; | 168 _loggers[Level.MESSAGE] = _logToStdout; |
168 _loggers[Level.IO] = null; | 169 _loggers[Level.IO] = null; |
169 _loggers[Level.FINE] = null; | 170 _loggers[Level.FINE] = null; |
170 } | 171 } |
(...skipping 12 matching lines...) Expand all Loading... |
183 void showAll() { | 184 void showAll() { |
184 _loggers[Level.ERROR] = _logToStderrWithLabel; | 185 _loggers[Level.ERROR] = _logToStderrWithLabel; |
185 _loggers[Level.WARNING] = _logToStderrWithLabel; | 186 _loggers[Level.WARNING] = _logToStderrWithLabel; |
186 _loggers[Level.MESSAGE] = _logToStdoutWithLabel; | 187 _loggers[Level.MESSAGE] = _logToStdoutWithLabel; |
187 _loggers[Level.IO] = _logToStderrWithLabel; | 188 _loggers[Level.IO] = _logToStderrWithLabel; |
188 _loggers[Level.FINE] = _logToStderrWithLabel; | 189 _loggers[Level.FINE] = _logToStderrWithLabel; |
189 } | 190 } |
190 | 191 |
191 /// Log function that prints the message to stdout. | 192 /// Log function that prints the message to stdout. |
192 void _logToStdout(Entry entry) { | 193 void _logToStdout(Entry entry) { |
193 _logToStream(stdoutSink, entry, showLabel: false); | 194 _logToStream(stdout, entry, showLabel: false); |
194 } | 195 } |
195 | 196 |
196 /// Log function that prints the message to stdout with the level name. | 197 /// Log function that prints the message to stdout with the level name. |
197 void _logToStdoutWithLabel(Entry entry) { | 198 void _logToStdoutWithLabel(Entry entry) { |
198 _logToStream(stdoutSink, entry, showLabel: true); | 199 _logToStream(stdout, entry, showLabel: true); |
199 } | 200 } |
200 | 201 |
201 /// Log function that prints the message to stderr. | 202 /// Log function that prints the message to stderr. |
202 void _logToStderr(Entry entry) { | 203 void _logToStderr(Entry entry) { |
203 _logToStream(stderrSink, entry, showLabel: false); | 204 _logToStream(stderr, entry, showLabel: false); |
204 } | 205 } |
205 | 206 |
206 /// Log function that prints the message to stderr with the level name. | 207 /// Log function that prints the message to stderr with the level name. |
207 void _logToStderrWithLabel(Entry entry) { | 208 void _logToStderrWithLabel(Entry entry) { |
208 _logToStream(stderrSink, entry, showLabel: true); | 209 _logToStream(stderr, entry, showLabel: true); |
209 } | 210 } |
210 | 211 |
211 void _logToStream(Sink<List<int>> sink, Entry entry, {bool showLabel}) { | 212 void _logToStream(OutputStream stream, Entry entry, {bool showLabel}) { |
212 bool firstLine = true; | 213 bool firstLine = true; |
213 for (var line in entry.lines) { | 214 for (var line in entry.lines) { |
214 if (showLabel) { | 215 if (showLabel) { |
215 if (firstLine) { | 216 if (firstLine) { |
216 sink.add(entry.level.name.charCodes); | 217 stream.writeString(entry.level.name); |
217 sink.add(': '.charCodes); | 218 stream.writeString(': '); |
218 } else { | 219 } else { |
219 sink.add(' | '.charCodes); | 220 stream.writeString(' | '); |
220 } | 221 } |
221 } | 222 } |
222 | 223 |
223 sink.add(line.charCodes); | 224 stream.writeString(line); |
224 sink.add('\n'.charCodes); | 225 stream.writeString('\n'); |
225 | 226 |
226 firstLine = false; | 227 firstLine = false; |
227 } | 228 } |
228 } | 229 } |
OLD | NEW |