OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 part of dart.io; |
| 6 |
| 7 Console _console; |
| 8 |
| 9 Console get console { |
| 10 if (_console == null) { |
| 11 _console = new Console._(); |
| 12 } |
| 13 return _console; |
| 14 } |
| 15 |
| 16 /** |
| 17 * [Console] provides synchronous write access to stdout and stderr. |
| 18 * |
| 19 * The direct access to stdout and stderr through [stdout] and [stderr] |
| 20 * provides non-blocking async operations. |
| 21 */ |
| 22 class Console { |
| 23 final ConsoleSink _stdout; |
| 24 final ConsoleSink _stderr; |
| 25 |
| 26 Console._() |
| 27 : _stdout = new ConsoleSink._(1), |
| 28 _stderr = new ConsoleSink._(2); |
| 29 |
| 30 /** |
| 31 * Write to stdout. |
| 32 */ |
| 33 ConsoleSink get log => _stdout; |
| 34 |
| 35 /** |
| 36 * Write to stderr. |
| 37 */ |
| 38 ConsoleSink get error => _stderr; |
| 39 } |
| 40 |
| 41 /** |
| 42 * Sink class used for console writing. |
| 43 * |
| 44 * This class has a call method so you can call it directly. Calling |
| 45 * it directly is the same as calling its `writeln` method. |
| 46 */ |
| 47 class ConsoleSink implements Sink<List<int>>, StringSink { |
| 48 IOSink _sink; |
| 49 |
| 50 ConsoleSink._(int fd) { |
| 51 _sink = new IOSink(new _ConsoleConsumer(fd)); |
| 52 } |
| 53 |
| 54 void call([Object message = ""]) => _sink.writeln(message); |
| 55 |
| 56 void add(List<int> data) => _sink.add(data); |
| 57 |
| 58 void close() {} |
| 59 |
| 60 void write(Object obj) => _sink.write(obj); |
| 61 |
| 62 void writeAll(Iterable objects, [String separator=""]) => |
| 63 _sink.writeAll(objects, separator); |
| 64 |
| 65 void writeCharCode(int charCode) => _sink.writeCharCode(charCode); |
| 66 |
| 67 void writeln([Object obj=""]) => _sink.writeln(obj); |
| 68 } |
| 69 |
| 70 class _ConsoleConsumer implements StreamConsumer<List<int>> { |
| 71 final _file; |
| 72 |
| 73 _ConsoleConsumer(int fd) : _file = _File._openStdioSync(fd); |
| 74 |
| 75 Future addStream(Stream<List<int>> stream) { |
| 76 var completer = new Completer(); |
| 77 var sub; |
| 78 sub = stream.listen( |
| 79 (data) { |
| 80 try { |
| 81 _file.writeFromSync(data); |
| 82 } catch (e, s) { |
| 83 sub.cancel(); |
| 84 completer.completeError(e, s); |
| 85 } |
| 86 }, |
| 87 onError: completer.completeError, |
| 88 onDone: completer.complete, |
| 89 cancelOnError: true); |
| 90 return completer.future; |
| 91 } |
| 92 |
| 93 Future close() { |
| 94 _file.closeSync(); |
| 95 return new Future.value(); |
| 96 } |
| 97 } |
OLD | NEW |