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