Chromium Code Reviews| Index: sdk/lib/io/console.dart |
| diff --git a/sdk/lib/io/console.dart b/sdk/lib/io/console.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f3ec221f53398eee6434570e0cafbbdb73c7a450 |
| --- /dev/null |
| +++ b/sdk/lib/io/console.dart |
| @@ -0,0 +1,97 @@ |
| +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +part of dart.io; |
| + |
| +Console _console; |
| + |
| +Console get console { |
| + if (_console == null) { |
| + _console = new Console._(); |
| + } |
| + return _console; |
| +} |
| + |
| +/** |
| + * [Console] provides synchronous write access to stdout and stderr. |
| + * |
| + * The direct access to stdout and stderr through [stdout] and [stderr] |
| + * provides non-blocking async operations. |
| + */ |
| +class Console { |
| + final ConsoleSink _stdout; |
| + final ConsoleSink _stderr; |
| + |
| + Console._() |
| + : _stdout = new ConsoleSink._(1), |
| + _stderr = new ConsoleSink._(2); |
| + |
| + /** |
| + * Write to stdout. |
| + */ |
| + ConsoleSink get log => _stdout; |
| + |
| + /** |
| + * Write to stderr. |
| + */ |
| + ConsoleSink get error => _stderr; |
| +} |
| + |
| +/** |
| + * Sink class used for console writing. |
| + * |
| + * This class had a call method so you can call it directly. Calling |
|
Paul Berry
2014/11/14 14:06:45
s/had/has/
Søren Gjesse
2014/11/18 13:51:35
Done.
|
| + * it directly is the same as calling its `writeln` method. |
| + */ |
| +class ConsoleSink implements Sink<List<int>>, StringSink { |
| + IOSink _sink; |
| + |
| + ConsoleSink._(int fd) { |
| + _sink = new IOSink(new _ConsoleConsumer(fd)); |
| + } |
| + |
| + void call(String message) => _sink.writeln(message); |
|
Ivan Posva
2014/11/18 05:54:44
Wondering whether we should accept any message and
Søren Gjesse
2014/11/18 13:51:35
Good point. Changed type to Object. writeln alread
|
| + |
| + void add(List<int> data) => _sink.add(data); |
| + |
| + void close() {} |
| + |
| + void write(Object obj) => _sink.write(obj); |
| + |
| + void writeAll(Iterable objects, [String separator=""]) => |
| + _sink.writeAll(objects, separator); |
| + |
| + void writeCharCode(int charCode) => _sink.writeCharCode(charCode); |
| + |
| + void writeln([Object obj=""]) => _sink.writeln(obj); |
| +} |
| + |
| +class _ConsoleConsumer implements StreamConsumer<List<int>> { |
| + final _file; |
| + |
| + _ConsoleConsumer(int fd) : _file = _File._openStdioSync(fd); |
| + |
| + Future addStream(Stream<List<int>> stream) { |
| + var completer = new Completer(); |
| + var sub; |
| + sub = stream.listen( |
| + (data) { |
| + try { |
| + _file.writeFromSync(data); |
| + } catch (e, s) { |
| + sub.cancel(); |
| + completer.completeError(e, s); |
| + } |
| + }, |
| + onError: completer.completeError, |
| + onDone: completer.complete, |
| + cancelOnError: true); |
| + return completer.future; |
| + } |
| + |
| + Future close() { |
| + _file.closeSync(); |
| + return new Future.value(); |
| + } |
| +} |