OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 // Output provider that collects the output in string buffers. | 5 // Output provider that collects the output in string buffers. |
6 | 6 |
7 library output_collector; | 7 library output_collector; |
8 | 8 |
9 import 'dart:async'; | 9 import 'dart:async'; |
10 | 10 |
11 class BufferedEventSink implements EventSink<String> { | 11 class BufferedEventSink implements EventSink<String> { |
12 StringBuffer sb = new StringBuffer(); | 12 StringBuffer sb = new StringBuffer(); |
13 String text; | 13 String text; |
14 | 14 |
15 void add(String event) { | 15 void add(String event) { |
16 sb.write(event); | 16 sb.write(event); |
17 } | 17 } |
18 | 18 |
19 void addError(errorEvent, [StackTrace stackTrace]) { | 19 void addError(errorEvent, [StackTrace stackTrace]) { |
20 // Do not support this. | 20 // Do not support this. |
21 } | 21 } |
22 | 22 |
23 void close() { | 23 void close() { |
24 text = sb.toString(); | 24 text = sb.toString(); |
25 sb = null; | 25 sb = null; |
26 } | 26 } |
27 } | 27 } |
28 | 28 |
| 29 class CloningEventSink implements EventSink<String> { |
| 30 final List<EventSink<String>> sinks; |
| 31 |
| 32 CloningEventSink(this.sinks); |
| 33 |
| 34 @override |
| 35 void add(String event) { |
| 36 sinks.forEach((EventSink<String> sink) => sink.add(event)); |
| 37 } |
| 38 |
| 39 @override |
| 40 void addError(errorEvent, [StackTrace stackTrace]) { |
| 41 sinks.forEach((EventSink<String> sink) { |
| 42 sink.addError(errorEvent, stackTrace); |
| 43 }); |
| 44 } |
| 45 |
| 46 @override |
| 47 void close() { |
| 48 sinks.forEach((EventSink<String> sink) => sink.close()); |
| 49 } |
| 50 } |
| 51 |
29 class OutputCollector { | 52 class OutputCollector { |
30 Map<String, Map<String, BufferedEventSink>> outputMap = {}; | 53 Map<String, Map<String, BufferedEventSink>> outputMap = {}; |
31 | 54 |
32 EventSink<String> call(String name, String extension) { | 55 EventSink<String> call(String name, String extension) { |
33 Map<String, BufferedEventSink> sinkMap = | 56 Map<String, BufferedEventSink> sinkMap = |
34 outputMap.putIfAbsent(extension, () => {}); | 57 outputMap.putIfAbsent(extension, () => {}); |
35 return sinkMap.putIfAbsent(name, () => new BufferedEventSink()); | 58 return sinkMap.putIfAbsent(name, () => new BufferedEventSink()); |
36 } | 59 } |
37 | 60 |
38 String getOutput(String name, String extension) { | 61 String getOutput(String name, String extension) { |
39 Map<String, BufferedEventSink> sinkMap = outputMap[extension]; | 62 Map<String, BufferedEventSink> sinkMap = outputMap[extension]; |
40 if (sinkMap == null) return null; | 63 if (sinkMap == null) return null; |
41 BufferedEventSink sink = sinkMap[name]; | 64 BufferedEventSink sink = sinkMap[name]; |
42 return sink != null ? sink.text : null; | 65 return sink != null ? sink.text : null; |
43 } | 66 } |
44 } | 67 } |
OLD | NEW |