|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| SystemOutLogSink.java | 0% | 33.3% | 33.3% | 31% |
|
||||||||||||||
| 1 | /*BEGIN_COPYRIGHT_BLOCK* | |
| 2 | ||
| 3 | PLT Utilities BSD License | |
| 4 | ||
| 5 | Copyright (c) 2007-2010 JavaPLT group at Rice University | |
| 6 | All rights reserved. | |
| 7 | ||
| 8 | Developed by: Java Programming Languages Team | |
| 9 | Rice University | |
| 10 | http://www.cs.rice.edu/~javaplt/ | |
| 11 | ||
| 12 | Redistribution and use in source and binary forms, with or without modification, are permitted | |
| 13 | provided that the following conditions are met: | |
| 14 | ||
| 15 | - Redistributions of source code must retain the above copyright notice, this list of conditions | |
| 16 | and the following disclaimer. | |
| 17 | - Redistributions in binary form must reproduce the above copyright notice, this list of | |
| 18 | conditions and the following disclaimer in the documentation and/or other materials provided | |
| 19 | with the distribution. | |
| 20 | - Neither the name of the JavaPLT group, Rice University, nor the names of the library's | |
| 21 | contributors may be used to endorse or promote products derived from this software without | |
| 22 | specific prior written permission. | |
| 23 | ||
| 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR | |
| 25 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 26 | FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND | |
| 27 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 28 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 29 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER | |
| 30 | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
| 31 | OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 32 | ||
| 33 | *END_COPYRIGHT_BLOCK*/ | |
| 34 | ||
| 35 | package edu.rice.cs.plt.debug; | |
| 36 | ||
| 37 | import java.io.*; | |
| 38 | import java.nio.charset.Charset; | |
| 39 | ||
| 40 | /** | |
| 41 | * A log sink that writes tagged, indented text to {@link System#out}. Automatically changes the destination | |
| 42 | * stream after updates are made to {@code System.out}. | |
| 43 | */ | |
| 44 | public class SystemOutLogSink extends IndentedTextLogSink { | |
| 45 | ||
| 46 | private final Charset _charset; | |
| 47 | private volatile BufferedWriter _writer; | |
| 48 | private volatile PrintStream _currentStream; // the OutputStream on which _writer is based | |
| 49 | ||
| 50 | /** Create a log sink to {@code System.err} using the platform's default charset. */ | |
| 51 | 4 | public SystemOutLogSink() { |
| 52 | 4 | super(); |
| 53 | 4 | _charset = Charset.defaultCharset(); |
| 54 | 4 | _writer = null; |
| 55 | 4 | _currentStream = null; |
| 56 | } | |
| 57 | ||
| 58 | /** Create a log sink to {@code System.err} using the given charset. */ | |
| 59 | 1 | public SystemOutLogSink(String charsetName) throws UnsupportedEncodingException { |
| 60 | 1 | super(); |
| 61 | 1 | _charset = Charset.forName(charsetName); |
| 62 | 1 | _currentStream = null; |
| 63 | } | |
| 64 | ||
| 65 | /** Create a log sink to {@code System.err} using the platform's default charset and the given line width. */ | |
| 66 | 0 | public SystemOutLogSink(int idealLineWidth) { |
| 67 | 0 | super(idealLineWidth); |
| 68 | 0 | _charset = Charset.defaultCharset(); |
| 69 | 0 | _writer = null; |
| 70 | 0 | _currentStream = null; |
| 71 | } | |
| 72 | ||
| 73 | /** Create a log to {@code System.err} using the given charset and line width. */ | |
| 74 | 0 | public SystemOutLogSink(String charsetName, int idealLineWidth) throws UnsupportedEncodingException { |
| 75 | 0 | super(idealLineWidth); |
| 76 | 0 | _charset = Charset.forName(charsetName); |
| 77 | 0 | _writer = null; |
| 78 | 0 | _currentStream = null; |
| 79 | } | |
| 80 | ||
| 81 | 0 | @Override protected BufferedWriter writer(Message m) { |
| 82 | // Make sure we reflect changes to System.out (via System.setOut()) | |
| 83 | 0 | PrintStream out = System.out; |
| 84 | 0 | if (_currentStream != out) { |
| 85 | // the update isn't atomic, but that's okay as long as _currentStream is updated last | |
| 86 | 0 | _writer = new BufferedWriter(new OutputStreamWriter(out, _charset)); |
| 87 | 0 | _currentStream = out; |
| 88 | } | |
| 89 | 0 | return _writer; |
| 90 | } | |
| 91 | ||
| 92 | 0 | public void close() throws IOException { _writer.close(); } |
| 93 | ||
| 94 | } |
|
||||||||||